1

I have to find if the point belongs to an area. Information is given by geojson file. I created DataFrame by:

choro = pd.concat([pd.DataFrame({'name':block['properties']['name'], 'area': shape(block['geometry'])}) for block in geojson['features']], ignore_index=True)

that code work with warning:

ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry

but works! btw, if you know how to modify the code to switch off that warning - much appreaite!

the dataframe looks like:

enter image description here

Here is the code for checking if point belong to the area:

for index, area in choro.iterrows():
    polygon = area['area']
    if polygon.contains(point):
        print('Point belongs to ',area['name'])

if I change my dataframe (exclude "shape" command) to get only coordinates (and no warning messages):

choro = pd.concat([pd.DataFrame({'name':block['properties']['name'], 'area': block['geometry']['coordinates']}) for block in geojson['features']], ignore_index=True)

I will have:

enter image description here

and obviously "shape" command for polygon:

polygon = shape(area['area'])

and:

polygon = MultiPolygon(area['area'])

are not working anymore.

The question is how I can get more clear code : to have polygons (or any other "working" structure) be stored in dataframe structure to work with shapely function to find if point is belong to the area and w/o any warnings or errors.

OcMaRUS
  • 329
  • 3
  • 13

1 Answers1

4

You can use geopandas package to easily create your DataFrame like this:

import geopandas as gpd
import geojson
import pandas as pd
with open("FileName.geojson", encoding='UTF-8') as json_file:
    geojson = geojson.load(json_file)
df = gpd.GeoDataFrame.from_features(geojson["features"])

Then you create this function to return the polygon name for any point:

def find_department(point):
    for index, row in df.iterrows():
        if row.geometry.contains(point):
            return row['name']

To define the geo-point you can use:

from shapely.geometry import shape, Point
point = Point(Longitude, latitude)

If you want to use shapely, I would like to till you that your code is working well without any errors or warnings but here is some of excepted mistakes:

  1. You put your point coordinates in reverse order. Remember it is (Longitude, latitude)
  2. The point and the geoJSON file May be not with the same coordinate reference system (CRS). Please make sure to export your geoJSON file with the correct EPSG value which is: WGS84 (EPSG: 4326) Check this PDF out for more details.
  • Thank you! I tried geopandas. unfortunately, it wasn't able to install them over Jupiter Notebook. But yes, will try to search new ideas by your replay. – OcMaRUS Dec 11 '21 at 19:27
  • 1
    To install geopandas use the cmd instead of jupyter and install it using conda as `conda install --channel conda-forge geopandas` You may need to install those packages first `conda install pandas fiona shapely pyproj rtree` – Mohammed Saleh Dec 11 '21 at 21:11
  • 1
    @OcMaRUS try this link for installing geopandas in anaconda https://stackoverflow.com/questions/54734667/error-installing-geopandas-a-gdal-api-version-must-be-specified-in-anaconda – Joe Wolf May 18 '22 at 17:16