{"id":127327,"date":"2022-08-17T06:34:31","date_gmt":"2022-08-17T06:34:31","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=583523"},"modified":"2022-08-17T06:34:31","modified_gmt":"2022-08-17T06:34:31","slug":"python-convert-geojson-to-csv","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/08\/17\/python-convert-geojson-to-csv\/","title":{"rendered":"Python Convert GeoJSON to CSV"},"content":{"rendered":"\n<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;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2>What is GeoJSON?<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" 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>\n<h2>Example GeoJSON to CSV<\/h2>\n<p>Say, you have the following GeoJSON snippet:<\/p>\n<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>\n<p>You want to convert it to the following CSV format:<\/p>\n<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\n39.984,-75.343,,Point,Location A,Store\n40.12,-80.24,,Point,Location B,House\n41.427,-77.2,,Point,Location C,Office<\/pre>\n<h2>Python GeoJSON to CSV Conversion<\/h2>\n<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>\n<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\nimport csv geo_filename = 'my_file.json'\ncsv_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>\n<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>\n<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 &#8220;scriptable&#8221; variant to be used in the command line. This code is inspired by the GitHub but simplified significantly.<\/p>\n<p><strong>Example input:<\/strong><\/p>\n<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>\n<p><strong>Example output:<\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" 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\/uploads\/2022\/08\/image-44-300x224.png 300w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/figure>\n<\/div>\n<h2>GeoJSON to CSV in QGIS<\/h2>\n<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>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" 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\/uploads\/2022\/08\/image-43-300x192.png 300w\" sizes=\"auto, (max-width: 767px) 100vw, 767px\" \/><\/figure>\n<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>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i.stack.imgur.com\/616Wf.png\"><img decoding=\"async\" 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>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i.stack.imgur.com\/0Hi9Q.png\"><img decoding=\"async\" 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>\n<\/div>\n<h2>GeoJSON to CSV Online Converter<\/h2>\n<p>You can easily convert specific GeoJSON snippets to CSV using the following online converter:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.convertcsv.com\/geojson-to-csv.htm\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" 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\/uploads\/2022\/08\/image-42-300x201.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-42-768x515.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-42-1536x1029.png 1536w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-42.png 1603w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (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: { &#8220;type&#8221;: &#8220;FeatureCollection&#8221;, &#8220;features&#8221;: [ { &#8220;type&#8221;: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-127327","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127327"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127327\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}