0

I'm actually learning how to do some cartography with python but first I would like to convert my json file to a GeoJson dynamically with Python. This is how my Json looks like :

[
  {
    "shape_name": "unit_shape",
    "source_name": "7724C_BUSSY_SAINT_GEORGES_Bussycomore",
    "building_id": "7724C",
    "chaud_froid": "Chaud",
    "geojson": {
      "type": "LineString",
      "properties": {
        "densite_cc": null
      },
      "coordinates": [
        [
          2.726142,
          48.834359
        ],
        [
          2.726149,
          48.834367
        ],
        [
          2.726202,
          48.834422
        ],
        [
          2.726262,
          48.834429
        ],
        [
          2.726307,
          48.834434
        ],
        [
          2.726316,
          48.834435
        ]
      ]
    },
    "to_drop": null,
    "id_enquete": null,
    "shape_nom": null
  },
...(many other features similar to the precedent one)]

Can someone please help me ?

5
  • What’s a GeoJson ? Commented Mar 6, 2022 at 16:17
  • GeoJSON is described at geojson.org. It's just a somewhat standardized way of storing geographic data as JSON; there's nothing very exotic about it. So the question for the OP becomes: What have you tried? What problems did you encounter? What exactly do you need help with? Please read "How do I ask a good question". Commented Mar 6, 2022 at 18:08
  • Okay I reformulate my question. The problem here is that my file is written as a simple JSON containing geographical data (coordinates), this kind of files can't be read by GIS and can't be used with geographical libraries like Leaflet or Folium. So, my question is, how to rewrite it (reformat the syntaxe) as plain GeoJSON with Python. Commented Mar 7, 2022 at 12:52
  • You do that by writing a small program, probably using Python's json module, that reads the input file, makes the necessary adjustments, and writes the output file. If you run into problems with that, you are welcome to ask specific questions about those problems. Right now, this question reads like "please write my code for me". Commented Mar 13, 2022 at 18:53
  • Thank you for your answer, I'm actually a beginner in python so I didn't know that a module named json was available. My code is now done, and here it is in case another stacker encounters the same problem. Commented Mar 15, 2022 at 10:01

1 Answer 1

1
import json 

input_file=json.load(open("./data/data.json", "r", encoding="utf-8"))

geojs={
     "type": "FeatureCollection",
     "features":[
           {
                "type":"Feature",
                "geometry": {
                "type":"LineString",
                "coordinates":d["geojson"]["coordinates"],
            },
                "properties":d,
        
         } for d in input_file 
    ]  
 }

output_file=open("./data/geodata.json", "w", encoding="utf-8")
json.dump(geojs, output_file)


output_file.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.