-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeospatial.py
140 lines (105 loc) · 4.67 KB
/
geospatial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import warnings # Warning control
import pandas as pd
import geopandas as gpd
from pyproj import CRS
from shapely.geometry import Point
# import pygeos
# Filter Warnings
warnings.filterwarnings("ignore") # Suppress all warnings
def convert_to_geodata(
df, xcoord_col, ycoord_col, crs_epsg, geom_col_name="geometry"
):
"""
Convert a pandas DataFrame to a GeoPandas GeoDataFrame.
Parameters:
df : pandas.DataFrame
The DataFrame containing the data.
xcoord_col : str
The name of the column containing the x-coordinates.
ycoord_col : str
The name of the column containing the y-coordinates.
crs_epsg : str
The EPSG code of the coordinate reference system (e.g., "EPSG:4326").
geom_col_name : str, optional
The name of the geometry column in the resulting GeoDataFrame (default is "geometry").
Returns:
geo_df : geopandas.GeoDataFrame
The resulting GeoDataFrame with geometry and CRS set.
"""
try:
# Extract coordinates
x_coord = df[xcoord_col]
y_coord = df[ycoord_col]
# Create geometry column
geom_column = [Point(xy) for xy in zip(x_coord, y_coord)]
# Define CRS
crs = CRS(crs_epsg)
# Create GeoDataFrame
geo_df = gpd.GeoDataFrame(df, crs=crs, geometry=geom_column)
# Rename geometry column if necessary
if geom_col_name != "geometry":
geo_df = geo_df.rename_geometry(geom_col_name)
return geo_df
except KeyError as e:
raise KeyError(f"Column not found: {e}")
except Exception as e:
raise Exception(f"An error occurred during the conversion: {e}")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def find_point_neighbors(
central_point, target_points_gdf, central_key_column, buffer_radius
):
"""
Find points within a buffer radius from a single central point and assign the central point's key to the target points.
Parameters:
central_point : GeoSeries
GeoSeries containing the central point.
target_points_gdf : GeoDataFrame
GeoDataFrame containing the target points.
central_key_column : str
The column name in central_point containing the key values to assign to target points.
buffer_radius : float
The radius of the buffer around the central point in the units of the GeoDataFrame's coordinate reference system.
Returns:
GeoDataFrame
A GeoDataFrame of target points within the buffer, with the central point's key assigned.
Example:
# Apply the find_point_neighbors function to each row of central_gdf
results = central_gdf.apply(
lambda row: find_point_neighbors(row, target_points_gdf, central_key_colum, buffer_radius), axis=1
)
# Concatenate the results into a single GeoDataFrame
result_gdf = pd.concat(results.tolist(), ignore_index=True)
"""
# Extract the key value from the central point
key_value = central_point[central_key_column]
# Create a buffer around the central point
central_point_buffer = central_point.geometry.buffer(buffer_radius)
# Find target points within the buffer
filter_cond = target_points_gdf.geometry.within(central_point_buffer)
selected_points = target_points_gdf[filter_cond].copy()
# Assign the central point's key value to the target points
key_value_series = pd.Series(
data=[key_value] * len(selected_points), index=selected_points.index
)
selected_points.insert(
loc=len(selected_points.columns)-1,
column=central_key_column,
value=key_value_series,
)
# selected_points[central_key_column] = key_value
return selected_points
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# def find_point_neighbors_pygeos(central_point, target_points_gdf, central_key_column, buffer_radius):
# # Convert geometries to pygeos geometries
# central_point_geom = pygeos.from_shapely(central_point.geometry)
# target_points_geom = pygeos.from_shapely(target_points_gdf.geometry)
# # Create buffer around the central point
# buffer = pygeos.buffer(central_point_geom, buffer_radius)
# # Find points within the buffer
# within_buffer = pygeos.within(target_points_geom, buffer)
# # Filter the target points
# selected_points = target_points_gdf[within_buffer]
# # Assign the central point's key to selected points
# selected_points[central_key_column] = central_point[central_key_column]
# return selected_points
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -