I have a GeoJSON in below format:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"uuid": "___",
"type": "road",
"length": "10m",
},
"geometry": {
"type": "LineString",
"coordinates": [
[__________],
[__________],
[__________]
]
}
}]
}
I have written the below python script to try and read out the type of GeoJSON, known in this file as road
.
def process_incoming_file(self, bucket, key, event):
bulk_data = []
try:
obj = s3.get_object(Bucket=bucket, Key=key)
decoded_content = json.loads(obj['Body'].read().decode('utf-8'))
print(decoded_content)
if decoded_content.get('type') == "FeatureCollection":
for (idx, obj) in enumerate(decoded_content['features']):
bulk_item = {}
props = obj.get('properties')
name = obj['properties'].get('name') or 'NULL'
print(name)
keys = [title.lower() for title in props]
print('keys', keys)
type = obj['properties'].get('type', 'road')
print(obj['properties'].get('road'))
My issue is that in other GeoJSON files, type could be called something other than formofway
.
How do I write a statement to look for different names it could come under, e.g.
type = obj['properties'].get('type', 'road', 'othername', 'something')
and if one of those names is present within the properties part of the GeoJSON then print its associated value? e.g. road
.
CodePudding user response:
You could nest calls to get()
prop = obj['properties']
type = prop.get('type', prop.get('formofway', prop.get('othername', prop.get('something'))))
But this will get very deep if there are lots of possibilities. You can use a loop instead.
type_props = ['type', 'formofway', 'othername', 'something']
for prop in type_props:
if prop in obj['properties']:
type = obj['properties'][prop]
break