简体   繁体   English

从 GADM 获取国家级几何图形

[英]Get country level Geometries from GADM

I'm using geotools to open gadm36.shp from gadm.org which shapefile containing worldwide administrative areas.我正在使用 geotools从 gadm.org 打开 gadm36.shp,其中 shapefile 包含全球行政区域。 I'm trying to get a single Geometry (eg org.locationtech.jts.geom.MultiPolygon) for each country.我正在尝试为每个国家/地区获取一个几何图形(例如 org.locationtech.jts.geom.MultiPolygon)。 So if for example, there were 195 countries in this shapefile, I would have 195 Geometries.因此,例如,如果此 shapefile 中有 195 个国家/地区,我将有 195 个几何图形。 As a side note, I also have the GPKG file for the world so if using that is simpler, I'm happy to use that instead.作为旁注,我也有世界的 GPKG 文件,所以如果使用它更简单,我很乐意使用它。

// load collection from shapefile
File file = new File("<PATH>/gadm36.shp");
Map<String, Object> map = new HashMap<>();
map.put("url", file.toURI().toURL());
DataStore dataStore = DataStoreFinder.getDataStore(map);
String typeName = dataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);

FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(Filter.INCLUDE);

That last line collection has a size of 339127. It seems to contain every sate, country, town, village, etc. How do I get a smaller list of just the countries?最后一行集合的大小为 339127。它似乎包含每个州、国家、城镇、村庄等。如何获得仅包含国家/地区的较小列表?

Instead of Filter.INCLUDE you need to provide a filter that selects the features that you want.您需要提供一个过滤器来选择您想要的功能,而不是Filter.INCLUDE You'll need to play about a bit but if you just want countries then something like ENGTYPE_1 = 'Kingdom' or ENGTYPE_1 = 'Country' might do it.你需要玩一点,但如果你只是想要国家,那么像 ENGTYPE_1 = 'Kingdom' 或 ENGTYPE_1 = 'Country' 这样的东西可能会做到。

The easiest way to build a filter is to add the gt-cql module to your project and use构建过滤器的最简单方法是将gt-cql模块添加到您的项目中并使用

Filter f = ECQL.toFilter("ENGTYPE_1 = 'Kingdom' or ENGTYPE_1 = 'Country'");
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(f);

I'd also recommend using the geopackage rather than the shapefile as your filter will be passed to the database and you'll get the features back more quickly.我还建议使用geopackage而不是 shapefile,因为您的过滤器将被传递到数据库,您将更快地恢复功能。

And as a final style note URLs.fileToUrl(file) is preferred over file.toURI().toURL() as it handles windows files and spaces much better.作为最后的风格说明URLs.fileToUrl(file)优于file.toURI().toURL()因为它可以更好地处理 windows 文件和空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM