tablegis 0.0.2__tar.gz → 0.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tablegis-0.0.2/tablegis.egg-info → tablegis-0.0.4}/PKG-INFO +117 -1
- {tablegis-0.0.2 → tablegis-0.0.4}/README.md +113 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/README_pypi.md +114 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/pyproject.toml +3 -1
- tablegis-0.0.4/tablegis/__init__.py +15 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/tablegis/core.py +229 -1
- {tablegis-0.0.2 → tablegis-0.0.4/tablegis.egg-info}/PKG-INFO +117 -1
- {tablegis-0.0.2 → tablegis-0.0.4}/tablegis.egg-info/requires.txt +2 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/tests/test_core.py +185 -2
- tablegis-0.0.2/tablegis/__init__.py +0 -9
- {tablegis-0.0.2 → tablegis-0.0.4}/LICENSE +0 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/MANIFEST.in +0 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/setup.cfg +0 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/tablegis/utils.py +0 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/tablegis.egg-info/SOURCES.txt +0 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/tablegis.egg-info/dependency_links.txt +0 -0
- {tablegis-0.0.2 → tablegis-0.0.4}/tablegis.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tablegis
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: A powerful Python library for geospatial data processing, analysis and conversion - integrating pandas with GIS operations
|
|
5
5
|
Author-email: Non-existent987 <jiaqi0425_000@163.com>
|
|
6
6
|
License: MIT License
|
|
@@ -40,6 +40,8 @@ Requires-Dist: pandas
|
|
|
40
40
|
Requires-Dist: numpy
|
|
41
41
|
Requires-Dist: scipy
|
|
42
42
|
Requires-Dist: pyproj
|
|
43
|
+
Requires-Dist: geopandas
|
|
44
|
+
Requires-Dist: shapely
|
|
43
45
|
Dynamic: license-file
|
|
44
46
|
|
|
45
47
|
# tablegis
|
|
@@ -226,6 +228,120 @@ print(result)
|
|
|
226
228
|
| D | 116.408 | 39.919 | 12958479.28 | 4854178.552 |
|
|
227
229
|
|
|
228
230
|
|
|
231
|
+
### 4、Generate buffers of the specified range based on the longitude and latitude columns in the table and add the geometry.
|
|
232
|
+
```python
|
|
233
|
+
import pandas as pd
|
|
234
|
+
import tablegis as tg
|
|
235
|
+
|
|
236
|
+
df = pd.DataFrame({
|
|
237
|
+
'lon': [116.4074, 121.4737],
|
|
238
|
+
'lat': [39.9042, 31.2304],
|
|
239
|
+
'buffer_size': [500, 1000]
|
|
240
|
+
})
|
|
241
|
+
# Set a 100-meter buffer zone
|
|
242
|
+
res_100 = tg.add_buffer(df,'lon','lat',100)
|
|
243
|
+
# Set the buffer range according to the numbers in the "buffer_size" column.
|
|
244
|
+
res_buffer_size = tg.add_buffer(df,'lon','lat','buffer_size')
|
|
245
|
+
print(res_100)
|
|
246
|
+
print(res_buffer_size)
|
|
247
|
+
```
|
|
248
|
+
**Result Display:**
|
|
249
|
+
## df table
|
|
250
|
+
|
|
251
|
+
| | lon | lat | buffer_size |
|
|
252
|
+
|---|----------|---------|-------------|
|
|
253
|
+
| 0 | 116.4074 | 39.9042 | 500 |
|
|
254
|
+
| 1 | 121.4737 | 31.2304 | 1000 |
|
|
255
|
+
|
|
256
|
+
## Set a 100-meter buffer zone
|
|
257
|
+
|
|
258
|
+
| | lon | lat | buffer_size | geometry |
|
|
259
|
+
|---|----------|---------|-------------|-------------------------------------------------|
|
|
260
|
+
| 0 | 116.4074 | 39.9042 | 500 | POLYGON ((116.40857 39.90421, 116.40856 39.904... |
|
|
261
|
+
| 1 | 121.4737 | 31.2304 | 1000 | POLYGON ((121.47475 31.23036, 121.47474 31.230... |
|
|
262
|
+
|
|
263
|
+
## Set the buffer range according to the numbers in the "buffer_size" column.
|
|
264
|
+
|
|
265
|
+
| | lon | lat | buffer_size | geometry |
|
|
266
|
+
|---|----------|---------|-------------|-------------------------------------------------|
|
|
267
|
+
| 0 | 116.4074 | 39.9042 | 500 | POLYGON ((116.41325 39.90423, 116.41322 39.903... |
|
|
268
|
+
| 1 | 121.4737 | 31.2304 | 1000 | POLYGON ((121.48417 31.23003, 121.48408 31.229... |
|
|
269
|
+
### 5、Convert the latitude and longitude columns in the table into point-shaped geometries and then transform them into a gdf.
|
|
270
|
+
```python
|
|
271
|
+
import pandas as pd
|
|
272
|
+
import tablegis as tg
|
|
273
|
+
|
|
274
|
+
df = pd.DataFrame({
|
|
275
|
+
'lon': [116.4074, 121.4737, 113.2644],
|
|
276
|
+
'lat': [39.9042, 31.2304, 23.1291],
|
|
277
|
+
'city': ['Beijing', 'Shanghai', 'Guangzhou']
|
|
278
|
+
})
|
|
279
|
+
# Generate points based on latitude and longitude
|
|
280
|
+
result1 = tg.add_points(df)
|
|
281
|
+
print(result1)
|
|
282
|
+
```
|
|
283
|
+
**Result Display:**
|
|
284
|
+
|
|
285
|
+
| lon | lat | city | geometry |
|
|
286
|
+
|-----------|------------|-----------|------------------------------|
|
|
287
|
+
| 116.4074 | 39.9042 | Beijing | POINT (116.4074 39.9042) |
|
|
288
|
+
| 121.4737 | 31.2304 | Shanghai | POINT (121.4737 31.2304) |
|
|
289
|
+
| 113.2644 | 23.1291 | Guangzhou | POINT (113.2644 23.1291) |
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
### 6、Concentrate the latitude and longitude data in the table using the method of expansion and recombination, and add the fused id as well as the range geom.
|
|
293
|
+
```python
|
|
294
|
+
import pandas as pd
|
|
295
|
+
import tablegis as tg
|
|
296
|
+
# Prepare test data
|
|
297
|
+
test_data = pd.DataFrame({
|
|
298
|
+
'lon': [116.40, 116.41, 116.50, 116.51],
|
|
299
|
+
'lat': [39.90, 39.91, 39.95, 39.96],
|
|
300
|
+
'name': ['A', 'B', 'C', 'D'],
|
|
301
|
+
'value': [1, 2, 3, 4]
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
# Test 1: Return geometry
|
|
305
|
+
result_no_geom = tg.add_buffer_groupbyid(
|
|
306
|
+
test_data,
|
|
307
|
+
lon='lon',
|
|
308
|
+
lat='lat',
|
|
309
|
+
distance=1000,
|
|
310
|
+
columns_name='group_id',
|
|
311
|
+
id_label_prefix='id_',
|
|
312
|
+
geom=True
|
|
313
|
+
)
|
|
314
|
+
# Test 2: Do not return geometry
|
|
315
|
+
result_geom = tg.add_buffer_groupbyid(
|
|
316
|
+
test_data,
|
|
317
|
+
lon='lon',
|
|
318
|
+
lat='lat',
|
|
319
|
+
distance=1000,
|
|
320
|
+
columns_name='group_id',
|
|
321
|
+
id_label_prefix='id_',
|
|
322
|
+
geom=False
|
|
323
|
+
)
|
|
324
|
+
```
|
|
325
|
+
**Result Display:**
|
|
326
|
+
## no geom
|
|
327
|
+
| lon | lat | name | value | group_id |
|
|
328
|
+
|-------|-------|------|-------|----------|
|
|
329
|
+
| 116.40| 39.90 | A | 1 | id_0 |
|
|
330
|
+
| 116.41| 39.91 | B | 2 | id_0 |
|
|
331
|
+
| 116.50| 39.95 | C | 3 | id_1 |
|
|
332
|
+
| 116.51| 39.96 | D | 4 | id_1 |
|
|
333
|
+
|
|
334
|
+
## geom
|
|
335
|
+
| lon | lat | name | value | group_id | geometry |
|
|
336
|
+
|-------|-------|------|-------|----------|---------|
|
|
337
|
+
| 116.40| 39.90 | A | 1 | id_0 | POLYGON ((116.41149 39.8983, 116.41122 39.8974...) |
|
|
338
|
+
| 116.41| 39.91 | B | 2 | id_0 | POLYGON ((116.41149 39.8983, 116.41122 39.8974...) |
|
|
339
|
+
| 116.50| 39.95 | C | 3 | id_1 | POLYGON ((116.51149 39.94829, 116.51122 39.947...) |
|
|
340
|
+
| 116.51| 39.96 | D | 4 | id_1 | POLYGON ((116.51149 39.94829, 116.51122 39.947...) |
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
229
345
|
## Contributing
|
|
230
346
|
|
|
231
347
|
Contributions in all forms are welcome, including feature requests, bug reports, and code contributions.
|
|
@@ -184,6 +184,119 @@ print(result)
|
|
|
184
184
|
| C | 116.404 | 39.916 | 12958034.01 | 4853743.126 |
|
|
185
185
|
| D | 116.408 | 39.919 | 12958479.28 | 4854178.552 |
|
|
186
186
|
|
|
187
|
+
### 4、Generate buffers of the specified range based on the longitude and latitude columns in the table and add the geometry.
|
|
188
|
+
```python
|
|
189
|
+
import pandas as pd
|
|
190
|
+
import tablegis as tg
|
|
191
|
+
|
|
192
|
+
df = pd.DataFrame({
|
|
193
|
+
'lon': [116.4074, 121.4737],
|
|
194
|
+
'lat': [39.9042, 31.2304],
|
|
195
|
+
'buffer_size': [500, 1000]
|
|
196
|
+
})
|
|
197
|
+
# Set a 100-meter buffer zone
|
|
198
|
+
res_100 = tg.add_buffer(df,'lon','lat',100)
|
|
199
|
+
# Set the buffer range according to the numbers in the "buffer_size" column.
|
|
200
|
+
res_buffer_size = tg.add_buffer(df,'lon','lat','buffer_size')
|
|
201
|
+
print(res_100)
|
|
202
|
+
print(res_buffer_size)
|
|
203
|
+
```
|
|
204
|
+
**Result Display:**
|
|
205
|
+
## df table
|
|
206
|
+
|
|
207
|
+
| | lon | lat | buffer_size |
|
|
208
|
+
|---|----------|---------|-------------|
|
|
209
|
+
| 0 | 116.4074 | 39.9042 | 500 |
|
|
210
|
+
| 1 | 121.4737 | 31.2304 | 1000 |
|
|
211
|
+
|
|
212
|
+
## Set a 100-meter buffer zone
|
|
213
|
+
|
|
214
|
+
| | lon | lat | buffer_size | geometry |
|
|
215
|
+
|---|----------|---------|-------------|-------------------------------------------------|
|
|
216
|
+
| 0 | 116.4074 | 39.9042 | 500 | POLYGON ((116.40857 39.90421, 116.40856 39.904... |
|
|
217
|
+
| 1 | 121.4737 | 31.2304 | 1000 | POLYGON ((121.47475 31.23036, 121.47474 31.230... |
|
|
218
|
+
|
|
219
|
+
## Set the buffer range according to the numbers in the "buffer_size" column.
|
|
220
|
+
|
|
221
|
+
| | lon | lat | buffer_size | geometry |
|
|
222
|
+
|---|----------|---------|-------------|-------------------------------------------------|
|
|
223
|
+
| 0 | 116.4074 | 39.9042 | 500 | POLYGON ((116.41325 39.90423, 116.41322 39.903... |
|
|
224
|
+
| 1 | 121.4737 | 31.2304 | 1000 | POLYGON ((121.48417 31.23003, 121.48408 31.229... |
|
|
225
|
+
|
|
226
|
+
### 5、Convert the latitude and longitude columns in the table into point-shaped geometries and then transform them into a gdf.
|
|
227
|
+
```python
|
|
228
|
+
import pandas as pd
|
|
229
|
+
import tablegis as tg
|
|
230
|
+
|
|
231
|
+
df = pd.DataFrame({
|
|
232
|
+
'lon': [116.4074, 121.4737, 113.2644],
|
|
233
|
+
'lat': [39.9042, 31.2304, 23.1291],
|
|
234
|
+
'city': ['Beijing', 'Shanghai', 'Guangzhou']
|
|
235
|
+
})
|
|
236
|
+
# Generate points based on latitude and longitude
|
|
237
|
+
result1 = tg.add_points(df)
|
|
238
|
+
print(result1)
|
|
239
|
+
```
|
|
240
|
+
**Result Display:**
|
|
241
|
+
|
|
242
|
+
| lon | lat | city | geometry |
|
|
243
|
+
|-----------|------------|-----------|------------------------------|
|
|
244
|
+
| 116.4074 | 39.9042 | Beijing | POINT (116.4074 39.9042) |
|
|
245
|
+
| 121.4737 | 31.2304 | Shanghai | POINT (121.4737 31.2304) |
|
|
246
|
+
| 113.2644 | 23.1291 | Guangzhou | POINT (113.2644 23.1291) |
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
### 6、Concentrate the latitude and longitude data in the table using the method of expansion and recombination, and add the fused id as well as the range geom.
|
|
250
|
+
```python
|
|
251
|
+
import pandas as pd
|
|
252
|
+
import tablegis as tg
|
|
253
|
+
# Prepare test data
|
|
254
|
+
test_data = pd.DataFrame({
|
|
255
|
+
'lon': [116.40, 116.41, 116.50, 116.51],
|
|
256
|
+
'lat': [39.90, 39.91, 39.95, 39.96],
|
|
257
|
+
'name': ['A', 'B', 'C', 'D'],
|
|
258
|
+
'value': [1, 2, 3, 4]
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
# Test 1: Return geometry
|
|
262
|
+
result_no_geom = tg.add_buffer_groupbyid(
|
|
263
|
+
test_data,
|
|
264
|
+
lon='lon',
|
|
265
|
+
lat='lat',
|
|
266
|
+
distance=1000,
|
|
267
|
+
columns_name='group_id',
|
|
268
|
+
id_label_prefix='id_',
|
|
269
|
+
geom=True
|
|
270
|
+
)
|
|
271
|
+
# Test 2: Do not return geometry
|
|
272
|
+
result_geom = tg.add_buffer_groupbyid(
|
|
273
|
+
test_data,
|
|
274
|
+
lon='lon',
|
|
275
|
+
lat='lat',
|
|
276
|
+
distance=1000,
|
|
277
|
+
columns_name='group_id',
|
|
278
|
+
id_label_prefix='id_',
|
|
279
|
+
geom=False
|
|
280
|
+
)
|
|
281
|
+
```
|
|
282
|
+
**Result Display:**
|
|
283
|
+
## no geom
|
|
284
|
+
| lon | lat | name | value | group_id |
|
|
285
|
+
|-------|-------|------|-------|----------|
|
|
286
|
+
| 116.40| 39.90 | A | 1 | id_0 |
|
|
287
|
+
| 116.41| 39.91 | B | 2 | id_0 |
|
|
288
|
+
| 116.50| 39.95 | C | 3 | id_1 |
|
|
289
|
+
| 116.51| 39.96 | D | 4 | id_1 |
|
|
290
|
+
|
|
291
|
+
## geom
|
|
292
|
+
| lon | lat | name | value | group_id | geometry |
|
|
293
|
+
|-------|-------|------|-------|----------|---------|
|
|
294
|
+
| 116.40| 39.90 | A | 1 | id_0 | POLYGON ((116.41149 39.8983, 116.41122 39.8974...) |
|
|
295
|
+
| 116.41| 39.91 | B | 2 | id_0 | POLYGON ((116.41149 39.8983, 116.41122 39.8974...) |
|
|
296
|
+
| 116.50| 39.95 | C | 3 | id_1 | POLYGON ((116.51149 39.94829, 116.51122 39.947...) |
|
|
297
|
+
| 116.51| 39.96 | D | 4 | id_1 | POLYGON ((116.51149 39.94829, 116.51122 39.947...) |
|
|
298
|
+
|
|
299
|
+
|
|
187
300
|
## Contributing
|
|
188
301
|
|
|
189
302
|
Contributions in all forms are welcome, including feature requests, bug reports, and code contributions.
|
|
@@ -182,6 +182,120 @@ print(result)
|
|
|
182
182
|
| D | 116.408 | 39.919 | 12958479.28 | 4854178.552 |
|
|
183
183
|
|
|
184
184
|
|
|
185
|
+
### 4、Generate buffers of the specified range based on the longitude and latitude columns in the table and add the geometry.
|
|
186
|
+
```python
|
|
187
|
+
import pandas as pd
|
|
188
|
+
import tablegis as tg
|
|
189
|
+
|
|
190
|
+
df = pd.DataFrame({
|
|
191
|
+
'lon': [116.4074, 121.4737],
|
|
192
|
+
'lat': [39.9042, 31.2304],
|
|
193
|
+
'buffer_size': [500, 1000]
|
|
194
|
+
})
|
|
195
|
+
# Set a 100-meter buffer zone
|
|
196
|
+
res_100 = tg.add_buffer(df,'lon','lat',100)
|
|
197
|
+
# Set the buffer range according to the numbers in the "buffer_size" column.
|
|
198
|
+
res_buffer_size = tg.add_buffer(df,'lon','lat','buffer_size')
|
|
199
|
+
print(res_100)
|
|
200
|
+
print(res_buffer_size)
|
|
201
|
+
```
|
|
202
|
+
**Result Display:**
|
|
203
|
+
## df table
|
|
204
|
+
|
|
205
|
+
| | lon | lat | buffer_size |
|
|
206
|
+
|---|----------|---------|-------------|
|
|
207
|
+
| 0 | 116.4074 | 39.9042 | 500 |
|
|
208
|
+
| 1 | 121.4737 | 31.2304 | 1000 |
|
|
209
|
+
|
|
210
|
+
## Set a 100-meter buffer zone
|
|
211
|
+
|
|
212
|
+
| | lon | lat | buffer_size | geometry |
|
|
213
|
+
|---|----------|---------|-------------|-------------------------------------------------|
|
|
214
|
+
| 0 | 116.4074 | 39.9042 | 500 | POLYGON ((116.40857 39.90421, 116.40856 39.904... |
|
|
215
|
+
| 1 | 121.4737 | 31.2304 | 1000 | POLYGON ((121.47475 31.23036, 121.47474 31.230... |
|
|
216
|
+
|
|
217
|
+
## Set the buffer range according to the numbers in the "buffer_size" column.
|
|
218
|
+
|
|
219
|
+
| | lon | lat | buffer_size | geometry |
|
|
220
|
+
|---|----------|---------|-------------|-------------------------------------------------|
|
|
221
|
+
| 0 | 116.4074 | 39.9042 | 500 | POLYGON ((116.41325 39.90423, 116.41322 39.903... |
|
|
222
|
+
| 1 | 121.4737 | 31.2304 | 1000 | POLYGON ((121.48417 31.23003, 121.48408 31.229... |
|
|
223
|
+
### 5、Convert the latitude and longitude columns in the table into point-shaped geometries and then transform them into a gdf.
|
|
224
|
+
```python
|
|
225
|
+
import pandas as pd
|
|
226
|
+
import tablegis as tg
|
|
227
|
+
|
|
228
|
+
df = pd.DataFrame({
|
|
229
|
+
'lon': [116.4074, 121.4737, 113.2644],
|
|
230
|
+
'lat': [39.9042, 31.2304, 23.1291],
|
|
231
|
+
'city': ['Beijing', 'Shanghai', 'Guangzhou']
|
|
232
|
+
})
|
|
233
|
+
# Generate points based on latitude and longitude
|
|
234
|
+
result1 = tg.add_points(df)
|
|
235
|
+
print(result1)
|
|
236
|
+
```
|
|
237
|
+
**Result Display:**
|
|
238
|
+
|
|
239
|
+
| lon | lat | city | geometry |
|
|
240
|
+
|-----------|------------|-----------|------------------------------|
|
|
241
|
+
| 116.4074 | 39.9042 | Beijing | POINT (116.4074 39.9042) |
|
|
242
|
+
| 121.4737 | 31.2304 | Shanghai | POINT (121.4737 31.2304) |
|
|
243
|
+
| 113.2644 | 23.1291 | Guangzhou | POINT (113.2644 23.1291) |
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
### 6、Concentrate the latitude and longitude data in the table using the method of expansion and recombination, and add the fused id as well as the range geom.
|
|
247
|
+
```python
|
|
248
|
+
import pandas as pd
|
|
249
|
+
import tablegis as tg
|
|
250
|
+
# Prepare test data
|
|
251
|
+
test_data = pd.DataFrame({
|
|
252
|
+
'lon': [116.40, 116.41, 116.50, 116.51],
|
|
253
|
+
'lat': [39.90, 39.91, 39.95, 39.96],
|
|
254
|
+
'name': ['A', 'B', 'C', 'D'],
|
|
255
|
+
'value': [1, 2, 3, 4]
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
# Test 1: Return geometry
|
|
259
|
+
result_no_geom = tg.add_buffer_groupbyid(
|
|
260
|
+
test_data,
|
|
261
|
+
lon='lon',
|
|
262
|
+
lat='lat',
|
|
263
|
+
distance=1000,
|
|
264
|
+
columns_name='group_id',
|
|
265
|
+
id_label_prefix='id_',
|
|
266
|
+
geom=True
|
|
267
|
+
)
|
|
268
|
+
# Test 2: Do not return geometry
|
|
269
|
+
result_geom = tg.add_buffer_groupbyid(
|
|
270
|
+
test_data,
|
|
271
|
+
lon='lon',
|
|
272
|
+
lat='lat',
|
|
273
|
+
distance=1000,
|
|
274
|
+
columns_name='group_id',
|
|
275
|
+
id_label_prefix='id_',
|
|
276
|
+
geom=False
|
|
277
|
+
)
|
|
278
|
+
```
|
|
279
|
+
**Result Display:**
|
|
280
|
+
## no geom
|
|
281
|
+
| lon | lat | name | value | group_id |
|
|
282
|
+
|-------|-------|------|-------|----------|
|
|
283
|
+
| 116.40| 39.90 | A | 1 | id_0 |
|
|
284
|
+
| 116.41| 39.91 | B | 2 | id_0 |
|
|
285
|
+
| 116.50| 39.95 | C | 3 | id_1 |
|
|
286
|
+
| 116.51| 39.96 | D | 4 | id_1 |
|
|
287
|
+
|
|
288
|
+
## geom
|
|
289
|
+
| lon | lat | name | value | group_id | geometry |
|
|
290
|
+
|-------|-------|------|-------|----------|---------|
|
|
291
|
+
| 116.40| 39.90 | A | 1 | id_0 | POLYGON ((116.41149 39.8983, 116.41122 39.8974...) |
|
|
292
|
+
| 116.41| 39.91 | B | 2 | id_0 | POLYGON ((116.41149 39.8983, 116.41122 39.8974...) |
|
|
293
|
+
| 116.50| 39.95 | C | 3 | id_1 | POLYGON ((116.51149 39.94829, 116.51122 39.947...) |
|
|
294
|
+
| 116.51| 39.96 | D | 4 | id_1 | POLYGON ((116.51149 39.94829, 116.51122 39.947...) |
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
185
299
|
## Contributing
|
|
186
300
|
|
|
187
301
|
Contributions in all forms are welcome, including feature requests, bug reports, and code contributions.
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "tablegis"
|
|
7
|
-
version = "0.0.
|
|
7
|
+
version = "0.0.4"
|
|
8
8
|
keywords = [
|
|
9
9
|
"gis", "tablegeo", "pandas", "geopandas",
|
|
10
10
|
"shapefile", "Nearest", "crs"
|
|
@@ -25,6 +25,8 @@ dependencies = [
|
|
|
25
25
|
"numpy",
|
|
26
26
|
"scipy",
|
|
27
27
|
"pyproj",
|
|
28
|
+
"geopandas",
|
|
29
|
+
"shapely",
|
|
28
30
|
]
|
|
29
31
|
|
|
30
32
|
[project.urls]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .core import (min_distance_onetable,
|
|
2
|
+
min_distance_twotable,
|
|
3
|
+
to_lonlat,
|
|
4
|
+
add_buffer,
|
|
5
|
+
add_points,
|
|
6
|
+
add_buffer_groupbyid)
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"min_distance_onetable",
|
|
10
|
+
"min_distance_twotable",
|
|
11
|
+
"to_lonlat",
|
|
12
|
+
'add_buffer',
|
|
13
|
+
'add_points',
|
|
14
|
+
'add_buffer_groupbyid',
|
|
15
|
+
]
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
# coding: utf-8
|
|
3
3
|
import pandas as pd
|
|
4
4
|
import numpy as np
|
|
5
|
+
import geopandas as gpd
|
|
6
|
+
from shapely.geometry import Point
|
|
5
7
|
from scipy.spatial import cKDTree
|
|
6
8
|
import pyproj
|
|
7
9
|
from .utils import *
|
|
@@ -324,4 +326,230 @@ def to_lonlat(df, lon, lat, from_crs, to_crs):
|
|
|
324
326
|
返回:带新增列的 DataFrame
|
|
325
327
|
抛错:当 from_crs 或 to_crs 非支持集合时抛 ValueError
|
|
326
328
|
"""
|
|
327
|
-
return to_lonlat_utils(df, lon, lat, from_crs, to_crs)
|
|
329
|
+
return to_lonlat_utils(df, lon, lat, from_crs, to_crs)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def add_buffer(df, lon='lon', lat='lat',dis=None, geometry='geometry'):
|
|
333
|
+
"""
|
|
334
|
+
创建精确的以“米”为单位的 buffer,基于正确的 UTM 投影。
|
|
335
|
+
|
|
336
|
+
参数:
|
|
337
|
+
df: DataFrame 含经纬度
|
|
338
|
+
lon, lat: 经纬度列名
|
|
339
|
+
dis: 字符串表示用距离的字段,数字表示固定距离单位(米)
|
|
340
|
+
geometry: 输出几何列名
|
|
341
|
+
|
|
342
|
+
返回:
|
|
343
|
+
GeoDataFrame: 包含精确 buffer 的多边形,CRS=4326
|
|
344
|
+
"""
|
|
345
|
+
df = df.copy()
|
|
346
|
+
# 检查列是否存在
|
|
347
|
+
if lon not in df.columns or lat not in df.columns:
|
|
348
|
+
raise ValueError(f"Missing columns: {lon}, {lat}")
|
|
349
|
+
# 2. 数据验证
|
|
350
|
+
lon_values = df[lon].dropna()
|
|
351
|
+
lat_values = df[lat].dropna()
|
|
352
|
+
|
|
353
|
+
# 检查是否有空值
|
|
354
|
+
if len(lon_values) == 0 or len(lat_values) == 0:
|
|
355
|
+
raise ValueError("经纬度列包含全部空值")
|
|
356
|
+
# 检查经度范围 (-180 到 180)
|
|
357
|
+
lon_min, lon_max = lon_values.min(), lon_values.max()
|
|
358
|
+
lat_min, lat_max = lat_values.min(), lat_values.max()
|
|
359
|
+
|
|
360
|
+
invalid_lon = (lon_min < -180) or (lon_max > 180)
|
|
361
|
+
invalid_lat = (lat_min < -90) or (lat_max > 90)
|
|
362
|
+
|
|
363
|
+
# 3. 处理异常情况
|
|
364
|
+
if invalid_lon or invalid_lat:
|
|
365
|
+
error_msg = f"坐标数据异常:\n"
|
|
366
|
+
error_msg += f" 经度范围: [{lon_min:.4f}, {lon_max:.4f}] (标准: -180 到 180)\n"
|
|
367
|
+
error_msg += f" 纬度范围: [{lat_min:.4f}, {lat_max:.4f}] (标准: -90 到 90)\n"
|
|
368
|
+
raise ValueError(error_msg)
|
|
369
|
+
|
|
370
|
+
# 计算中心点以确定最佳 UTM zone
|
|
371
|
+
center_lon = df[lon].mean()
|
|
372
|
+
center_lat = df[lat].mean()
|
|
373
|
+
# 判断 UTM zone number
|
|
374
|
+
utm_zone = int((center_lon + 180) // 6) + 1
|
|
375
|
+
# 北半球 EPSG: 326XX;南半球 EPSG: 327XX
|
|
376
|
+
hemisphere = 32600 if center_lat >= 0 else 32700
|
|
377
|
+
target_crs = f"EPSG:{hemisphere + utm_zone}"
|
|
378
|
+
print(f"Center: ({center_lon:.4f}, {center_lat:.4f}) → UTM Zone {utm_zone} {'N' if center_lat>=0 else 'S'} → {target_crs}")
|
|
379
|
+
# 创建点并指定原始 CRS
|
|
380
|
+
gdf = gpd.GeoDataFrame(
|
|
381
|
+
df,
|
|
382
|
+
geometry=gpd.points_from_xy(df[lon], df[lat]),
|
|
383
|
+
crs="EPSG:4326"
|
|
384
|
+
)
|
|
385
|
+
# 转到 UTM 进行 buffer(此时单位是米)
|
|
386
|
+
gdf_utm = gdf.to_crs(target_crs)
|
|
387
|
+
if type(dis) == str:
|
|
388
|
+
gdf_utm[geometry] = gdf_utm[[geometry, dis]].apply(lambda x: x.iloc[0].buffer(x.iloc[1]), axis=1)
|
|
389
|
+
elif type(dis) == float or type(dis) == int:
|
|
390
|
+
gdf_utm[geometry] = gdf_utm.geometry.buffer(dis)
|
|
391
|
+
else:
|
|
392
|
+
raise ValueError(f"type Error: {dis}")
|
|
393
|
+
|
|
394
|
+
# 转回 WGS84 便于可视化
|
|
395
|
+
result = gdf_utm.set_geometry(geometry).to_crs("EPSG:4326")
|
|
396
|
+
return result
|
|
397
|
+
|
|
398
|
+
def add_points(df1, lon='lon', lat='lat', geometry='geometry',crs='epsg:4326'):
|
|
399
|
+
"""
|
|
400
|
+
将具有经度和纬度列的 DataFrame 转换为具有 Point 几何图形的 GeoDataFrame。
|
|
401
|
+
|
|
402
|
+
参数
|
|
403
|
+
----------
|
|
404
|
+
df1 : pandas.DataFrame
|
|
405
|
+
lon : str, 填写精度的列名
|
|
406
|
+
lat : str, 填写纬度的列名
|
|
407
|
+
geometry : str, 默认geometry,添加的geometry列名
|
|
408
|
+
crs : str,默认使用4326
|
|
409
|
+
|
|
410
|
+
返回:geopandas.GeoDataFrame
|
|
411
|
+
|
|
412
|
+
报错
|
|
413
|
+
------
|
|
414
|
+
列名错误
|
|
415
|
+
如果指定的经度或纬度列不在数据框中。
|
|
416
|
+
值错误
|
|
417
|
+
如果数据框为空,或者坐标值无效。
|
|
418
|
+
|
|
419
|
+
举例
|
|
420
|
+
--------
|
|
421
|
+
import pandas as pd
|
|
422
|
+
df = pd.DataFrame({
|
|
423
|
+
'lon': [116.4074, 121.4737],
|
|
424
|
+
'lat': [39.9042, 31.2304],
|
|
425
|
+
'city': ['Beijing', 'Shanghai']
|
|
426
|
+
})
|
|
427
|
+
gdf = add_points(df)
|
|
428
|
+
print(type(gdf))
|
|
429
|
+
<class 'geopandas.geodataframe.GeoDataFrame'>
|
|
430
|
+
|
|
431
|
+
# 使用自定义列名
|
|
432
|
+
df2 = pd.DataFrame({
|
|
433
|
+
'longitude': [116.4074],
|
|
434
|
+
'latitude': [39.9042]
|
|
435
|
+
})
|
|
436
|
+
gdf2 = add_points(df2, lon='longitude', lat='latitude')
|
|
437
|
+
|
|
438
|
+
说明
|
|
439
|
+
-----
|
|
440
|
+
- 坐标值应为有效的经度(-180 至 180)和纬度(-90 至 90)。
|
|
441
|
+
"""
|
|
442
|
+
# 验证输入
|
|
443
|
+
if df1 is None or df1.empty:
|
|
444
|
+
raise ValueError("Input DataFrame is empty or None")
|
|
445
|
+
|
|
446
|
+
if lon not in df1.columns:
|
|
447
|
+
raise KeyError(f"Longitude column '{lon}' not found in DataFrame")
|
|
448
|
+
|
|
449
|
+
if lat not in df1.columns:
|
|
450
|
+
raise KeyError(f"Latitude column '{lat}' not found in DataFrame")
|
|
451
|
+
|
|
452
|
+
# 创建一个副本以避免修改原始的数据框
|
|
453
|
+
df = df1.copy()
|
|
454
|
+
|
|
455
|
+
# 创建点几何体
|
|
456
|
+
df[geometry] = [Point(x, y) for x, y in zip(df[lon], df[lat])]
|
|
457
|
+
|
|
458
|
+
# 创建地理数据框
|
|
459
|
+
df_p = gpd.GeoDataFrame(df, crs="epsg:4326", geometry=geometry)
|
|
460
|
+
|
|
461
|
+
return df_p
|
|
462
|
+
|
|
463
|
+
def add_buffer_groupbyid(df, lon='lon', lat='lat', distance=50,
|
|
464
|
+
columns_name='聚合id', id_label_prefix='聚合_', geom=False):
|
|
465
|
+
"""
|
|
466
|
+
按照给定的距离将点位聚合在一起,添加聚合ID列用于标识。
|
|
467
|
+
该函数通过创建缓冲区、融合重叠区域、然后将原始点位与聚合区域关联,
|
|
468
|
+
实现点位的空间聚类。
|
|
469
|
+
|
|
470
|
+
参数
|
|
471
|
+
----------
|
|
472
|
+
df : pd.DataFrame
|
|
473
|
+
包含经纬度信息的DataFrame
|
|
474
|
+
lon : str, optional
|
|
475
|
+
经度字段名,默认为 'lon'
|
|
476
|
+
lat : str, optional
|
|
477
|
+
纬度字段名,默认为 'lat'
|
|
478
|
+
distance : float, optional
|
|
479
|
+
聚合的缓冲距离(单位取决于坐标系统),默认为 50
|
|
480
|
+
columns_name : str, optional
|
|
481
|
+
添加的聚合ID列名,默认为 '聚合id'
|
|
482
|
+
id_label_prefix : str, optional
|
|
483
|
+
聚合ID的前缀,默认为 '聚合_'
|
|
484
|
+
例如:'聚合_' 会生成 '聚合_0', '聚合_1' 等
|
|
485
|
+
geom : bool, optional
|
|
486
|
+
是否返回包含geometry列的GeoDataFrame,默认为 False
|
|
487
|
+
当为 True 时,geometry列包含聚合后的多边形区域
|
|
488
|
+
当为 False 时,不包含geometry列
|
|
489
|
+
|
|
490
|
+
Returns
|
|
491
|
+
-------
|
|
492
|
+
pd.DataFrame or gpd.GeoDataFrame
|
|
493
|
+
添加了聚合ID列的数据框
|
|
494
|
+
如果 geom=True,返回GeoDataFrame且geometry列为聚合多边形
|
|
495
|
+
如果 geom=False,返回DataFrame且不包含geometry列
|
|
496
|
+
|
|
497
|
+
举例
|
|
498
|
+
--------
|
|
499
|
+
import pandas as pd
|
|
500
|
+
data = pd.DataFrame({
|
|
501
|
+
'lon': [116.40, 116.41, 116.50],
|
|
502
|
+
'lat': [39.90, 39.91, 39.95],
|
|
503
|
+
'name': ['A', 'B', 'C']
|
|
504
|
+
})
|
|
505
|
+
|
|
506
|
+
# 不返回几何信息
|
|
507
|
+
result = add_buffer_groupbyid(data, distance=1000)
|
|
508
|
+
|
|
509
|
+
# 返回聚合多边形几何信息
|
|
510
|
+
result_with_geom = add_buffer_groupbyid(data, distance=1000, geom=True)
|
|
511
|
+
"""
|
|
512
|
+
|
|
513
|
+
# 参数验证
|
|
514
|
+
if lon not in df.columns or lat not in df.columns:
|
|
515
|
+
raise ValueError(f"Columns '{lon}' and '{lat}' must exist in dataframe")
|
|
516
|
+
|
|
517
|
+
# 创建缓冲区
|
|
518
|
+
data_buffer = add_buffer(df, lon, lat, distance)
|
|
519
|
+
|
|
520
|
+
# 融合重叠的缓冲区
|
|
521
|
+
data_dissolve = data_buffer[['geometry']].dissolve()
|
|
522
|
+
|
|
523
|
+
# 分解多部件几何为单个几何
|
|
524
|
+
data_explode = data_dissolve.explode(index_parts=False).reset_index(drop=True)[['geometry']]
|
|
525
|
+
|
|
526
|
+
# 添加聚合ID
|
|
527
|
+
data_explode[columns_name] = id_label_prefix + data_explode.index.astype(str)
|
|
528
|
+
|
|
529
|
+
# 创建点几何
|
|
530
|
+
data_points = add_points(df, lon, lat)
|
|
531
|
+
|
|
532
|
+
# 空间连接:将点与聚合区域关联
|
|
533
|
+
data_sjoin = gpd.sjoin(data_points, data_explode, how='left', predicate='intersects')
|
|
534
|
+
|
|
535
|
+
if geom:
|
|
536
|
+
# 保留geometry列,但使用聚合多边形替换点几何
|
|
537
|
+
# 先获取原始数据列和聚合ID
|
|
538
|
+
data_columns = list(df.columns) + [columns_name]
|
|
539
|
+
result = data_sjoin[data_columns].copy()
|
|
540
|
+
|
|
541
|
+
# 通过聚合ID关联回多边形geometry
|
|
542
|
+
result = result.merge(
|
|
543
|
+
data_explode[[columns_name, 'geometry']],
|
|
544
|
+
on=columns_name,
|
|
545
|
+
how='left'
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
# 转换为GeoDataFrame
|
|
549
|
+
result = gpd.GeoDataFrame(result, geometry='geometry', crs=data_explode.crs)
|
|
550
|
+
else:
|
|
551
|
+
# 不保留geometry列
|
|
552
|
+
data_columns = list(df.columns) + [columns_name]
|
|
553
|
+
result = data_sjoin[data_columns].copy()
|
|
554
|
+
|
|
555
|
+
return result
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tablegis
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: A powerful Python library for geospatial data processing, analysis and conversion - integrating pandas with GIS operations
|
|
5
5
|
Author-email: Non-existent987 <jiaqi0425_000@163.com>
|
|
6
6
|
License: MIT License
|
|
@@ -40,6 +40,8 @@ Requires-Dist: pandas
|
|
|
40
40
|
Requires-Dist: numpy
|
|
41
41
|
Requires-Dist: scipy
|
|
42
42
|
Requires-Dist: pyproj
|
|
43
|
+
Requires-Dist: geopandas
|
|
44
|
+
Requires-Dist: shapely
|
|
43
45
|
Dynamic: license-file
|
|
44
46
|
|
|
45
47
|
# tablegis
|
|
@@ -226,6 +228,120 @@ print(result)
|
|
|
226
228
|
| D | 116.408 | 39.919 | 12958479.28 | 4854178.552 |
|
|
227
229
|
|
|
228
230
|
|
|
231
|
+
### 4、Generate buffers of the specified range based on the longitude and latitude columns in the table and add the geometry.
|
|
232
|
+
```python
|
|
233
|
+
import pandas as pd
|
|
234
|
+
import tablegis as tg
|
|
235
|
+
|
|
236
|
+
df = pd.DataFrame({
|
|
237
|
+
'lon': [116.4074, 121.4737],
|
|
238
|
+
'lat': [39.9042, 31.2304],
|
|
239
|
+
'buffer_size': [500, 1000]
|
|
240
|
+
})
|
|
241
|
+
# Set a 100-meter buffer zone
|
|
242
|
+
res_100 = tg.add_buffer(df,'lon','lat',100)
|
|
243
|
+
# Set the buffer range according to the numbers in the "buffer_size" column.
|
|
244
|
+
res_buffer_size = tg.add_buffer(df,'lon','lat','buffer_size')
|
|
245
|
+
print(res_100)
|
|
246
|
+
print(res_buffer_size)
|
|
247
|
+
```
|
|
248
|
+
**Result Display:**
|
|
249
|
+
## df table
|
|
250
|
+
|
|
251
|
+
| | lon | lat | buffer_size |
|
|
252
|
+
|---|----------|---------|-------------|
|
|
253
|
+
| 0 | 116.4074 | 39.9042 | 500 |
|
|
254
|
+
| 1 | 121.4737 | 31.2304 | 1000 |
|
|
255
|
+
|
|
256
|
+
## Set a 100-meter buffer zone
|
|
257
|
+
|
|
258
|
+
| | lon | lat | buffer_size | geometry |
|
|
259
|
+
|---|----------|---------|-------------|-------------------------------------------------|
|
|
260
|
+
| 0 | 116.4074 | 39.9042 | 500 | POLYGON ((116.40857 39.90421, 116.40856 39.904... |
|
|
261
|
+
| 1 | 121.4737 | 31.2304 | 1000 | POLYGON ((121.47475 31.23036, 121.47474 31.230... |
|
|
262
|
+
|
|
263
|
+
## Set the buffer range according to the numbers in the "buffer_size" column.
|
|
264
|
+
|
|
265
|
+
| | lon | lat | buffer_size | geometry |
|
|
266
|
+
|---|----------|---------|-------------|-------------------------------------------------|
|
|
267
|
+
| 0 | 116.4074 | 39.9042 | 500 | POLYGON ((116.41325 39.90423, 116.41322 39.903... |
|
|
268
|
+
| 1 | 121.4737 | 31.2304 | 1000 | POLYGON ((121.48417 31.23003, 121.48408 31.229... |
|
|
269
|
+
### 5、Convert the latitude and longitude columns in the table into point-shaped geometries and then transform them into a gdf.
|
|
270
|
+
```python
|
|
271
|
+
import pandas as pd
|
|
272
|
+
import tablegis as tg
|
|
273
|
+
|
|
274
|
+
df = pd.DataFrame({
|
|
275
|
+
'lon': [116.4074, 121.4737, 113.2644],
|
|
276
|
+
'lat': [39.9042, 31.2304, 23.1291],
|
|
277
|
+
'city': ['Beijing', 'Shanghai', 'Guangzhou']
|
|
278
|
+
})
|
|
279
|
+
# Generate points based on latitude and longitude
|
|
280
|
+
result1 = tg.add_points(df)
|
|
281
|
+
print(result1)
|
|
282
|
+
```
|
|
283
|
+
**Result Display:**
|
|
284
|
+
|
|
285
|
+
| lon | lat | city | geometry |
|
|
286
|
+
|-----------|------------|-----------|------------------------------|
|
|
287
|
+
| 116.4074 | 39.9042 | Beijing | POINT (116.4074 39.9042) |
|
|
288
|
+
| 121.4737 | 31.2304 | Shanghai | POINT (121.4737 31.2304) |
|
|
289
|
+
| 113.2644 | 23.1291 | Guangzhou | POINT (113.2644 23.1291) |
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
### 6、Concentrate the latitude and longitude data in the table using the method of expansion and recombination, and add the fused id as well as the range geom.
|
|
293
|
+
```python
|
|
294
|
+
import pandas as pd
|
|
295
|
+
import tablegis as tg
|
|
296
|
+
# Prepare test data
|
|
297
|
+
test_data = pd.DataFrame({
|
|
298
|
+
'lon': [116.40, 116.41, 116.50, 116.51],
|
|
299
|
+
'lat': [39.90, 39.91, 39.95, 39.96],
|
|
300
|
+
'name': ['A', 'B', 'C', 'D'],
|
|
301
|
+
'value': [1, 2, 3, 4]
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
# Test 1: Return geometry
|
|
305
|
+
result_no_geom = tg.add_buffer_groupbyid(
|
|
306
|
+
test_data,
|
|
307
|
+
lon='lon',
|
|
308
|
+
lat='lat',
|
|
309
|
+
distance=1000,
|
|
310
|
+
columns_name='group_id',
|
|
311
|
+
id_label_prefix='id_',
|
|
312
|
+
geom=True
|
|
313
|
+
)
|
|
314
|
+
# Test 2: Do not return geometry
|
|
315
|
+
result_geom = tg.add_buffer_groupbyid(
|
|
316
|
+
test_data,
|
|
317
|
+
lon='lon',
|
|
318
|
+
lat='lat',
|
|
319
|
+
distance=1000,
|
|
320
|
+
columns_name='group_id',
|
|
321
|
+
id_label_prefix='id_',
|
|
322
|
+
geom=False
|
|
323
|
+
)
|
|
324
|
+
```
|
|
325
|
+
**Result Display:**
|
|
326
|
+
## no geom
|
|
327
|
+
| lon | lat | name | value | group_id |
|
|
328
|
+
|-------|-------|------|-------|----------|
|
|
329
|
+
| 116.40| 39.90 | A | 1 | id_0 |
|
|
330
|
+
| 116.41| 39.91 | B | 2 | id_0 |
|
|
331
|
+
| 116.50| 39.95 | C | 3 | id_1 |
|
|
332
|
+
| 116.51| 39.96 | D | 4 | id_1 |
|
|
333
|
+
|
|
334
|
+
## geom
|
|
335
|
+
| lon | lat | name | value | group_id | geometry |
|
|
336
|
+
|-------|-------|------|-------|----------|---------|
|
|
337
|
+
| 116.40| 39.90 | A | 1 | id_0 | POLYGON ((116.41149 39.8983, 116.41122 39.8974...) |
|
|
338
|
+
| 116.41| 39.91 | B | 2 | id_0 | POLYGON ((116.41149 39.8983, 116.41122 39.8974...) |
|
|
339
|
+
| 116.50| 39.95 | C | 3 | id_1 | POLYGON ((116.51149 39.94829, 116.51122 39.947...) |
|
|
340
|
+
| 116.51| 39.96 | D | 4 | id_1 | POLYGON ((116.51149 39.94829, 116.51122 39.947...) |
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
229
345
|
## Contributing
|
|
230
346
|
|
|
231
347
|
Contributions in all forms are welcome, including feature requests, bug reports, and code contributions.
|
|
@@ -2,7 +2,10 @@ import tablegis as tg
|
|
|
2
2
|
import pytest
|
|
3
3
|
import pandas as pd
|
|
4
4
|
import numpy as np
|
|
5
|
-
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import geopandas as gpd
|
|
7
|
+
from shapely.geometry import Point, Polygon
|
|
8
|
+
from tablegis.utils import wgs84_to_gcj02, gcj02_to_wgs84, wgs84_to_bd09, bd09_to_wgs84, transform
|
|
6
9
|
|
|
7
10
|
def test_min_distance_onetable():
|
|
8
11
|
"""测试 min_distance_onetable 函数"""
|
|
@@ -207,6 +210,183 @@ def test_unknown_crs_raises():
|
|
|
207
210
|
tg.to_lonlat(df, "lon", "lat", from_crs="wgs84", to_crs="unknown")
|
|
208
211
|
print("✓ test_unknown_crs_raises 所有测试通过!")
|
|
209
212
|
|
|
213
|
+
def test_add_buffer():
|
|
214
|
+
"""测试 add_buffer 函数的基本功能"""
|
|
215
|
+
import pandas as pd
|
|
216
|
+
import geopandas as gpd
|
|
217
|
+
|
|
218
|
+
# 测试1: 基本固定距离
|
|
219
|
+
df = pd.DataFrame({
|
|
220
|
+
'lon': [116.4074, 121.4737],
|
|
221
|
+
'lat': [39.9042, 31.2304],
|
|
222
|
+
'name': ['北京', '上海']
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
result = tg.add_buffer(df, lon='lon', lat='lat', dis=1000)
|
|
226
|
+
|
|
227
|
+
assert isinstance(result, gpd.GeoDataFrame), "返回类型应该是 GeoDataFrame"
|
|
228
|
+
assert result.crs.to_string() == "EPSG:4326", "CRS 应该是 EPSG:4326"
|
|
229
|
+
assert len(result) == 2, "应该返回2行数据"
|
|
230
|
+
assert all(result.geometry.geom_type == 'Polygon'), "几何类型应该是 Polygon"
|
|
231
|
+
assert 'name' in result.columns, "原始列应该保留"
|
|
232
|
+
|
|
233
|
+
# 测试2: 使用列名指定距离
|
|
234
|
+
df2 = pd.DataFrame({
|
|
235
|
+
'lon': [116.4074, 121.4737],
|
|
236
|
+
'lat': [39.9042, 31.2304],
|
|
237
|
+
'buffer_size': [500, 1000]
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
result2 = tg.add_buffer(df2, lon='lon', lat='lat', dis='buffer_size')
|
|
241
|
+
assert len(result2) == 2, "应该返回2行数据"
|
|
242
|
+
|
|
243
|
+
# 测试3: 错误处理 - 缺失列
|
|
244
|
+
df3 = pd.DataFrame({'x': [116.4074], 'y': [39.9042]})
|
|
245
|
+
try:
|
|
246
|
+
tg.add_buffer(df3, lon='lon', lat='lat', dis=1000)
|
|
247
|
+
assert False, "应该抛出 ValueError"
|
|
248
|
+
except ValueError as e:
|
|
249
|
+
assert "Missing columns" in str(e)
|
|
250
|
+
|
|
251
|
+
# 测试4: 错误处理 - 无效经度
|
|
252
|
+
df4 = pd.DataFrame({'lon': [200.0], 'lat': [39.9042]})
|
|
253
|
+
try:
|
|
254
|
+
tg.add_buffer(df4, dis=1000)
|
|
255
|
+
assert False, "应该抛出 ValueError"
|
|
256
|
+
except ValueError as e:
|
|
257
|
+
assert "坐标数据异常" in str(e)
|
|
258
|
+
|
|
259
|
+
print("✓ test_add_buffer 所有测试通过!")
|
|
260
|
+
|
|
261
|
+
def test_add_points():
|
|
262
|
+
"""
|
|
263
|
+
“添加点数”测试函数。
|
|
264
|
+
测试基本功能、自定义列名以及错误处理机制。
|
|
265
|
+
"""
|
|
266
|
+
|
|
267
|
+
# 测试 1:使用默认列名的基本功能
|
|
268
|
+
df1 = pd.DataFrame({
|
|
269
|
+
'lon': [116.4074, 121.4737, 113.2644],
|
|
270
|
+
'lat': [39.9042, 31.2304, 23.1291],
|
|
271
|
+
'city': ['Beijing', 'Shanghai', 'Guangzhou']
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
result1 = tg.add_points(df1)
|
|
275
|
+
assert isinstance(result1, gpd.GeoDataFrame), "Result should be a GeoDataFrame"
|
|
276
|
+
assert 'geometry' in result1.columns, "Geometry column should exist"
|
|
277
|
+
assert len(result1) == 3, "Should have 3 rows"
|
|
278
|
+
assert result1.crs.to_string() == "EPSG:4326", "CRS should be EPSG:4326"
|
|
279
|
+
assert all(isinstance(geom, Point) for geom in result1.geometry), "All geometries should be Points"
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
# 测试 2:自定义列名
|
|
283
|
+
df2 = pd.DataFrame({
|
|
284
|
+
'longitude': [0.0, 10.0],
|
|
285
|
+
'latitude': [0.0, 20.0],
|
|
286
|
+
'name': ['Point A', 'Point B']
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
result2 = tg.add_points(df2, lon='longitude', lat='latitude', geometry='geom')
|
|
290
|
+
assert 'geom' in result2.columns, "Custom geometry column should exist"
|
|
291
|
+
assert result2.geometry.name == 'geom', "Geometry column name should be 'geom'"
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
# 测试 3:验证原始数据框未被修改
|
|
295
|
+
df3 = pd.DataFrame({'lon': [1.0], 'lat': [2.0]})
|
|
296
|
+
original_columns = df3.columns.tolist()
|
|
297
|
+
tg.add_points(df3)
|
|
298
|
+
assert df3.columns.tolist() == original_columns, "Original DataFrame should not be modified"
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
# 测试 4:错误处理 - 缺失列
|
|
302
|
+
df4 = pd.DataFrame({'x': [1.0], 'y': [2.0]})
|
|
303
|
+
try:
|
|
304
|
+
tg.add_points(df4)
|
|
305
|
+
assert False, "Should raise KeyError for missing columns"
|
|
306
|
+
except KeyError:
|
|
307
|
+
pass
|
|
308
|
+
|
|
309
|
+
# 测试 5:错误处理 - 空数据框
|
|
310
|
+
df5 = pd.DataFrame({'lon': [], 'lat': []})
|
|
311
|
+
try:
|
|
312
|
+
tg.add_points(df5)
|
|
313
|
+
assert False, "Should raise ValueError for empty DataFrame"
|
|
314
|
+
except ValueError:
|
|
315
|
+
pass
|
|
316
|
+
|
|
317
|
+
# 测试 6:验证坐标值
|
|
318
|
+
df6 = pd.DataFrame({'lon': [100.5], 'lat': [50.5]})
|
|
319
|
+
result6 = tg.add_points(df6)
|
|
320
|
+
point = result6.geometry.iloc[0]
|
|
321
|
+
assert point.x == 100.5, "Longitude value should match"
|
|
322
|
+
assert point.y == 50.5, "Latitude value should match"
|
|
323
|
+
print("✓ test_add_points 所有测试通过!")
|
|
324
|
+
|
|
325
|
+
def test_add_buffer_groupbyid():
|
|
326
|
+
"""
|
|
327
|
+
测试 add_buffer_groupbyid 函数的功能
|
|
328
|
+
"""
|
|
329
|
+
|
|
330
|
+
# 准备测试数据
|
|
331
|
+
test_data = pd.DataFrame({
|
|
332
|
+
'lon': [116.40, 116.41, 116.50, 116.51],
|
|
333
|
+
'lat': [39.90, 39.91, 39.95, 39.96],
|
|
334
|
+
'name': ['A', 'B', 'C', 'D'],
|
|
335
|
+
'value': [1, 2, 3, 4]
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
# 测试1: 不返回geometry
|
|
339
|
+
result_no_geom = tg.add_buffer_groupbyid(
|
|
340
|
+
test_data,
|
|
341
|
+
lon='lon',
|
|
342
|
+
lat='lat',
|
|
343
|
+
distance=1000,
|
|
344
|
+
geom=False
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
assert isinstance(result_no_geom, pd.DataFrame), "应该返回DataFrame"
|
|
348
|
+
assert 'geometry' not in result_no_geom.columns, "不应包含geometry列"
|
|
349
|
+
assert '聚合id' in result_no_geom.columns, "应该包含聚合id列"
|
|
350
|
+
assert all(col in result_no_geom.columns for col in test_data.columns), "应保留原始列"
|
|
351
|
+
|
|
352
|
+
# 测试2: 返回geometry(多边形)
|
|
353
|
+
result_with_geom = tg.add_buffer_groupbyid(
|
|
354
|
+
test_data,
|
|
355
|
+
lon='lon',
|
|
356
|
+
lat='lat',
|
|
357
|
+
distance=1000,
|
|
358
|
+
geom=True
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
assert isinstance(result_with_geom, gpd.GeoDataFrame), "应该返回GeoDataFrame"
|
|
362
|
+
assert 'geometry' in result_with_geom.columns, "应该包含geometry列"
|
|
363
|
+
assert '聚合id' in result_with_geom.columns, "应该包含聚合id列"
|
|
364
|
+
|
|
365
|
+
# 验证geometry是多边形而不是点
|
|
366
|
+
for geom in result_with_geom.geometry.dropna():
|
|
367
|
+
assert isinstance(geom, Polygon), f"geometry应该是Polygon类型,但是 {type(geom)}"
|
|
368
|
+
|
|
369
|
+
# 测试3: 自定义列名和前缀
|
|
370
|
+
result_custom = tg.add_buffer_groupbyid(
|
|
371
|
+
test_data,
|
|
372
|
+
columns_name='cluster_id',
|
|
373
|
+
id_label_prefix='C_',
|
|
374
|
+
geom=False
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
assert 'cluster_id' in result_custom.columns, "应该使用自定义列名"
|
|
378
|
+
assert result_custom['cluster_id'].iloc[0].startswith('C_'), "应该使用自定义前缀"
|
|
379
|
+
|
|
380
|
+
# 测试4: 验证点与多边形的对应关系
|
|
381
|
+
result_check = tg.add_buffer_groupbyid(test_data, distance=1000, geom=True)
|
|
382
|
+
|
|
383
|
+
# 每个点应该在其对应的聚合多边形内
|
|
384
|
+
for idx, row in result_check.iterrows():
|
|
385
|
+
if pd.notna(row['聚合id']) and pd.notna(row.geometry):
|
|
386
|
+
point = Point(row['lon'], row['lat'])
|
|
387
|
+
assert row.geometry.contains(point) or row.geometry.touches(point), \
|
|
388
|
+
f"点 {row['name']} 应该在其聚合多边形内"
|
|
389
|
+
print("✓ test_add_buffer_groupbyid 所有测试通过!")
|
|
210
390
|
|
|
211
391
|
if __name__ == "__main__":
|
|
212
392
|
test_min_distance_onetable()
|
|
@@ -214,4 +394,7 @@ if __name__ == "__main__":
|
|
|
214
394
|
test_wgs84_to_gcj02_and_nan()
|
|
215
395
|
test_bd09_to_wgs84_roundtrip()
|
|
216
396
|
test_webmercator_and_back()
|
|
217
|
-
test_unknown_crs_raises()
|
|
397
|
+
test_unknown_crs_raises()
|
|
398
|
+
test_add_buffer()
|
|
399
|
+
test_add_points()
|
|
400
|
+
test_add_buffer_groupbyid()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|