清理您的数据
Elastic Stack Serverless
Elasticsearch 中的地理空间字段有一些限制,需要在上传之前解决。本节将介绍一些方法来帮助您解决此类数据中的常见问题。
使用 ogr2ogr(属于 GDAL/OGR 套件)将数据集转换为 GeoJSON 或 Esri Shapefile。 例如,使用以下命令将 GPX 文件转换为 JSON
# Example GPX file from https://www.topografix.com/gpx_sample_files.asp
#
# Convert the GPX waypoints layer into a GeoJSON file
$ ogr2ogr \
-f GeoJSON "waypoints.geo.json" \
"fells_loop.gpx" \
"waypoints"
# Extract the routes layer into a GeoJSON file
$ ogr2ogr -f "GeoJSON" "routes.geo.json" "fells_loop.gpx" "routes"
- 输出格式和文件名
- 输入文件名
- 输入图层(通常与文件名相同)
Elasticsearch 仅支持 WGS84 坐标参考系。 使用 ogr2ogr
从其他坐标系转换为 WGS84。
在以下示例中,ogr2ogr
将 shapefile 从 NAD83 转换为 WGS84。 由于源数据集中的 .prj
辅助文件,输入 CRS 会自动检测。
# Example NAD83 file from https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_5m.zip
#
# Convert the Census Counties shapefile to WGS84 (EPSG:4326)
$ ogr2ogr -f "Esri Shapefile" \
"cb_2018_us_county_5m.4326.shp" \
-t_srs "EPSG:4326" \
"cb_2018_us_county_5m.shp" \
"cb_2018_us_county_5m"
- 输出文件
- EPSG:4326 是 WGS84 的代码
- 输入文件
- 输入图层
有时,地理空间数据集由少量几何图形组成,这些几何图形包含大量单独的部分几何图形。 这种情况的一个很好的例子是详细的世界国家边界数据集,其中加拿大或菲律宾等国家的记录有数百个小岛屿几何图形。 根据数据集的最终用途,您可能需要分解此类数据集,以保持每个文档一个几何图形,从而大大提高索引的性能。
# Example NAD83 file from www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/2016/ler_000b16a_e.zip
#
# Check the number of input features
$ ogrinfo -summary ler_000b16a_e.shp ler_000b16a_e \
| grep "Feature Count"
Feature Count: 76
# Convert to WGS84 exploding the multiple geometries
$ ogr2ogr \
-f "Esri Shapefile" \
"ler_000b16a_e.4326-parts.shp" \
-explodecollections \
-t_srs "EPSG:4326" \
"ler_000b16a_e.shp" \
"ler_000b16a_e"
# Check the number of geometries in the output file
# to confirm the 76 records are exploded into 27 thousand rows
$ ogrinfo -summary ler_000b16a_e.4326-parts.shp ler_000b16a_e.4326 \
| grep "Feature Count"
Feature Count: 27059
- 输出文件
- 将多部分转换为单个记录
- 转换为 WGS84
- 输入文件
- 输入图层
包含大量零件的记录的数据集(如上面的示例所示)甚至可能会在 Kibana Maps 文件上传器中挂起。
某些机器生成的数据集存储的小数位数超过了严格必要的位数。 作为参考,GeoJSON RFC 7946 坐标精度部分 指定六位数字是常见的默认值,对应于地面上约 10 厘米。 Maps 应用程序中的文件上传器会自动将精度降低到 6 位小数,但对于大型数据集,最好在上传之前执行此操作。
在请求符合 RFC7946
的文件时,ogr2ogr
会生成带有 7 位小数的 GeoJSON 文件,但如果数据的使用允许,可以使用 COORDINATE_PRECISION
GeoJSON 图层创建选项 将其进一步缩小。
# Example NAD83 file from https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_5m.zip
#
# Generate a 2008 GeoJSON file
$ ogr2ogr \
-f GeoJSON \
"cb_2018_us_county_5m.4326.geo.json" \
-t_srs "EPSG:4326" \
-lco "RFC7946=NO" \
"cb_2018_us_county_5m.shp" \
"cb_2018_us_county_5m"
# Generate a RFC7946 GeoJSON file
$ ogr2ogr \
-f GeoJSON \
"cb_2018_us_county_5m.4326.RFC7946.geo.json" \
-t_srs "EPSG:4326" \
-lco "RFC7946=YES" \
"cb_2018_us_county_5m.shp" \
"cb_2018_us_county_5m"
# Generate a RFC7946 GeoJSON file with just 5 decimal figures
$ ogr2ogr \
-f GeoJSON \
"cb_2018_us_county_5m.4326.RFC7946_mini.geo.json" \
-t_srs "EPSG:4326" \
-lco "RFC7946=YES" \
-lco "COORDINATE_PRECISION=5" \
"cb_2018_us_county_5m.shp" \
"cb_2018_us_county_5m"
# Compare the disk size of the three output files
$ du -h cb_2018_us_county_5m.4326*.geo.json
7,4M cb_2018_us_county_5m.4326.geo.json
6,7M cb_2018_us_county_5m.4326.RFC7946.geo.json
6,1M cb_2018_us_county_5m.4326.RFC7946_mini.geo.json
- 输出文件
- 转换为 WGS84
- 请求 2008 GeoJSON 文件
- 输出文件
- 转换为 WGS84
- 请求 RFC7946 GeoJSON 文件
- 输出文件
- 转换为 WGS84
- 请求 RFC7946 GeoJSON 文件
- 缩小到仅 5 个小数位
区域数据集是多边形数据集,其中文档的边界不重叠。 这在行政边界、土地使用和其他连续数据集中很常见。 这种类型的数据集具有特殊的特点,即任何修改多边形线的地理空间操作都需要以相同的方式应用于多边形的公共边,以避免生成细间隙和重叠伪影。
mapshaper
是处理此类数据集的优秀工具,因为它了解这种性质的数据集并相应地处理它们。
根据区域数据集的使用情况,不同的地理空间精度可能就足够了。 用于显示整个行星的世界国家数据集不需要与南亚大陆国家地图相同的精度。
mapshaper
提供了一个 simplify
命令,该命令接受百分比、分辨率和不同的简化算法。
# Example NAD83 file from https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_5m.zip
#
# Generate a baseline GeoJSON file from OGR
$ ogr2ogr \
-f GeoJSON "cb_2018_us_county_5m.ogr.geo.json" \
-t_srs "EPSG:4326" \
-lco RFC7946=YES \
"cb_2018_us_county_5m.shp" \
"cb_2018_us_county_5m"
# Simplify at different percentages with mapshaper
$ for pct in 10 50 75 99; do \
mapshaper \
-i "cb_2018_us_county_5m.shp" \
-proj "EPSG:4326" \
-simplify "${pct}%" \
-o cb_2018_us_county_5m.mapshaper_${pct}.geo.json; \
done
# Compare the size of the output files
$ du -h cb_2018_us_county_5m*.geo.json
2,0M cb_2018_us_county_5m.mapshaper_10.geo.json
4,1M cb_2018_us_county_5m.mapshaper_50.geo.json
5,3M cb_2018_us_county_5m.mapshaper_75.geo.json
6,7M cb_2018_us_county_5m.mapshaper_99.geo.json
6,7M cb_2018_us_county_5m.ogr.geo.json
- 输入文件
- 输出投影
- 简化
- 输出文件
Maps 应用程序期望有效的 GeoJSON 或 Shapefile 数据集。 除了前面提到的 CRS 要求外,几何图形还需要有效。 ogr2ogr
和 mapshaper
都有选项来尝试修复无效几何图形
- OGR
-makevalid
选项 - Mapshaper
-clean
命令
ogr2ogr
和 mapshaper
是优秀的地理空间 ETL(提取、转换和加载)实用程序,可以做比这里看到的更多的事情。 详细阅读文档值得投资,可以通过删除不需要的字段、优化数据类型、验证值域等来提高数据集的质量。 最后,作为命令行实用程序,两者都可以自动化并添加到 QA 管道中。