![]() |
|
[Tut] Python Convert GeoJSON to CSV - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Convert GeoJSON to CSV (/thread-99830.html) |
[Tut] Python Convert GeoJSON to CSV - xSicKxBot - 08-20-2022 Python Convert GeoJSON to CSV 5/5 – (1 vote) What is GeoJSON? GeoJSON is an RFC standardized data format to encode geographic data structures such as Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon. GeoJSON is based on the JavaScript Object Notation (JSON).Example GeoJSON to CSV Say, you have the following GeoJSON snippet: Code: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, "properties": { "name": "Location A", "category": "Store" } }, { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-80.24, 40.12]}, "properties": { "name": "Location B", "category": "House" } }, { "type": "Feature", "geometry": {"type": "Point", "coordinates": [ -77.2, 41.427]}, "properties": { "name": "Location C", "category": "Office" } } ] }You want to convert it to the following CSV format: Code: latitude,longitude,altitude,geometry,name,category
39.984,-75.343,,Point,Location A,Store
40.12,-80.24,,Point,Location B,House
41.427,-77.2,,Point,Location C,OfficeThe Python code to convert a GeoJSON to a CSV in Python uses a combination of the Code: jsonCode: csvCode: import json
import csv geo_filename = 'my_file.json'
csv_filename = 'my_file.csv' def feature_to_row(feature, header): l = [] for k in header: l.append(feature['properties'][k]) coords = feature['geometry']['coordinates'] assert(len(coords)==2) l.extend(coords) return l with open(geo_filename, 'r') as geo_file: with open(csv_filename, 'w', newline='') as csv_file: geojson_data = json.load(geo_file) features = geojson_data['features'] csv_writer = csv.writer(csv_file) is_header = True header = [] for feature in features: if is_header: is_header = False header = list(feature['properties'].keys()) header.extend(['px','py']) csv_writer.writerow(header) csv_writer.writerow(feature_to_row(feature, feature['properties'].keys()))You can either copy&paste this code and run it in the same folder as your GeoJSON (of course, after renaming the input and output filenames. Or you can check out this excellent GitHub to get a more “scriptable” variant to be used in the command line. This code is inspired by the GitHub but simplified significantly. Example input: Code: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, "properties": { "name": "Location A", "category": "Store" } }, { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-80.24, 40.12]}, "properties": { "name": "Location B", "category": "House" } }, { "type": "Feature", "geometry": {"type": "Point", "coordinates": [ -77.2, 41.427]}, "properties": { "name": "Location C", "category": "Office" } } ] }Example output: ![]() GeoJSON to CSV in QGIS In QGIS, if you have a map like this one (source): ![]() You can convert GEOJSON to CSV right within QGIS by clicking Code: ExportCode: Save Feature As (source) (source)GeoJSON to CSV Online Converter You can easily convert specific GeoJSON snippets to CSV using the following online converter:
|