I am new to using GIS data in Python. I have two datasets in GeoJSON format. One of them has states of a country with multi-polygons representing each states. The other has multiple points of latitude and longitude, each one describing a particular branch information (also in the same country).

I am looking to integrate these two GeoJSON files so that I can get something like all the branches or the number of branches located in a particular state.

How can this be done in Python?

I've attached the code so far and the results that I've obtained below.

import geopandas as gpd
import geojson, json

with open(fname_states) as fs:
    data_states = json.load(fs)
with open(fname_branches) as fb:
    data_branches = json.load(fb)
for feature in data_states['features']:
    print (feature['geometry']['type'])
    print (feature['geometry']['coordinates'])

MultiPolygon [[[[-155.666192384, 18.921786345999976], ............, [-160.073803717, 22.00417732599999]]]]

MultiPolygon [[[[-122.40201532499998, 48.22521676600002],.............., [-122.96797834099999, 48.443794749999995]]]]

I've just attached two polygons output statement for reference. These coordinates indicate polygons which refer to a region.

for feature in data_branches['features']:
    print (feature['geometry']['type'])
    print (feature['geometry']['coordinates'])

Point [-84.11397141, 35.97929825] Point [-84.17377774, 35.89437917] Point [-83.84168579, 36.22807494] Point [-83.92360125, 36.08801205] Point [-84.02843334, 35.93056813] Point [-84.00351839, 36.04683377] Point [-83.97332803, 35.97456329]

This is the output of the branches file which also contain numerous other fields like branch id and other attributes.

So, I would like to add an extra field to the branches data as 'state'. I want to obtain this field by checking whether it lies in any of the regions that are covered by polygons in the states GeoJSON file. This is where I need help.

Please suggest an optimal method to achieve this.

  • Welcome to GIS SE! As a new user be sure to take the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. Have you looked at any spatial Python libraries yet? If so, which one(s) have you found that support GeoJSON?
    – PolyGeo
    Apr 13, 2018 at 6:30
  • Hi. Yes i have looked at the libraries and found pyshp shapely and geojson python libraries useful. I ve tried creating a few points and polygons and checking whether the points are in the polygon. was successful with that. But what iam stuck is reading the two geojson files and linking them. So that i can check whether the point from one file is inside the polygon in another file.
    – Adarsh
    Apr 13, 2018 at 6:34
  • Have you been able to read one GeoJSON file? A code snippet illustrating that might be enough for us to vote to re-open your question.
    – PolyGeo
    Apr 13, 2018 at 8:09

1 Answer 1


It sounds like you want to do a geometry intersection to 'join' two datasets by location.

This website has an example of what you want to do, and it looks something like this:

import json
from shapely import shape, Point

# load GeoJSON file containing sectors
with ('sectors.json', 'r') as f:
    js = json.load(f)

# construct point based on lat/long returned by geocoder
point = Point(45.4519896, -122.7924463)

# check each polygon to see if it contains the point
for feature in js['features']:
    polygon = shape(feature['geometry'])
    if polygon.contains(point):
        print 'Found containing polygon:', feature

Check out the Shapely docs for more info.

  • Thank you.. This works for me. But i had to write two for loops to get the data from both the files. And it works fine. Thank you.
    – Adarsh
    Apr 16, 2018 at 5:13
  • Yep, the code I posted was meant as an example. Can you mark this as the answer, please, if it helped answer?
    – Alex Leith
    Apr 16, 2018 at 6:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.