Search code examples
pythonpython-3.xpandasgeopandas

Intersection between 2 Geodataframe


I am doing some work on the Geopanda library, I have a shapefile with polygons and data on a excel sheet that I transform into points. I want to intersect the two DataFrames and export it to a file. I use also on both projections (WGS84) so that I can compare them. There should be at least some points that intersects the polygons. My intersect GeoSeries does not give me any points that fit into the polygon, but I don't see why...

I checked if the unit of the shapefile was really Kilometer and not somthing else. I am not proficient into GeoPlot so I can't really make sure what the GeoDataFrame look like.

f = pd.read_excel(io = 'C:\\Users\\peilj\\meteo_sites.xlsx')

#Converting panda dataframe into a GeoDataFrame with CRS projection
geometry = [Point(xy) for xy in zip(df.geoBreite, df.geoLaenge)]
df = df.drop(['geoBreite', 'geoLaenge'], axis=1)
crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
gdf = GeoDataFrame(df, crs=crs, geometry=geometry)

#Reading shapefile and creating buffer
gdfBuffer = geopandas.read_file(filename = 'C:\\Users\\peilj\\lkr_vallanUTM.shp')
gdfBuffer = gdfBuffer.buffer(100) #When the unit is kilometer

#Converting positions long/lat into shapely object
gdfBuffer = gdfBuffer.to_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

#Intersection coordonates from polygon Buffer and points of stations
gdf['intersection'] = gdf.geometry.intersects(gdfBuffer)
#Problem: DOES NOT FIND ANY POINTS INSIDE STATIONS !!!!!!!

#Giving CRS projection to the intersect GeoDataframe
gdf_final = gdf.to_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
gdf_final['intersection'] = gdf_final['intersection'].astype(int) #Shapefile does not accept bool

#Exporting to a file
gdf_final.to_file(driver='ESRI Shapefile', filename=r'C:\\GIS\\dwd_stationen.shp

The files needed: https://drive.google.com/open?id=11x55aNxPOdJVKDzRWLqrI3S_ExwbqCE9


Solution

  • two things:

    You need to swap geoBreite and geoLaenge when creating the points to:

    geometry = [Point(xy) for xy in zip(df.geoLaenge, df.geoBreite)]

    This is because shapely follows the x, y logic, not lat, lon.

    As for checking the intersection, you could do as follows:

    gdf['inside'] = gdf['geometry'].apply(lambda shp: shp.intersects(gdfBuffer.dissolve('LAND').iloc[0]['geometry']))
    

    which detects six stations inside the shape file:

    gdf['inside'].sum()
    

    ouputs:

    6
    

    So along with some other minor fixes we get:

    import geopandas as gpd
    from shapely.geometry import Point
    
    df = pd.read_excel(r'C:\Users\peilj\meteo_sites.xlsx')
    
    geometry = [Point(xy) for xy in zip(df.geoLaenge, df.geoBreite)]
    crs = {'init': 'epsg:4326'}
    gdf = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
    
    gdfBuffer = gpd.read_file(filename = r'C:\Users\peilj\lkr_vallanUTM.shp')
    gdfBuffer['goemetry'] = gdfBuffer['geometry'].buffer(100)
    
    gdfBuffer = gdfBuffer.to_crs(crs)
    
    gdf['inside'] = gdf['geometry'].apply(lambda shp: shp.intersects(gdfBuffer.dissolve('LAND').iloc[0]['geometry']))