I am trying to get a KML-file (wijken.kml) into Shapely. The KML-file is validated against the proper XML schema, so I guess the input is correct.
Routes I have tried:
1) Converting to WKT or WKB format, and reading in with build in functions
Conversion:
ogr2ogr -f CSV wijken.csv wijken.kml -lco GEOMETRY=AS_WKT
ogr2ogr -f SQLite wijken.wkb wijken.kml
In shapely:
from shapely import wkt, wkb
f = open('../kml/wijken.wkb')
wkb.load(f)
Which gives (same for wkt()
):
ReadingError: Could not create geometry because of errors while reading input.
As it provides no further info, and the Python code wraps other (C?) libraries, I do not know what is wrong with the format. The CSV-file contains multiple columns, perhaps something is wrong there, but I have not found a comparable online WKT-example to test.
2) Converting to GeoJSON and using the build in Shapely asShape
function
ogr2ogr2 -f GeoJSON wijken.json wijken.kml
In Shapely:
import json
from shapely.geometry import asShape
f = open('wijken.json', 'r')
js = json.load(f)
f.close()
asShape(js)
Which gives:
ValueError: Unknown geometry type: featurecollection
This error is the same for a minimal, valid GeoJSON example. Looking at the Shapley code, the problem is that basic GeoJSON types such as "Feature" and "FeatureCollection" are not recognised. It is not clear which route to take to get from GeoJSON features to features Shapely understands.
3) Read in KML with fastkml, which returns Shapely objects
This works, but it seems to lose the properties/ExtendedData values in the KML (or at least does not pass it to the Shapely objects).
I feel that I'm missing something, it can't be so hard to get data into Shapely. Can someone point me towards the most robust/a working solution?
Answer
Shapely deals with geometric objects, not features or collections of features. See the manual on shape().
Your code (with JSON) could be:
import json
from shapely.geometry import shape
f = open('wijken.json', 'r')
js = json.load(f)
f.close()
for f in js['features']:
s = shape(f['geometry'])
...
No comments:
Post a Comment