Spaces:
Sleeping
Sleeping
Commit
·
f1a1df2
1
Parent(s):
b638e63
Delete modules/neighbors.py
Browse files- modules/neighbors.py +0 -47
modules/neighbors.py
DELETED
@@ -1,47 +0,0 @@
|
|
1 |
-
import geopandas as gpd
|
2 |
-
from shapely.geometry import Polygon
|
3 |
-
|
4 |
-
# Sample DataFrame with geometries (use your own data)
|
5 |
-
data = {'ID': [1, 2, 3],
|
6 |
-
'geometry': [Polygon([(0, 0), (0, 2), (2, 2), (2, 0)]),
|
7 |
-
Polygon([(2, 0), (2, 2), (4, 2), (4, 0)]),
|
8 |
-
Polygon([(4, 0), (4, 2), (6, 2), (6, 0)])]}
|
9 |
-
gdf = gpd.GeoDataFrame(data, crs="EPSG:4326")
|
10 |
-
|
11 |
-
# Create a new column to store the neighboring IDs
|
12 |
-
gdf['neighbors'] = None
|
13 |
-
|
14 |
-
# Iterate through the GeoDataFrame to find neighbors
|
15 |
-
for index, row in gdf.iterrows():
|
16 |
-
neighbors = []
|
17 |
-
for other_index, other_row in gdf.iterrows():
|
18 |
-
if index != other_index and row['geometry'].touches(other_row['geometry']):
|
19 |
-
neighbors.append(other_row['ID'])
|
20 |
-
gdf.at[index, 'neighbors'] = neighbors
|
21 |
-
|
22 |
-
# Display the DataFrame with neighbors
|
23 |
-
print(gdf[['ID', 'neighbors']])
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
def find_neighbors(geom_df):
|
28 |
-
geom_df['neighbors'] = geom_df.apply(lambda row: find_single_neighbors(row, geom_df), axis=1)
|
29 |
-
return geom_df
|
30 |
-
|
31 |
-
def find_single_neighbors(row, geom_df):
|
32 |
-
neighbors = []
|
33 |
-
for other_index, other_row in geom_df.iterrows():
|
34 |
-
if row.name != other_index and row['geometry'].touches(other_row['geometry']):
|
35 |
-
neighbors.append(other_row['ID'])
|
36 |
-
return neighbors
|
37 |
-
|
38 |
-
# Example usage:
|
39 |
-
# Replace 'your_data.geojson' with the path to your GeoJSON file or any other supported format
|
40 |
-
# Make sure the GeoDataFrame has a 'geometry' column
|
41 |
-
your_gdf = gpd.read_file('your_data.geojson')
|
42 |
-
|
43 |
-
# Call the function to find neighbors
|
44 |
-
result_gdf = find_neighbors(your_gdf)
|
45 |
-
|
46 |
-
# Print the resulting GeoDataFrame
|
47 |
-
print(result_gdf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|