justanotherguy
justanotherguy

Reputation: 83

Extracting coordinates (lat, lon) from MultiLineString GeoJSON in Python

I want to make a ScatterMapbox map based on the information contained in this GeoJSON file: https://cdn.buenosaires.gob.ar/datosabiertos/datasets/metrobus/recorrido-de-metrobus.geojson .

However, I am facing the following problem: the geometry in the GeoJSON is a MultiLineString. I provide a written example of the problem here:

{
"type": "FeatureCollection",
"name": "callejero_badata_WGS84",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 28686, "codigo": 17138, "nomoficial": "PUNTA ARENAS", "alt_izqini": 0, "alt_izqfin": 0, "alt_derini": 0, "alt_derfin": 0, "nomanter": null, "nom_mapa": "TÚNEL PUNTA ARENAS", "tipo_c": "TÚNEL", "long": 334.60341680080836, "sentido": "DOBLE", "cod_sent": 2, "observa": "Viaducto - Túnel inaugurado en Abril de 2009", "bicisenda": "-", "lado_ciclo": null, "recorrid_x": null, "ciclo_obse": null, "tooltip_bi": null, "red_jerarq": "VÍA DISTRIBUIDORA COMPLEMENTARIA", "red_tp": null, "ffcc": "SI", "tipo_ffcc": "Túnel", "COMUNA": 15, "COM_PAR": 15, "COM_IMPAR": 15, "BARRIO": "PATERNAL", "BARRIO_PAR": "PATERNAL", "BARRIO_IMP": "PATERNAL" }, 
"geometry": { "type": "MultiLineString", "coordinates": 
        [ [ [ -58.46939502613111, -34.590823162817173 ], 
        [ -58.469462454508552, -34.59098118466796 ], 
        [ -58.469669480276444, -34.591727559343369 ], 
        [ -58.469869735691702, -34.592625455739224 ], 
        [ -58.470073382303283, -34.593447590597258 ], 
        [ -58.470121607819273, -34.593775790374316 ] ] ] } }, ...
]
}

As you may see, the coordinates are composed by an array of lists (i.e. [ [ [ -58.46939502613111, -34.590823162817173 ], [ -58.469462454508552, -34.59098118466796 ] ] ]).

In order to plot the map, I need to extract from that file ALL the coordinates separately: all the latitudes must be on one list, and all the longitudes must be on another one. I would need something like this (following the example previously provided):

lats = [ -34.590823162817173,
         -34.59098118466796,
         -34.591727559343369,
         ...
       ]

lons = [ -58.46939502613111,
         -58.469462454508552,
         -58.469669480276444,
         ...
       ]

I tried several things, but got nothing close. Everything I did resulted in various errors and/or obtaining just one value/pair of values (coordinates) instead of the wanted list (above).

Everything I have now is the code where I load the GeoJSON, which I provide below.

metrobus = json.load(open("recorrido-de-metrobus.geojson"))

Is there any way to do something like this?

I really appreciate any advice, workaround or solution you might share with me. Thank you, in advance.


EDIT 2 Finally solved after following solution in this link: https://community.plotly.com/t/possible-bug-plotting-multipolygons-in-scattermapbox/33479 .

Thank you Yuri and fusion for your help.

Upvotes: 1

Views: 1882

Answers (1)

fusion
fusion

Reputation: 1397

# read your data:
metrobus = json.load(open("recorrido-de-metrobus.geojson"))

lats = []
lons = []

for i in data['features']:
    cord = i['geometry']['coordinates']
    for j in cord:
        for k in j:
            lats.append(k[1])
            lons.append(k[0])

Upvotes: 1

Related Questions