Search code examples
pythongeolocationgeopy

How to get center location from polygon geometry with geopy


I'm trying to get a location for each neighborhood in my city. I want a single lat/log pair for each neighborhood so I can then use that with folium. The local government provides open data in the form of a GEO JSON, but it contains perimeter, area, and limits.

This is the full file: http://cdn.buenosaires.gob.ar/datosabiertos/datasets/barrios/barrios.geojson

And this is an example:

features": [
{
"type": "Feature",
"properties": {
"barrio": "CHACARITA",
"comuna": 15,
"perimetro": 7724.85295457,
"area": 3115707.10627
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-58.4528200492791,
-34.5959886570639
],
[
-58.453655193137,
-34.5965557163041
],
[
-58.4537674321647,
-34.5966342484152
],
[
-58.4538163134148,
-34.5966684788922
],
[
-58.4547947928051,
-34.5973527273644
],
[
-58.4554840815948,
-34.5978347610252
],
[
-58.4559204833296,
-34.5976953435829
],
[
-58.4560093721285,
-34.5976669530232
],
[
-58.4560576047802,
-34.5976515472868
],
[
-58.4562363723257,
-34.5975900748435
],
[
-58.4564940053845,
-34.597501563774
],
[
-58.4570733158433,
-34.5973024999733
],
[
-58.4576142986162,
-34.5971165306991
],
[
-58.4578298572666,
-34.5970394940224
],
[
-58.4579314199407,
-34.5970031094655
],
[
-58.4579383597537,
-34.5970006235554
],
[
-58.457939151188,
-34.5970004324802
],
[
-58.4580314840712,
-34.5969781271249
],
[
-58.4582467474022,
-34.5969261254882
],
[
-58.4592081863103,
-34.5966936826245
],
[
-58.4592753814056,
-34.5966774864904
],
[
-58.4607717313584,
-34.5965908256843
],
[
-58.461008489241,
-34.5965771669178
],
[
-58.461500184562,
-34.5965469590603
],
[
-58.4615048306101,
-34.5965466787569
],
[
-58.4623220712384,
-34.5965110490604
],
[
-58.4624802220071,
-34.5965042183086
],
[
-58.4625206541306,
-34.5965018079147
],
[
-58.4637678264729,
-34.5964274496069
],
[
-58.4638637221527,
-34.5964294210885
],
[
-58.4646140234657,
-34.5964785682445
],
[
-58.4651592721924,
-34.5965757917791
],
[
-58.4651613793847,
-34.5965761676386
],
[
-58.4652710724273,
-34.5966020115654
],
[
-58.465339885911,
-34.5966119403979
],
[
-58.4654505835153,
-34.59661891152
],
[
-58.4655402165487,
-34.5966463016308
],
[
-58.4656201094845,
-34.5967043067336
],
[
-58.4655768128541,
-34.5965577078058
],
[
-58.465554434696,
-34.5964822872553
],
[
-58.4655118251641,
-34.5963386864843
],
[
-58.4654546732526,
-34.5961458635988
],
[
-58.4651988343882,
-34.5953364276981
],
[
-58.4650736836825,
-34.5949011987359
],
[
-58.4648267024928,
-34.5940422724471
],
[
-58.4644540217146,
-34.5927935584922
],
[
-58.4643817851099,
-34.5925515163591
],
[
-58.4644091198271,
-34.592467286843
],
[
-58.4650280527625,
-34.591936410724
],
[
-58.4658796865855,
-34.5912081588557
],
[
-58.4660489985854,
-34.5910867180571
],
[
-58.4663928573575,
-34.5908622593099
],
[
-58.4665306488686,
-34.5907723125485
],
[
-58.4665923016107,
-34.5907351963295
],
[
-58.4668281722696,
-34.5905930920952
],
[
-58.4657830008229,
-34.5896043925249
],
[
-58.4645332635208,
-34.5884221360889
],
[
-58.4630744816449,
-34.5870649213261
],
[
-58.4629751205092,
-34.5869805400343
],
[
-58.461952645947,
-34.5861122031234
],
[
-58.4618702043843,
-34.5860421891544
],
[
-58.4614638213396,
-34.5858332613902
],
[
-58.4603788016938,
-34.5855276845223
],
[
-58.4604139194362,
-34.5854024167589
],
[
-58.4604462802733,
-34.5852877643506
],
[
-58.4604255057231,
-34.5852032534777
],
[
-58.4604706411008,
-34.5850361353814
],
[
-58.4605356159209,
-34.5849439493523
],
[
-58.4605345318123,
-34.5840461665945
],
[
-58.4605331978237,
-34.5829502757213
],
[
-58.460531948912,
-34.5818532587471
],
[
-58.4605306146064,
-34.580772157502
],
[
-58.4605292809645,
-34.5796668306049
],
[
-58.4605276992685,
-34.5782946601008
],
[
-58.4605232166307,
-34.5782946690354
],
[
-58.4602248340306,
-34.5782952880515
],
[
-58.4601985594291,
-34.5782953479127
],
[
-58.4601021385732,
-34.5782955676544
],
[
-58.4597845714173,
-34.5783147333419
],
[
-58.4596950535931,
-34.5783201362424
],
[
-58.4585384246269,
-34.5783900346815
],
[
-58.4577639164538,
-34.57843681251
],
[
-58.4577436676417,
-34.5784380358534
],
[
-58.4573407479054,
-34.5785467557746
],
[
-58.457161766713,
-34.5785949886772
],
[
-58.4571475839219,
-34.5785988155734
],
[
-58.4559708829345,
-34.5789162808655
],
[
-58.4553992793144,
-34.579070479135
],
[
-58.4548623268909,
-34.5792427067768
],
[
-58.453533474128,
-34.5796688999369
],
[
-58.4530206113015,
-34.5798333742389
],
[
-58.4521930272086,
-34.5800988095872
],
[
-58.4521387166742,
-34.5801162118211
],
[
-58.4520222992006,
-34.5801535152443
],
[
-58.4512517652657,
-34.5804006516243
],
[
-58.451017590163,
-34.5804757586408
],
[
-58.4510096596172,
-34.5804789048291
],
[
-58.450952449802,
-34.5805016004481
],
[
-58.4505172245318,
-34.5806742198247
],
[
-58.4499989853758,
-34.5808797606534
],
[
-58.4496743253071,
-34.5810085114951
],
[
-58.449595440632,
-34.5810397949394
],
[
-58.4494416523924,
-34.5810963999448
],
[
-58.4491919812231,
-34.5811882776714
],
[
-58.448459568068,
-34.5814579241167
],
[
-58.4483934029757,
-34.5814822833589
],
[
-58.4483258230482,
-34.5815071661386
],
[
-58.4482895615355,
-34.5815205167813
],
[
-58.4482796339798,
-34.5815241718727
],
[
-58.4482750418364,
-34.5815261463976
],
[
-58.4480518326744,
-34.5816220940359
],
[
-58.4470924721564,
-34.5820344712672
],
[
-58.4461817147653,
-34.5824259258651
],
[
-58.4457859875885,
-34.5825960105066
],
[
-58.4446425154933,
-34.5830874842646
],
[
-58.4444715140105,
-34.5831609807395
],
[
-58.4444485731026,
-34.5831708409276
],
[
-58.4444069042627,
-34.583188756327
],
[
-58.4443997765097,
-34.5831918210746
],
[
-58.4444002339388,
-34.5831933878554
],
[
-58.4447856534,
-34.5845135514129
],
[
-58.4445857679104,
-34.5846657823276
],
[
-58.4434772519387,
-34.5854009946445
],
[
-58.4424251828668,
-34.5860987403321
],
[
-58.4413975399073,
-34.586780283069
],
[
-58.441089226108,
-34.5869846738708
],
[
-58.4401565270955,
-34.5876011493313
],
[
-58.4393890485044,
-34.5881083499955
],
[
-58.4386143204607,
-34.5886204031453
],
[
-58.4385455945534,
-34.5886617712813
],
[
-58.4385357500838,
-34.5886677008384
],
[
-58.4385362274348,
-34.5886679479287
],
[
-58.4385831147669,
-34.5886918427826
],
[
-58.4386416493153,
-34.5887216732237
],
[
-58.4506005822013,
-34.5943989522846
],
[
-58.4506310848741,
-34.5944213498744
],
[
-58.4509568590193,
-34.5946605554372
],
[
-58.4517045016343,
-34.5952095178055
],
[
-58.451886479147,
-34.5953370036164
],
[
-58.4520837007571,
-34.5954757585265
],
[
-58.4526072418644,
-34.5958441274193
],
[
-58.4528200492791,
-34.5959886570639
]
]
]
}
}

What I'm trying to do is to get a single lat/lon pair for each neighborhood. Is there way to calculate it? I've been looking around but can get my head around it. ¿Perhaps geopy isn't the tool for this?

Thanks for any guidance,

Cheers.


Solution

  • geopy is definitely not the tool for that. geopy is a wrapper for geocoding services, while you need a geometry engine, such as shapely.

    import requests
    from shapely.geometry import shape
    
    
    collection = requests.get('http://cdn.buenosaires.gob.ar/datosabiertos/datasets/barrios/barrios.geojson').json()
    features = collection["features"]
    for feature in features:
        s = shape(feature["geometry"])
        print(s.centroid)
    

    This yields the following:

    POINT (-58.4541801235578 -34.58836865970172)
    POINT (-58.46866483674238 -34.59742117689778)
    POINT (-58.44272494823578 -34.59882966797721)
    POINT (-58.49067707673827 -34.60424663818866)
    POINT (-58.4217445234198 -34.60922725926523)
    POINT (-58.44360306345074 -34.61682541891132)
    POINT (-58.48295678746965 -34.61619345688689)
    POINT (-58.50658094855392 -34.61929776178798)
    ...