Geotools 无法解析具有自定义属性的 GeoJSON 文件
Geotools fails to parse a GeoJSON file with custom properties
我正在使用 geotools 17.2 解析 GeoJSON 文件,如下所示:
try (FileInputStream is = new FileInputStream(routeFile)) {
FeatureJSON io = new FeatureJSON();
return io.readFeatureCollection(is);
}
我使用的 GeoJSON 文件如下:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": { },
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
}
}, {
"type": "Feature",
"properties": { "name": "test" },
"geometry": {
"type": "Point",
"coordinates": [44.912109375, 53.64463782485651]
}
}
]
}
解析失败,出现以下错误message/stacktrace:
Caused by: java.lang.IllegalArgumentException: No such attribute:name
at org.geotools.feature.simple.SimpleFeatureBuilder.set(SimpleFeatureBuilder.java:288)
at org.geotools.geojson.feature.FeatureHandler.endObject(FeatureHandler.java:176)
at org.geotools.geojson.DelegatingHandler.endObject(DelegatingHandler.java:81)
at org.geotools.geojson.feature.FeatureCollectionHandler.endObject(FeatureCollectionHandler.java:121)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.geotools.geojson.feature.FeatureJSON$FeatureCollectionIterator.readNext(FeatureJSON.java:746)
但是如果我把名字属性移到第一个点,那么解析就成功了。有没有办法让 geotools 在自定义属性方面更灵活一些? (或者这是库中的错误?)
您需要使用 readFeatureCollectionSchema 的第二种形式,它采用布尔值来强制 geotools 读取整个文件以计算出模式。
我正在使用 geotools 17.2 解析 GeoJSON 文件,如下所示:
try (FileInputStream is = new FileInputStream(routeFile)) {
FeatureJSON io = new FeatureJSON();
return io.readFeatureCollection(is);
}
我使用的 GeoJSON 文件如下:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": { },
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
}
}, {
"type": "Feature",
"properties": { "name": "test" },
"geometry": {
"type": "Point",
"coordinates": [44.912109375, 53.64463782485651]
}
}
]
}
解析失败,出现以下错误message/stacktrace:
Caused by: java.lang.IllegalArgumentException: No such attribute:name
at org.geotools.feature.simple.SimpleFeatureBuilder.set(SimpleFeatureBuilder.java:288)
at org.geotools.geojson.feature.FeatureHandler.endObject(FeatureHandler.java:176)
at org.geotools.geojson.DelegatingHandler.endObject(DelegatingHandler.java:81)
at org.geotools.geojson.feature.FeatureCollectionHandler.endObject(FeatureCollectionHandler.java:121)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.geotools.geojson.feature.FeatureJSON$FeatureCollectionIterator.readNext(FeatureJSON.java:746)
但是如果我把名字属性移到第一个点,那么解析就成功了。有没有办法让 geotools 在自定义属性方面更灵活一些? (或者这是库中的错误?)
您需要使用 readFeatureCollectionSchema 的第二种形式,它采用布尔值来强制 geotools 读取整个文件以计算出模式。