Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Convert GeoJSON to CSV

#1
Python Convert GeoJSON to CSV

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;583523&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>What is GeoJSON?</h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <a rel="noreferrer noopener" href="https://geojson.org/" data-type="URL" data-id="https://geojson.org/" target="_blank">GeoJSON</a> 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 (<a href="https://blog.finxter.com/convert-csv-to-json-in-python/" data-type="post" data-id="423132" target="_blank" rel="noreferrer noopener">JSON</a>). </p>
<h2>Example GeoJSON to CSV</h2>
<p>Say, you have the following GeoJSON snippet:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{ "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" } } ] }</pre>
<p>You want to convert it to the following CSV format:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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,Office</pre>
<h2>Python GeoJSON to CSV Conversion</h2>
<p>The Python code to convert a GeoJSON to a CSV in Python uses a combination of the <code>json</code> and <code>csv</code> packages.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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())) </pre>
<p>You can either copy&amp;paste this code and run it in the same folder as your GeoJSON (of course, after renaming the input and output filenames. </p>
<p>Or you can check out <a rel="noreferrer noopener" href="https://github.com/arnons1/geojson-to-csv/blob/master/geojson2csv.py" data-type="URL" data-id="https://github.com/arnons1/geojson-to-csv/blob/master/geojson2csv.py" target="_blank">this excellent GitHub</a> to get a more “scriptable” variant to be used in the command line. This code is inspired by the GitHub but simplified significantly.</p>
<p><strong>Example input:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{ "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" } } ] }</pre>
<p><strong>Example output:</strong></p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="573" height="427" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-44.png" alt="" class="wp-image-583612" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-44.png 573w, https://blog.finxter.com/wp-content/uplo...00x224.png 300w" sizes="(max-width: 573px) 100vw, 573px" /></figure>
</div>
<h2>GeoJSON to CSV in QGIS</h2>
<p>In QGIS, if you have a map like this one (<a href="https://gis.stackexchange.com/questions/388341/geojson-to-csv-with-geometry-using-qgis" data-type="URL" data-id="https://gis.stackexchange.com/questions/388341/geojson-to-csv-with-geometry-using-qgis" target="_blank" rel="noreferrer noopener">source</a>):</p>
<figure class="wp-block-image size-full"><img loading="lazy" width="767" height="490" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-43.png" alt="" class="wp-image-583562" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-43.png 767w, https://blog.finxter.com/wp-content/uplo...00x192.png 300w" sizes="(max-width: 767px) 100vw, 767px" /></figure>
<p class="has-global-color-8-background-color has-background">You can convert GEOJSON to CSV right within QGIS by clicking <code>Export</code>, then <code>Save Feature As</code> and select the Comma Separated Value [CSV] selector in the first dropdown menu.</p>
<div class="wp-block-image">
<figure class="aligncenter"><a href="https://i.stack.imgur.com/616Wf.png"><img src="https://i.stack.imgur.com/616Wf.png" alt="enter image description here"/></a><figcaption>(<a rel="noreferrer noopener" href="https://gis.stackexchange.com/questions/388341/geojson-to-csv-with-geometry-using-qgis" target="_blank">source</a>)</figcaption></figure>
</div>
<div class="wp-block-image">
<figure class="aligncenter"><a href="https://i.stack.imgur.com/0Hi9Q.png"><img src="https://i.stack.imgur.com/0Hi9Q.png" alt="enter image description here"/></a><figcaption>(<a rel="noreferrer noopener" href="https://gis.stackexchange.com/questions/388341/geojson-to-csv-with-geometry-using-qgis" target="_blank">source</a>)</figcaption></figure>
</div>
<h2>GeoJSON to CSV Online Converter</h2>
<p>You can easily convert specific GeoJSON snippets to CSV using the following online converter:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.convertcsv.com/geojson-to-csv.htm" target="_blank" rel="noreferrer noopener"><img loading="lazy" width="1024" height="686" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-42-1024x686.png" alt="" class="wp-image-583552" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-42-1024x686.png 1024w, https://blog.finxter.com/wp-content/uplo...00x201.png 300w, https://blog.finxter.com/wp-content/uplo...68x515.png 768w, https://blog.finxter.com/wp-content/uplo...6x1029.png 1536w, https://blog.finxter.com/wp-content/uplo...age-42.png 1603w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/08/...on-to-csv/
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016