1

In that part of the code I'm creating geodataframes (only point features) out of two CSV files. I want to merge all points from the two dataframes within a range of 150m. Therefore I create a buffer and then do a spatial join. However I need the original df later. Therefore I make a copy of the db before buffering. The join is working, but afterwards the original df (hikr_gdf) is containing polygons, not points. I don't get it, since I am not working on hikr_gdf, but on buffer_gdf? Probably I'm overseeing something simple, but what could be wrong?

files = ["../spatial_data/hikr_waypoints.csv", "../spatial_data/osm_waypoints.csv"]

gdfs = []
for file in files:
    df = pd.read_csv(file, delimiter=",")

    gdf = GeoDataFrame(
        df.drop(['latitude', 'longitude'], axis=1),
        crs={'init': 'epsg:4326'},  # wgs84
        geometry=[Point(xy) for xy in zip(df.longitude, df.latitude)])
    gdfs.append(gdf)

    gdf.to_file(f"{file[:-4]}_processed")

hikr_gdf = gdfs[0]
osm_gdf = gdfs[1]
hikr_gdf = hikr_gdf.to_crs({'init': 'epsg:3857'})
osm_gdf = osm_gdf.to_crs({'init': 'epsg:3857'})
hikr_gdf.to_file("../spatial_data/hikr_gdv.shp")

buffer_gdf = hikr_gdf
print(f"print1:\n{hikr_gdf.geometry}")

buffer_gdf.geometry = buffer_gdf.geometry.buffer(150)
print(f"print2:\n{hikr_gdf.geometry}")

hikr_gdf.to_file("../spatial_data/test.shp")

waypoints_gdf = gpd.sjoin(osm_gdf, buffer_gdf, how='left', op='within', lsuffix="osm", rsuffix='hikr')

The problem is between the two print-statements. The output is:

print1:
0      POINT (1238304.930 5936603.562)
1      POINT (1240420.095 5937074.189)
2      POINT (1241967.945 5936348.037)
3      POINT (1241604.869 5936252.491)
4      POINT (1258256.795 5973883.395)
                    ...               
478    POINT (1426811.788 6067915.272)
479    POINT (1430399.552 6066195.439)
480    POINT (1436366.417 6008753.012)
481    POINT (1437629.379 6017117.804)
482    POINT (1436299.730 6016980.603)
Name: geometry, Length: 483, dtype: geometry
print2:
0      POLYGON ((1238454.930 5936603.562, 1238454.208...
1      POLYGON ((1240570.095 5937074.189, 1240569.373...
2      POLYGON ((1242117.945 5936348.037, 1242117.223...
3      POLYGON ((1241754.869 5936252.491, 1241754.147...
4      POLYGON ((1258406.795 5973883.395, 1258406.073...
                             ...                        
478    POLYGON ((1426961.788 6067915.272, 1426961.066...
479    POLYGON ((1430549.552 6066195.439, 1430548.829...
480    POLYGON ((1436516.417 6008753.012, 1436515.695...
481    POLYGON ((1437779.379 6017117.804, 1437778.657...
482    POLYGON ((1436449.730 6016980.603, 1436449.008...
Name: geometry, Length: 483, dtype: geometry

I don't get why hikr_gdf is changing, when I am buffering buffer_gdf.

1

Consider this piece of you code:

buffer_gdf = hikr_gdf
print(f"print1:\n{hikr_gdf.geometry}")

buffer_gdf.geometry = buffer_gdf.geometry.buffer(150)
print(f"print2:\n{hikr_gdf.geometry}")

You can see that hikr_gdf changes by changing buffer_gdf. That is because, in Python, buffer_gdf = hikr_gdf doesn't make a copy, but only attaches a new name to the same GeoDataFrame.
If you want to make a copy that you can edit without changing the original, you explicitly need to make a copy:

buffer_gdf = hikr_gdf.copy()
| improve this answer | |
  • So I got something wrong about Python general, not about the function. Thank you for the answer! – Hade Mar 28 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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