-
-
Save KellyMWhitehead/e4369d9a72b2c6287a8ed78397ff117e to your computer and use it in GitHub Desktop.
1) proposed json fix for breakline issue.
2) how to handle featuers with no geometry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def featuresToShapely(self): | |
if len(self.features) == 0: | |
return [] | |
feats = [] | |
for feat in self.features: | |
# Very hacky, but some line features will not load json at all and unable to test if geometry type == unknown. | |
try: | |
featobj = json.loads(feat.ExportToJson()) | |
except: | |
geom = ogr.CreateGeometryFromWkt(feat.geometry().ExportToWkt()) | |
featobj = { | |
"geometry": json.loads(geom.ExportToJson())} | |
# TODO: ExportToJSON seems to fail when type: LineStringZM but it seems to work with this hack | |
# Essentially converting it to WKT and then back to JSON solves the problem | |
# if featobj['geometry']['type'] == 'Unknown': | |
# geom = ogr.CreateGeometryFromWkt(feat.geometry().ExportToWkt()) | |
# featobj = { | |
# "geometry": json.loads(geom.ExportToJson()) | |
# } | |
fields = {} | |
for f in self.fields: | |
fields[f] = feat.GetField(f) | |
feats.append({ | |
'FID': feat.GetFID(), | |
'geometry': shape(featobj['geometry']) if not featobj["geometry"] is None else None, # case when feature contains no geometry (rare) | |
'fields': fields | |
}) | |
return feats |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment