How to take an array within another array using python

Asked

Viewed 408 times

-1

I am trying to pick up an array of coordinates within another array in JSON document format, but am having difficulties.

This is the JSON document I’m using

{
  "bairro": "Vila Mariana",
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "color": "red"
      },
      "geometry": {
        "coordinates": [
          [
            [-46.664488,-23.595191],[-46.664941,-23.593657],[-46.663331,-23.581543],[-46.663972,-23.580556],[-46.661352,-23.579032],[-46.660373,-23.579655],
        [-46.659834,-23.580481],[-46.659682,-23.580653],[-46.653807,-23.583677],[-46.652670,-23.582900],[-46.654226,-23.580491],[-46.651061,-23.578770],
        [-46.651050,-23.579517],[-46.650932,-23.579841],[-46.649569,-23.581227],[-46.641584,-23.575588],[-46.641104,-23.576498],[-46.637409,-23.576827],
        [-46.637585,-23.578800],[-46.637242,-23.578938],[-46.636856,-23.578633],[-46.636287,-23.576303],[-46.635354,-23.575054],[-46.633176,-23.573825],
            [-46.631223,-23.573815],[-46.631502,-23.574228],[-46.632922,-23.574594],[-46.634213,-23.575324],[-46.634095,-23.575560],[-46.633784,-23.575521],
        [-46.633446,-23.575710],[-46.633921,-23.575888],[-46.634779,-23.577306],[-46.633057,-23.579980],[-46.632448,-23.579670],[-46.629101,-23.580181],
        [-46.629090,-23.581587],[-46.628645,-23.581545],[-46.628643,-23.581400],[-46.626203,-23.581671],[-46.626445,-23.581898],[-46.626445,-23.582124],
        [-46.622058,-23.582015],[-46.622047,-23.582359],[-46.622541,-23.582349],[-46.622530,-23.582909],[-46.622058,-23.584187],[-46.622648,-23.584551],
        [-46.622526,-23.585317],[-46.622875,-23.585387],[-46.623519,-23.584512],[-46.626856,-23.584551],[-46.626963,-23.586940],[-46.626244,-23.588267],
        [-46.625708,-23.588296],[-46.625526,-23.588955],[-46.625902,-23.589142],[-46.625430,-23.589545],[-46.627211,-23.589683],[-46.628305,-23.589142],
        [-46.629142,-23.589191],[-46.631717,-23.590341],[-46.631856,-23.591157],[-46.628359,-23.594559],[-46.624818,-23.595473],[-46.625156,-23.596596],
            [-46.622808,-23.597404],[-46.623999,-23.599646],[-46.633591,-23.599666],[-46.633623,-23.601534],[-46.629739,-23.601642],[-46.630361,-23.606587],
            [-46.631498,-23.607314],[-46.632045,-23.606586],[-46.633032,-23.606085],[-46.633404,-23.605470],[-46.635078,-23.605411],[-46.635110,-23.604054],
        [-46.636859,-23.603985],[-46.636752,-23.592669],[-46.639552,-23.593574],[-46.639295,-23.591303],[-46.642026,-23.591343],[-46.645534,-23.590704],
        [-46.645577,-23.590901],[-46.646682,-23.591442],[-46.647422,-23.592818],[-46.652239,-23.591314],[-46.664488,-23.595191]
      ],
      [
            [-46.664588,-23.595291],[-46.665041,-23.593757],[-46.663431,-23.581643],[-46.664072,-23.580656],[-46.661452,-23.579132],[-46.660473,-23.579755],
        [-46.659934,-23.580581],[-46.659782,-23.580753],[-46.653907,-23.583777],[-46.652770,-23.583000],[-46.654326,-23.580591],[-46.651161,-23.578870],
        [-46.651150,-23.579617],[-46.651032,-23.579941],[-46.649669,-23.581327],[-46.641684,-23.575688],[-46.641204,-23.576598],[-46.637509,-23.576927],
        [-46.637685,-23.578900],[-46.637342,-23.579038],[-46.636956,-23.578733],[-46.636387,-23.576403],[-46.635454,-23.575154],[-46.633276,-23.573925],
            [-46.631323,-23.573915],[-46.631602,-23.574328],[-46.633022,-23.574694],[-46.634313,-23.575424],[-46.634195,-23.575660],[-46.633884,-23.575621],
        [-46.633546,-23.575810],[-46.634021,-23.575988],[-46.634879,-23.577406],[-46.633157,-23.580080],[-46.632548,-23.579770],[-46.629201,-23.580281],
        [-46.629190,-23.581687],[-46.628745,-23.581645],[-46.628743,-23.581500],[-46.626303,-23.581771],[-46.626545,-23.581998],[-46.626545,-23.582224],
        [-46.622158,-23.582115],[-46.622147,-23.582459],[-46.622641,-23.582449],[-46.622630,-23.583009],[-46.622158,-23.584287],[-46.622748,-23.584651],
        [-46.622626,-23.585417],[-46.622975,-23.585487],[-46.623619,-23.584612],[-46.626956,-23.584651],[-46.627063,-23.587040],[-46.626344,-23.588367],
        [-46.625808,-23.588396],[-46.625626,-23.589055],[-46.626002,-23.589242],[-46.625530,-23.589645],[-46.627311,-23.589783],[-46.628405,-23.589242],
        [-46.629242,-23.589291],[-46.631817,-23.590441],[-46.631956,-23.591257],[-46.628459,-23.594659],[-46.624918,-23.595573],[-46.625256,-23.596696],
            [-46.622908,-23.597504],[-46.624099,-23.599746],[-46.633691,-23.599766],[-46.633723,-23.601634],[-46.629839,-23.601742],[-46.630461,-23.606687],
            [-46.631598,-23.607414],[-46.632145,-23.606686],[-46.633132,-23.606185],[-46.633504,-23.605570],[-46.635178,-23.605511],[-46.635210,-23.604154],
        [-46.636959,-23.604085],[-46.636852,-23.592769],[-46.639652,-23.593674],[-46.639395,-23.591403],[-46.642126,-23.591443],[-46.645634,-23.590804],
        [-46.645677,-23.591001],[-46.646782,-23.591542],[-46.647522,-23.592918],[-46.652339,-23.591414],[-46.664588,-23.595291]
          ]
    ],
        "type": "Polygon"  
      }
    }
  ]
}

My main problem is I’m trying to get the first list, but I can only get both. My application uses Mongodb I’m trying to use their query language to search for it.

That’s the part of the code I’m trying to search:

busca_bairro = bairros.find_one({'bairro': bairro}, {'_id': 0})

        busca_on_circulo = [[lon, lat], 3.10686 / 3963.2]
        busca_no_bairro = busca_bairro['features'][0]['geometry']

        busca_resaturante = restaurantes.find({'location': {
            '$geoWithin': {'$centerSphere': busca_on_circulo,'$geometry': busca_no_bairro}}, 'tipo': {'$all': texto}})

1 answer

0

Hello, Andy! I solved your problem as follows, I don’t know if it’s exactly what you would like, but maybe it will give you some important insights.

Initially, I used python’s lib json to load and process the data. Then I used her load method to load the data

import json
raw_dados = open("dados.json","r")    
dados = json.load(raw_dados)

Note that if you use the "type" function in the variable data, it will return that it is a dictionary, so we must search in the key/value format

Analyzing the JSON, I was able to observe that the coordinates are within a list, whose key is "Features", so I extracted this list

features = dados["features"]

Now, if you print Features, you will see that it is not in the dictionary format, but in a list. To take it out, just select the only element inside it

features = features[0]

Now yes, Features is a dictionary! Just now return the coordinates

geometria = features["geometry"]
coordenadas = geometria["coordinates"]

Your coordinates will be in the variable of the same name.

If you want to give an optimized, you can also do as follows:

dados = json.load(dados_raw)
coordenadas = dados["features"][0]["geometry"]['coordinates']

Notice that I just pieced together the whole search into just one variable

I recommend that you read the python JSON function. It will be very useful for you in several cases! I hope I’ve helped you, anything, just call!

Documentation of lib: https://docs.python.org/3/library/json.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.