0

Problem

Hi, we are working on an ETL that cleans geodataframes according to it's geometries, we're trying to match some geometries inside others. For that we try to use some Geopandas shapely functions like overlaps, contains and covers, but the only one that seems to work is overlaps, but it's not working the way that we need.

Outer geometry

The image from above is our general shapes, these are the ones that we want to be over the other shapes.

Shapes that we need to clean

What we need to do is to delete the shapes that are outside the green area, that is the shapes that we presented as general.

What we've tried

The main problem is that when we use the overlaps function it gets the result presented in the next image.

import geopandas as gpd 
from ..config import db

def transform (gdf: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    locality_data = gpd.GeoDataFrame.from_postgis(sql='SELECT * FROM localities', con=db.engine, geom_col='geometry')
    for upz_index, upz in gdf.iterrows():
        if not upz['geometry'] is None:
            if not filter_data(upz, locality_data):
                gdf = gdf.drop(upz_index)
    gdf = gdf.reset_index()
    return gdf

def filter_data(upz_geodataframe: gpd.GeoDataFrame, locality_geodataframe: gpd.GeoDataFrame) -> bool:
    for locality_index, locality in locality_geodataframe.iterrows():
        print(f"upz: {upz_geodataframe['CMNOMUPLA']} - loc: {locality['CMNOMLOCAL']}")
        if locality['geometry'].overlaps(other=upz_geodataframe['geometry']):
            print('Overlaps')
            return True
    return False

Resulting shapes after overlaps

We created the function filter_data to associate each gray shape to each green shape.

D4-Ramirez
  • 21
  • 3

0 Answers0