Search code examples
javacoordinatesshapefilecoordinate-systemscensus

Obtaining census block groups from shapefile based on latlong inputs - Java


I am new to shapefile processing. Kindly guide me on how to achieve my below query.

I am using this shapefile tl_2018_us_aiannh.shp from census.gov : TIGER-LINE. I am to obtain the census block group entities like Block, Tract, County subdivision and County details from the shapefile based on the latitude and longitude provided by the user.

My requirement is to achieve this by shapefile alone and not through any API's.

Can someone help on which framework I can achieve this?

What I've tried/using so far:

  • I have used GeoTools to read the shapefile . Can I continue using the same? Will my requirement be achievable by this tool?
  • I have gone through a documentation from census.gov which states:

The Census Bureau assigns a code and these appear in fields such as “TRACTCE”, where “CE” stands for Census. Finally, state-submitted codes end in “ST”, such as “SLDLST”, and local education agency codes end in “LEA”, as in “ELSDLEA”.

Which I tried in my code by:

File file = new File("D:\\tl_2018_us_aiannh.shp");

        try {
            Map<String, String> connect = new HashMap();
            connect.put("url", file.toURI().toString());

            DataStore dataStore = DataStoreFinder.getDataStore(connect);
            String[] typeNames = dataStore.getTypeNames();
            String typeName = typeNames[0];

            System.out.println("Reading content " + typeName);

            SimpleFeatureSource featureSource = dataStore
                    .getFeatureSource(typeName);
            SimpleFeatureCollection collection = featureSource.getFeatures();
            SimpleFeatureIterator iterator = collection.features();

            try {
                while (iterator.hasNext()) {
                    SimpleFeature feature = iterator.next();
                    GeometryAttribute sourceGeometry = feature
                            .getDefaultGeometryProperty();
                    String name = (String) (feature).getAttribute("TRACTCE");
                    Property property = feature.getProperty("TRACTCE");
                    System.out.println(property);
                }
            } finally {
                iterator.close();
            }

        } catch (Throwable e) {
            e.getMessage();
        }

But I am receiving null as the value.

Any help would be much helpful.


Solution

  • I have found the solution to this. Hope this would be helpful to someone in need.

    SimpleFeature is the type that has the attributes of shape files that you can check when you try to debug or print a line on runtime. You can use the SimpleFeature to get the property. The attributes can be achieved by:

      try {
         while (iterator.hasNext()) {
             SimpleFeature feature = iterator.next();
             Property intptlat = feature.getProperty("TRACTCE");
         }
     }
    

    Make sure you are choosing the Block Groups as the layer type for download in Tiger-Line or which ever site is concerned, where you download the shape file.