2 years ago

#67447

test-img

hi_ho_2

Python - extract certain values from GeoJSON

I have a GeoJSON file containing over 46,000 features, but I am only looking to extract 60 of them. The 60 features have a reference number which has been saved as CSV (see pic below for format of CSV)

E.g. of GeoJSON format

{
"type": "FeatureCollection",
"name": "entities",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "Layer": "Ref02-B", "SubClasses": "AcDbEntity:AcDbPolyline", "EntityHandle": "18D49" }, "geometry": { "type": "LineString", "coordinates": [ [ etc ] ]
]
}

The python code below works, but is extracting any feature which has a digit for the layer name. So for example, a layer 0 is being extracted. How can I specify that it only picks out the layer name if it contains Ref and 2 digits?

E.g. Ref01

import json
import csv

with open('THE_CSV.csv') as f:
    cr = csv.reader(f)
    header = next(cr)
    ref_column = header.index('Ref')  # column name
    references = [(row[ref_column]) for row in cr]

with open('THE_GEOJSON.geojson') as f:
    geo_json = json.load(f)

geo_json['features'] = [
    feature for feature in geo_json['features']
    if (feature['properties']['Layer'][3:4]) in references #looking for refs named Ref01 to Ref60
]

with open('OUTPUT.geojson', 'w') as f: #writing out to file to check output
   json.dump(geo_json, f)

CSV snippet

python

csv

geojson

0 Answers

Your Answer

Accepted video resources