Creating Point in GeoDataFrage gives Error: unhashable type: 'Point'

38 views Asked by At

Im trying to create a new geodataframe with a point as one of the Attributes. It's all going to happen in a loop in the end, as I then want to concat multiple gdf's into one, but what is giving me a headache is this part:

        from shapely.geometry import Point
        import geopandas as gpd
        
        ptX = 13.435621213 #row2['geometry'].x
        ptY = 52.480377234 #row2['geometry'].y
        gdf_this = gpd.GeoDataFrame({('name', Point(ptX, ptY))})

as it gives the Error: TypeError: unhashable type: 'Point'

I assume it has something to do with the reference vs copy thing, so I've tried creating a temporary point first and then using

gdf_this = gpd.GeoDataFrame({('name', copy.deepcopy(temp_point))})

but that gives the same error.

The documentation for shapely.Points lists a method reverse() which is supposed to return a copy of the geometry, but Point(ptX, ptY).reverse() gives the Error AttributeError: 'Point' object has no attribute 'reverse'?!?

1

There are 1 answers

1
MH Zardary On BEST ANSWER

There are a few steps you better to consider:

1. Resolving the TypeError: unhashable type: 'Point':

Use a list of tuples: GeoDataFrame expects a list of tuples for its data, even when creating a single-row DataFrame. Here's the corrected code:

gdf_this = gpd.GeoDataFrame([('name', Point(ptX, ptY))])

2. Clarifying the AttributeError: 'Point' object has no attribute 'reverse':

Shapely's reverse() method: It's primarily used for line-like geometries (LineStrings, MultiLineStrings) to reverse their coordinates. Points don't have a meaningful "reverse" operation, hence the error. Corrected code for creating multiple GeoDataFrames and concatenating them:

import geopandas as gpd
from shapely.geometry import Point

# Assuming you have your data in a list of tuples
data = [
    ('name1', 13.435621213, 52.480377234),
    ('name2', 15.6789, 49.1234),
    # ... more data
]

# Create a list to store the individual GeoDataFrames
gdf_list = []

for name, x, y in data:
    point = Point(x, y)
    gdf = gpd.GeoDataFrame([(name, point)], columns=['name', 'geometry'])
    gdf_list.append(gdf)

# Concatenate the GeoDataFrames
gdf_final = gpd.concat(gdf_list)
print(gdf_final)