根据 latlong 输入从 shapefile 获取人口普查块组 - Java

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

我是 shapefile 处理的新手。请指导我如何实现以下查询。

我正在使用 tl_2018_us_aiannh.shp 来自 census.gov 的形状文件:TIGER-LINE。我要获得 人口普查区块组 实体,例如 BlockTractCounty基于用户提供的纬度和经度的形状文件中的细分详细信息。

我的要求是通过 shapefile 单独实现,而不是通过任何 API。

有人可以帮助我实现这个目标的框架吗?

到目前为止我tried/using:

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”.

我在我的代码中尝试了 :

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();
        }

但我收到的值为 null。

任何帮助都会很有帮助。

我找到了解决方法。希望对有需要的人有所帮助。

SimpleFeature 是具有形状文件属性的类型,您可以在尝试调试或在运行时打印一行时检查这些属性。您可以使用 SimpleFeature 来获取 属性。属性可以通过:

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

确保您选择 Block Groups 作为要在 Tiger-Line 中下载的图层类型,或者您下载形状文件的任何站点。