terraproof 0.0.0-stage → 0.4.0
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.
- package/LICENSE +21 -0
- package/README.md +356 -2
- package/bin/terraproof.js +4 -0
- package/dist/router/apply.js +147 -0
- package/dist/router/classify.js +111 -0
- package/dist/router/explain.js +9 -0
- package/dist/router/index.js +272 -0
- package/dist/router/resolve.js +239 -0
- package/dist/router/score.js +60 -0
- package/dist/router/types.js +1 -0
- package/dist/router/validate.js +81 -0
- package/instructions/engineering.md +14 -0
- package/instructions/security.md +14 -0
- package/instructions/simplicity.md +13 -0
- package/package.json +65 -4
- package/routing/DOMAIN_ROUTING.md +161 -0
- package/routing/ROUTER_DESIGN.md +173 -0
- package/routing/SKILL_MANIFEST.md +86 -0
- package/routing/checks.json +121 -0
- package/routing/collections.json +47 -0
- package/routing/registry.json +2211 -0
- package/routing/rules.json +64 -0
- package/skills/accuracy-assessment/SKILL.md +213 -0
- package/skills/accuracy-assessment/UPSTREAM.md +7 -0
- package/skills/accuracy-assessment/references/stratified-estimator.md +127 -0
- package/skills/accuracy-assessment/skill.json +103 -0
- package/skills/brainstorming/SKILL.md +250 -0
- package/skills/brainstorming/UPSTREAM.md +29 -0
- package/skills/brainstorming/scripts/frame-template.html +213 -0
- package/skills/brainstorming/scripts/helper.js +167 -0
- package/skills/brainstorming/scripts/server.cjs +723 -0
- package/skills/brainstorming/scripts/start-server.sh +209 -0
- package/skills/brainstorming/scripts/stop-server.sh +120 -0
- package/skills/brainstorming/skill.json +73 -0
- package/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
- package/skills/brainstorming/visual-companion.md +299 -0
- package/skills/crs-and-units/SKILL.md +200 -0
- package/skills/crs-and-units/UPSTREAM.md +7 -0
- package/skills/crs-and-units/skill.json +74 -0
- package/skills/design-postgis-tables/SKILL.md +494 -0
- package/skills/design-postgis-tables/UPSTREAM.md +21 -0
- package/skills/design-postgis-tables/skill.json +90 -0
- package/skills/gdal/SKILL.md +125 -0
- package/skills/gdal/UPSTREAM.md +22 -0
- package/skills/gdal/references/gdal-recipes.md +103 -0
- package/skills/gdal/skill.json +66 -0
- package/skills/geomaster/README.md +105 -0
- package/skills/geomaster/SKILL.md +366 -0
- package/skills/geomaster/UPSTREAM.md +21 -0
- package/skills/geomaster/references/advanced-gis.md +376 -0
- package/skills/geomaster/references/big-data.md +363 -0
- package/skills/geomaster/references/code-examples.md +531 -0
- package/skills/geomaster/references/coordinate-systems.md +364 -0
- package/skills/geomaster/references/core-libraries.md +273 -0
- package/skills/geomaster/references/data-sources.md +330 -0
- package/skills/geomaster/references/gis-software.md +369 -0
- package/skills/geomaster/references/industry-applications.md +420 -0
- package/skills/geomaster/references/machine-learning.md +462 -0
- package/skills/geomaster/references/programming-languages.md +456 -0
- package/skills/geomaster/references/remote-sensing.md +370 -0
- package/skills/geomaster/references/scientific-domains.md +416 -0
- package/skills/geomaster/references/specialized-topics.md +428 -0
- package/skills/geomaster/references/troubleshooting.md +439 -0
- package/skills/geomaster/skill.json +157 -0
- package/skills/geopandas/SKILL.md +250 -0
- package/skills/geopandas/UPSTREAM.md +22 -0
- package/skills/geopandas/references/crs-management.md +231 -0
- package/skills/geopandas/references/data-io.md +323 -0
- package/skills/geopandas/references/data-structures.md +207 -0
- package/skills/geopandas/references/geometric-operations.md +262 -0
- package/skills/geopandas/references/spatial-analysis.md +294 -0
- package/skills/geopandas/references/visualization.md +230 -0
- package/skills/geopandas/scripts/_common.py +605 -0
- package/skills/geopandas/scripts/crs_reprojection_plan.py +210 -0
- package/skills/geopandas/scripts/export_plan.py +305 -0
- package/skills/geopandas/scripts/geometry_validity_report.py +227 -0
- package/skills/geopandas/scripts/sensitive_coordinates_checklist.py +230 -0
- package/skills/geopandas/scripts/spatial_join_audit.py +368 -0
- package/skills/geopandas/scripts/vector_inventory.py +140 -0
- package/skills/geopandas/skill.json +116 -0
- package/skills/geoparquet-validation/SKILL.md +125 -0
- package/skills/geoparquet-validation/UPSTREAM.md +24 -0
- package/skills/geoparquet-validation/references/distribution-best-practices.md +118 -0
- package/skills/geoparquet-validation/references/gpio-commands.md +264 -0
- package/skills/geoparquet-validation/references/tool-comparison.md +155 -0
- package/skills/geoparquet-validation/skill.json +71 -0
- package/skills/geospatial-data-discovery/SKILL.md +49 -0
- package/skills/geospatial-data-discovery/UPSTREAM.md +15 -0
- package/skills/geospatial-data-discovery/skill.json +51 -0
- package/skills/geospatial-frontend/SKILL.md +86 -0
- package/skills/geospatial-frontend/UPSTREAM.md +23 -0
- package/skills/geospatial-frontend/references/design-system.md +283 -0
- package/skills/geospatial-frontend/references/user-preferences.md +96 -0
- package/skills/geospatial-frontend/skill.json +87 -0
- package/skills/geozarr/SKILL.md +244 -0
- package/skills/geozarr/UPSTREAM.md +21 -0
- package/skills/geozarr/skill.json +97 -0
- package/skills/networkx/SKILL.md +440 -0
- package/skills/networkx/UPSTREAM.md +22 -0
- package/skills/networkx/references/algorithms.md +384 -0
- package/skills/networkx/references/generators.md +385 -0
- package/skills/networkx/references/graph-basics.md +284 -0
- package/skills/networkx/references/io.md +457 -0
- package/skills/networkx/references/visualization.md +531 -0
- package/skills/networkx/skill.json +93 -0
- package/skills/raster-processing/SKILL.md +222 -0
- package/skills/raster-processing/UPSTREAM.md +7 -0
- package/skills/raster-processing/skill.json +81 -0
- package/skills/sar-basics/SKILL.md +202 -0
- package/skills/sar-basics/UPSTREAM.md +7 -0
- package/skills/sar-basics/references/calibration-lut.md +98 -0
- package/skills/sar-basics/skill.json +97 -0
- package/skills/satellite-preprocessing/SKILL.md +241 -0
- package/skills/satellite-preprocessing/UPSTREAM.md +7 -0
- package/skills/satellite-preprocessing/skill.json +84 -0
- package/skills/scientific-visualization/SKILL.md +285 -0
- package/skills/scientific-visualization/UPSTREAM.md +25 -0
- package/skills/scientific-visualization/assets/color_palettes.py +263 -0
- package/skills/scientific-visualization/assets/nature.mplstyle +68 -0
- package/skills/scientific-visualization/assets/presentation.mplstyle +68 -0
- package/skills/scientific-visualization/assets/publication.mplstyle +77 -0
- package/skills/scientific-visualization/assets/publisher_profiles.json +269 -0
- package/skills/scientific-visualization/references/color_palettes.md +227 -0
- package/skills/scientific-visualization/references/journal_requirements.md +169 -0
- package/skills/scientific-visualization/references/matplotlib_examples.md +336 -0
- package/skills/scientific-visualization/references/publication_guidelines.md +196 -0
- package/skills/scientific-visualization/references/sources.md +76 -0
- package/skills/scientific-visualization/scripts/_common.py +136 -0
- package/skills/scientific-visualization/scripts/export_plan.py +493 -0
- package/skills/scientific-visualization/scripts/figure_export.py +642 -0
- package/skills/scientific-visualization/scripts/image_metadata.py +731 -0
- package/skills/scientific-visualization/scripts/palette_audit.py +327 -0
- package/skills/scientific-visualization/scripts/style_presets.py +501 -0
- package/skills/scientific-visualization/scripts/style_preview.py +232 -0
- package/skills/scientific-visualization/skill.json +95 -0
- package/skills/spectral-indices/SKILL.md +235 -0
- package/skills/spectral-indices/UPSTREAM.md +7 -0
- package/skills/spectral-indices/skill.json +88 -0
- package/skills/stac-and-cloud-access/SKILL.md +209 -0
- package/skills/stac-and-cloud-access/UPSTREAM.md +7 -0
- package/skills/stac-and-cloud-access/references/run-manifest.md +94 -0
- package/skills/stac-and-cloud-access/skill.json +98 -0
- package/skills/statistical-analysis/SKILL.md +446 -0
- package/skills/statistical-analysis/UPSTREAM.md +24 -0
- package/skills/statistical-analysis/references/assumptions_and_diagnostics.md +379 -0
- package/skills/statistical-analysis/references/bayesian_statistics.md +686 -0
- package/skills/statistical-analysis/references/effect_sizes_and_power.md +649 -0
- package/skills/statistical-analysis/references/reporting_standards.md +482 -0
- package/skills/statistical-analysis/references/test_selection_guide.md +129 -0
- package/skills/statistical-analysis/scripts/assumption_checks.py +652 -0
- package/skills/statistical-analysis/skill.json +127 -0
- package/skills/systematic-debugging/SKILL.md +283 -0
- package/skills/systematic-debugging/UPSTREAM.md +28 -0
- package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
- package/skills/systematic-debugging/condition-based-waiting.md +115 -0
- package/skills/systematic-debugging/defense-in-depth.md +122 -0
- package/skills/systematic-debugging/find-polluter.sh +72 -0
- package/skills/systematic-debugging/root-cause-tracing.md +169 -0
- package/skills/systematic-debugging/skill.json +88 -0
- package/skills/time-series-and-change/SKILL.md +217 -0
- package/skills/time-series-and-change/UPSTREAM.md +7 -0
- package/skills/time-series-and-change/references/change-methods.md +42 -0
- package/skills/time-series-and-change/skill.json +116 -0
- package/skills/vector-quality/SKILL.md +204 -0
- package/skills/vector-quality/UPSTREAM.md +7 -0
- package/skills/vector-quality/skill.json +82 -0
- package/skills/verification/SKILL.md +120 -0
- package/skills/verification/UPSTREAM.md +31 -0
- package/skills/verification/skill.json +67 -0
- package/skills/writing-plans/SKILL.md +171 -0
- package/skills/writing-plans/UPSTREAM.md +23 -0
- package/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
- package/skills/writing-plans/skill.json +80 -0
- package/sources/SOURCES.md +374 -0
- package/third_party/isaaccorley-geospatial-skills/LICENSE +176 -0
- package/third_party/isaaccorley-geospatial-skills/NOTICE.md +17 -0
- package/third_party/kdense-scientific-agent-skills/LICENSE +21 -0
- package/third_party/kdense-scientific-agent-skills/NOTICE.md +19 -0
- package/third_party/superpowers/LICENSE +21 -0
- package/third_party/superpowers/NOTICE.md +17 -0
- package/third_party/timescale-pg-aiguide/LICENSE +176 -0
- package/third_party/timescale-pg-aiguide/NOTICE.md +14 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# Geometric operations, validity, and precision
|
|
2
|
+
|
|
3
|
+
GeoPandas delegates geometry work to Shapely/GEOS. Operations are planar,
|
|
4
|
+
two-dimensional, and expressed in CRS coordinate units. Z/M ordinates may be
|
|
5
|
+
carried but are not part of topology.
|
|
6
|
+
|
|
7
|
+
## Preflight state
|
|
8
|
+
|
|
9
|
+
Count missing, empty, invalid, and mixed geometries separately before any
|
|
10
|
+
constructive or set operation:
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
geometry = gdf.geometry
|
|
14
|
+
missing = geometry.isna()
|
|
15
|
+
empty = geometry.is_empty
|
|
16
|
+
invalid = (~missing) & (~empty) & (~geometry.is_valid)
|
|
17
|
+
|
|
18
|
+
state = {
|
|
19
|
+
"rows": len(gdf),
|
|
20
|
+
"missing": int(missing.sum()),
|
|
21
|
+
"empty": int(empty.sum()),
|
|
22
|
+
"invalid": int(invalid.sum()),
|
|
23
|
+
"types": geometry.geom_type.value_counts(dropna=False).to_dict(),
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`is_valid` concerns polygon/ring topology; points and lines are generally valid
|
|
28
|
+
unless malformed. `is_simple`, `is_ring`, and `minimum_clearance` answer
|
|
29
|
+
different questions.
|
|
30
|
+
|
|
31
|
+
### Redacted validity diagnostics
|
|
32
|
+
|
|
33
|
+
`is_valid_reason()` can include the coordinate of a defect, for example a
|
|
34
|
+
self-intersection. Precise coordinates can be sensitive. Aggregate only the
|
|
35
|
+
reason category before `[` and retain detailed diagnostics in a protected local
|
|
36
|
+
artifact:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
reason_category = (
|
|
40
|
+
geometry[invalid]
|
|
41
|
+
.is_valid_reason()
|
|
42
|
+
.str.split("[", n=1)
|
|
43
|
+
.str[0]
|
|
44
|
+
.value_counts()
|
|
45
|
+
)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Repair is a model change
|
|
49
|
+
|
|
50
|
+
GeoPandas 1.1 exposes Shapely 2.1 repair controls:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
repaired = geometry.make_valid(
|
|
54
|
+
method="structure",
|
|
55
|
+
keep_collapsed=True,
|
|
56
|
+
)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Methods:
|
|
60
|
+
|
|
61
|
+
- `linework` preserves every edge/vertex, nodes all rings, and reconstructs
|
|
62
|
+
areas with even/odd parity. It can produce complex `GeometryCollection`
|
|
63
|
+
output and requires `keep_collapsed=True`.
|
|
64
|
+
- `structure` repairs rings, merges shells, and subtracts holes. It assumes
|
|
65
|
+
shell/hole categorization is meaningful, requires GEOS >=3.10, and can drop
|
|
66
|
+
collapsed parts when `keep_collapsed=False`.
|
|
67
|
+
|
|
68
|
+
Repair can turn a polygon into a multipolygon, line, point, collection, or empty
|
|
69
|
+
geometry. Never replace source data in place. Create a new artifact and compare:
|
|
70
|
+
|
|
71
|
+
1. valid/invalid/null/empty counts;
|
|
72
|
+
2. geometry-type and dimensionality transitions;
|
|
73
|
+
3. component counts and collapsed outputs;
|
|
74
|
+
4. area/length changes in appropriate units;
|
|
75
|
+
5. stable feature IDs and row count;
|
|
76
|
+
6. downstream predicate/coverage behavior.
|
|
77
|
+
|
|
78
|
+
`overlay(make_valid=True)` also repairs invalid inputs, but that convenience can
|
|
79
|
+
hide type changes. Audit and repair explicitly for traceable work.
|
|
80
|
+
|
|
81
|
+
## Precision models
|
|
82
|
+
|
|
83
|
+
`set_precision(grid_size, mode=...)` rounds x/y to a grid in **CRS units**:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
snapped = geometry.set_precision(
|
|
87
|
+
grid_size=0.01,
|
|
88
|
+
mode="valid_output",
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Shapely 2.1 modes:
|
|
93
|
+
|
|
94
|
+
- `valid_output` removes collapsed polygonal/linear elements and duplicate
|
|
95
|
+
vertices while producing valid output;
|
|
96
|
+
- `pointwise` rounds independently, retains duplicate vertices, and may produce
|
|
97
|
+
invalid output;
|
|
98
|
+
- `keep_collapsed` preserves collapsed linear elements but removes collapsed
|
|
99
|
+
polygonal elements.
|
|
100
|
+
|
|
101
|
+
Consequences:
|
|
102
|
+
|
|
103
|
+
- features narrower/shorter than the grid may become empty;
|
|
104
|
+
- spikes and narrow sections can disappear or split polygons;
|
|
105
|
+
- duplicate vertices are normally removed;
|
|
106
|
+
- Z is not rounded;
|
|
107
|
+
- vertex/ring/order is canonicalized and must not be used as identity;
|
|
108
|
+
- inputs should be valid first;
|
|
109
|
+
- later operations use the higher precision (smaller grid size) of inputs.
|
|
110
|
+
|
|
111
|
+
Choose the grid from documented source resolution and error—not decimal
|
|
112
|
+
aesthetics. `0.001` degrees is not a universal metric tolerance.
|
|
113
|
+
|
|
114
|
+
For one union/dissolve, `grid_size=` can apply fixed precision without first
|
|
115
|
+
attaching a precision model:
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
merged = geometry.union_all(method="unary", grid_size=0.01)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Record whether precision was attached to inputs or applied only to an operation.
|
|
122
|
+
|
|
123
|
+
## `union_all` algorithms
|
|
124
|
+
|
|
125
|
+
GeoPandas 1.1.4 signature:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
geometry.union_all(method="unary", grid_size=None)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
- `unary`: robust general-purpose algorithm; the only method supporting
|
|
132
|
+
`grid_size`.
|
|
133
|
+
- `coverage`: optimized for non-overlapping edge-matched polygon coverages; it
|
|
134
|
+
can return invalid geometry if polygons overlap.
|
|
135
|
+
- `disjoint_subset`: optimized when input can be divided into non-intersecting
|
|
136
|
+
subsets; requires Shapely >=2.1 and may be slower when there is one subset.
|
|
137
|
+
|
|
138
|
+
Do not use `coverage` based on visual inspection:
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
if not geometry.is_valid_coverage(gap_width=0.0):
|
|
142
|
+
edges = geometry.invalid_coverage_edges(gap_width=0.0)
|
|
143
|
+
raise ValueError("Not an edge-matched non-overlapping coverage")
|
|
144
|
+
|
|
145
|
+
merged = geometry.union_all(method="coverage")
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`is_valid_coverage()` ignores non-polygon geometry and requires Shapely >=2.1.
|
|
149
|
+
If narrow gaps matter, select `gap_width` in justified projected units.
|
|
150
|
+
`simplify_coverage()` preserves shared boundaries for a valid coverage; ordinary
|
|
151
|
+
element-wise `simplify()` does not.
|
|
152
|
+
|
|
153
|
+
The old `unary_union` attribute is deprecated. Use `union_all()`.
|
|
154
|
+
|
|
155
|
+
## Constructive operations
|
|
156
|
+
|
|
157
|
+
All distance/tolerance arguments are CRS units:
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
buffered = geometry.buffer(50)
|
|
161
|
+
simplified = geometry.simplify(5, preserve_topology=True)
|
|
162
|
+
densified = geometry.segmentize(max_segment_length=10)
|
|
163
|
+
centroids = geometry.centroid
|
|
164
|
+
inside_points = geometry.representative_point()
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Correctness notes:
|
|
168
|
+
|
|
169
|
+
- Buffering geographic degrees does not create a fixed-metre buffer.
|
|
170
|
+
- Negative polygon buffers can collapse to empty.
|
|
171
|
+
- `centroid` may fall outside a concave polygon; `representative_point()` is
|
|
172
|
+
guaranteed within the geometry but is not a centroid.
|
|
173
|
+
- `preserve_topology=True` protects each geometry's validity, not shared
|
|
174
|
+
boundaries between adjacent features.
|
|
175
|
+
- `segmentize()` inserts vertices along planar segments; it does not create
|
|
176
|
+
geodesic densification.
|
|
177
|
+
- Affine rotate/scale/translate/skew operations are coordinate-space transforms,
|
|
178
|
+
not CRS transformations.
|
|
179
|
+
|
|
180
|
+
## Binary predicates
|
|
181
|
+
|
|
182
|
+
Predicates implement DE-9IM relationships and are directional:
|
|
183
|
+
|
|
184
|
+
| Predicate | Practical meaning |
|
|
185
|
+
|---|---|
|
|
186
|
+
| `intersects` | Boundaries or interiors share any point |
|
|
187
|
+
| `disjoint` | Share no point |
|
|
188
|
+
| `within` | Left geometry lies in right interior/boundary under DE-9IM |
|
|
189
|
+
| `contains` | Inverse direction of `within`; boundary-only point is not contained |
|
|
190
|
+
| `covers` | No point of right lies outside left; includes boundary cases |
|
|
191
|
+
| `covered_by` | Inverse of `covers` |
|
|
192
|
+
| `contains_properly` | Contains with no common boundary points |
|
|
193
|
+
| `touches` | Interiors do not meet, boundaries do |
|
|
194
|
+
| `crosses` | Interiors meet with lower-dimensional result |
|
|
195
|
+
| `overlaps` | Same-dimensional partial overlap, neither contains the other |
|
|
196
|
+
| `dwithin` | Planar distance is within the supplied CRS-unit threshold |
|
|
197
|
+
|
|
198
|
+
Do not describe `contains`, `covers`, and `intersects` as interchangeable.
|
|
199
|
+
Boundary-point tests are an important synthetic fixture.
|
|
200
|
+
|
|
201
|
+
Binary GeoSeries calls are one-to-one and index-aligned by default:
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
matched = left.intersects(right, align=True)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
They do not answer whether each left geometry intersects *any* right geometry.
|
|
208
|
+
Use `sjoin` or the spatial index for all-pairs matching.
|
|
209
|
+
|
|
210
|
+
## Overlay robustness and slivers
|
|
211
|
+
|
|
212
|
+
Overlay and intersection can create tiny slivers from precision mismatch,
|
|
213
|
+
near-coincident edges, or distinct source accuracy:
|
|
214
|
+
|
|
215
|
+
1. validate inputs and CRS;
|
|
216
|
+
2. quantify source precision/accuracy;
|
|
217
|
+
3. select a justified grid if snapping is appropriate;
|
|
218
|
+
4. run overlay with explicit `keep_geom_type`;
|
|
219
|
+
5. validate output and count type changes;
|
|
220
|
+
6. summarize area distribution and very small parts in projected units;
|
|
221
|
+
7. compare area conservation appropriate to the selected overlay mode.
|
|
222
|
+
|
|
223
|
+
Do not delete polygons below an arbitrary area threshold. A small polygon can be
|
|
224
|
+
legitimate, and thresholding can bias boundaries. Record any sliver rule and
|
|
225
|
+
retain pre-cleaning output.
|
|
226
|
+
|
|
227
|
+
## Equality and identity
|
|
228
|
+
|
|
229
|
+
- `geom_equals` is topological equality; coordinate order may differ.
|
|
230
|
+
- `geom_equals_exact(tolerance=...)` checks structural coordinate equality
|
|
231
|
+
within tolerance.
|
|
232
|
+
- `geom_equals_identical` exposes Shapely's identical comparison in GeoPandas
|
|
233
|
+
1.1 and includes coordinate/order details.
|
|
234
|
+
- `normalize()` can canonicalize ordering for reproducible comparisons, but a
|
|
235
|
+
normalized WKB hash is still geometry identity, not stable feature identity.
|
|
236
|
+
|
|
237
|
+
## Post-operation validation
|
|
238
|
+
|
|
239
|
+
For every geometry-changing operation, record:
|
|
240
|
+
|
|
241
|
+
- operation and all parameters;
|
|
242
|
+
- source/target CRS and units;
|
|
243
|
+
- package and GEOS versions;
|
|
244
|
+
- null/empty/invalid/type/component counts before and after;
|
|
245
|
+
- row expansion/contraction and stable-ID mapping;
|
|
246
|
+
- precision model, repair method, collapsed-part policy;
|
|
247
|
+
- area/length conservation checks where meaningful;
|
|
248
|
+
- a new output path and source/output hashes.
|
|
249
|
+
|
|
250
|
+
The bundled `scripts/geometry_validity_report.py` provides bounded dry-run
|
|
251
|
+
counts and optional new-file repair without emitting geometries or coordinates.
|
|
252
|
+
|
|
253
|
+
## Sources (verified 2026-07-23)
|
|
254
|
+
|
|
255
|
+
- [GeoPandas geometric manipulations](https://geopandas.org/en/stable/docs/user_guide/geometric_manipulations.html).
|
|
256
|
+
- [GeoSeries.make_valid](https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.make_valid.html).
|
|
257
|
+
- [GeoSeries.union_all](https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.union_all.html).
|
|
258
|
+
- [GeoSeries.is_valid_coverage](https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.is_valid_coverage.html).
|
|
259
|
+
- [Shapely 2.1.2 make_valid](https://shapely.readthedocs.io/en/2.1.2/reference/shapely.make_valid.html).
|
|
260
|
+
- [Shapely 2.1.2 set_precision](https://shapely.readthedocs.io/en/2.1.2/reference/shapely.set_precision.html).
|
|
261
|
+
- [Shapely 2.1.2 union_all](https://shapely.readthedocs.io/en/2.1.2/reference/shapely.union_all.html).
|
|
262
|
+
- [GeoPandas 1.1.0 release](https://github.com/geopandas/geopandas/releases/tag/v1.1.0) — released 2025-06-01.
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# Spatial joins, overlay, clip, and dissolve
|
|
2
|
+
|
|
3
|
+
Spatial operations can multiply, split, or merge records. Define geometry
|
|
4
|
+
semantics and expected cardinality before running them, then audit both.
|
|
5
|
+
|
|
6
|
+
## Shared preflight
|
|
7
|
+
|
|
8
|
+
For each input:
|
|
9
|
+
|
|
10
|
+
1. preserve a non-null stable feature-ID column;
|
|
11
|
+
2. count duplicate IDs and duplicate pandas indices;
|
|
12
|
+
3. require CRS and reproject to a common, justified CRS;
|
|
13
|
+
4. count null, empty, invalid, mixed, and Z/M geometries;
|
|
14
|
+
5. validate precision/accuracy compatibility;
|
|
15
|
+
6. state whether boundary contact should count;
|
|
16
|
+
7. state the expected one-to-one, one-to-many, many-to-one, or many-to-many
|
|
17
|
+
relationship.
|
|
18
|
+
|
|
19
|
+
GeoPandas operations are planar and ignore Z. Geographic longitude/latitude is
|
|
20
|
+
not suitable for distance/nearest work.
|
|
21
|
+
|
|
22
|
+
## Attribute joins
|
|
23
|
+
|
|
24
|
+
Call `merge` from the spatial object so geometry dtype and CRS are retained:
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
result = zones.merge(
|
|
28
|
+
attributes,
|
|
29
|
+
on="zone_id",
|
|
30
|
+
how="left",
|
|
31
|
+
validate="one_to_one",
|
|
32
|
+
indicator=True,
|
|
33
|
+
)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Use pandas `validate=` whenever the key contract is known. Before merging, audit
|
|
37
|
+
null/duplicate keys on both sides. Afterward, count `_merge` categories and
|
|
38
|
+
verify row count. A pandas index is not a feature key.
|
|
39
|
+
|
|
40
|
+
## Binary-predicate spatial joins
|
|
41
|
+
|
|
42
|
+
Stable GeoPandas 1.1.4 signature:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
joined = left.sjoin(
|
|
46
|
+
right,
|
|
47
|
+
how="inner",
|
|
48
|
+
predicate="intersects",
|
|
49
|
+
distance=None,
|
|
50
|
+
on_attribute=None,
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`predicate` is evaluated directionally from each left geometry against right
|
|
55
|
+
geometries. Query available values from
|
|
56
|
+
`left.sindex.valid_query_predicates`.
|
|
57
|
+
|
|
58
|
+
Common choices:
|
|
59
|
+
|
|
60
|
+
- point-in-polygon strict interior: points on polygon boundaries do not satisfy
|
|
61
|
+
`within`;
|
|
62
|
+
- point in polygon including boundary: reverse the relation and use `covers`,
|
|
63
|
+
or explicitly test the intended boundary behavior;
|
|
64
|
+
- any shared boundary/interior: `intersects`;
|
|
65
|
+
- boundary-only relationships: `touches`;
|
|
66
|
+
- distance threshold: `dwithin`.
|
|
67
|
+
|
|
68
|
+
`dwithin` requires `distance`. It may be a scalar or a one-dimensional array
|
|
69
|
+
with one value per **left row**; values are CRS units:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
joined = left.sjoin(
|
|
73
|
+
right,
|
|
74
|
+
predicate="dwithin",
|
|
75
|
+
distance=500,
|
|
76
|
+
)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`on_attribute="category"` (or a list/tuple) adds equality on columns present in
|
|
80
|
+
both frames after the spatial predicate. It is a restriction, not a substitute
|
|
81
|
+
for checking nulls, normalization, and duplicates in the attribute.
|
|
82
|
+
|
|
83
|
+
### Geometry and index retention
|
|
84
|
+
|
|
85
|
+
- `how="left"` keeps left keys and only left geometry.
|
|
86
|
+
- `how="right"` keeps right keys and only right geometry.
|
|
87
|
+
- `how="inner"` keeps matching pairs and only left geometry.
|
|
88
|
+
- GeoPandas 1.0 preserves a named right index as that output column name;
|
|
89
|
+
otherwise output often uses `index_right`. Do not hard-code that name as a
|
|
90
|
+
permanent ID.
|
|
91
|
+
|
|
92
|
+
Every qualifying pair produces a row. One left feature intersecting three right
|
|
93
|
+
features yields three rows. Empty/null geometries do not produce predicate
|
|
94
|
+
matches.
|
|
95
|
+
|
|
96
|
+
### Cardinality audit
|
|
97
|
+
|
|
98
|
+
Add internal unique row numbers before joining; never expose user IDs in a
|
|
99
|
+
public report:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
left_work = left.reset_index(drop=True).assign(_left_row=lambda x: range(len(x)))
|
|
103
|
+
right_work = right.reset_index(drop=True).assign(_right_row=lambda x: range(len(x)))
|
|
104
|
+
|
|
105
|
+
pairs = left_work[["_left_row", left_work.geometry.name]].sjoin(
|
|
106
|
+
right_work[["_right_row", right_work.geometry.name]],
|
|
107
|
+
predicate="intersects",
|
|
108
|
+
how="inner",
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
left_multiplicity = pairs.groupby("_left_row").size()
|
|
112
|
+
right_multiplicity = pairs.groupby("_right_row").size()
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Report pair count, matched/unmatched counts on both sides, and counts with
|
|
116
|
+
multiple matches. Compare these to the declared contract. The bundled
|
|
117
|
+
`scripts/spatial_join_audit.py` implements this with redacted aggregate output.
|
|
118
|
+
|
|
119
|
+
## Nearest spatial joins
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
nearest = left.sjoin_nearest(
|
|
123
|
+
right,
|
|
124
|
+
how="left",
|
|
125
|
+
max_distance=1_000,
|
|
126
|
+
distance_col="distance_crs_units",
|
|
127
|
+
exclusive=False,
|
|
128
|
+
)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Key semantics:
|
|
132
|
+
|
|
133
|
+
- distance and `max_distance` use CRS units;
|
|
134
|
+
- geographic CRS results are inaccurate;
|
|
135
|
+
- `max_distance > 0` can substantially reduce work and limits accepted matches;
|
|
136
|
+
- all equidistant nearest or intersecting neighbors are returned, so one input
|
|
137
|
+
can produce multiple rows;
|
|
138
|
+
- `exclusive=True` excludes geometrically equal nearest candidates;
|
|
139
|
+
- GeoPandas `sjoin_nearest` has no `k=` argument. For k-nearest behavior use a
|
|
140
|
+
separately designed spatial-index/neighbor workflow and define tie handling.
|
|
141
|
+
|
|
142
|
+
Never silently keep the first tie. Report tie counts and specify a deterministic
|
|
143
|
+
domain rule if one result must be selected.
|
|
144
|
+
|
|
145
|
+
## Overlay
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
result = left.overlay(
|
|
149
|
+
right,
|
|
150
|
+
how="intersection",
|
|
151
|
+
keep_geom_type=False,
|
|
152
|
+
make_valid=False,
|
|
153
|
+
)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Modes:
|
|
157
|
+
|
|
158
|
+
| `how` | Result |
|
|
159
|
+
|---|---|
|
|
160
|
+
| `intersection` | Areas/parts shared by both |
|
|
161
|
+
| `union` | All partitioned parts with attributes from either/both |
|
|
162
|
+
| `identity` | All left parts split by right |
|
|
163
|
+
| `difference` | Left minus right |
|
|
164
|
+
| `symmetric_difference` | Parts in exactly one input |
|
|
165
|
+
|
|
166
|
+
Constraints and choices:
|
|
167
|
+
|
|
168
|
+
- Each input must have a uniform supported family: (Multi)Polygon,
|
|
169
|
+
(Multi)Point, or line/LinearRing family.
|
|
170
|
+
- Inputs need the same CRS.
|
|
171
|
+
- `make_valid=True` repairs invalid inputs and may change types; pre-audit repair
|
|
172
|
+
is more traceable. With `False`, invalid inputs raise.
|
|
173
|
+
- `keep_geom_type=None` behaves as `True` and warns when dropping other result
|
|
174
|
+
types. Set it explicitly and count dropped/type-changed results.
|
|
175
|
+
- Union-like modes place `NaN` in attributes absent from one side.
|
|
176
|
+
- Near-coincident boundaries and precision mismatch produce slivers. Use a
|
|
177
|
+
justified precision model, quantify small parts, and validate area balance.
|
|
178
|
+
|
|
179
|
+
Always keep source IDs from both inputs. Overlay can split one source feature
|
|
180
|
+
into many derived records.
|
|
181
|
+
|
|
182
|
+
## Clip
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
clipped = gpd.clip(
|
|
186
|
+
features,
|
|
187
|
+
mask,
|
|
188
|
+
keep_geom_type=False,
|
|
189
|
+
sort=False,
|
|
190
|
+
)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
- Both layers must share CRS.
|
|
194
|
+
- Multiple mask geometries are dissolved before intersection, so mask
|
|
195
|
+
attributes are not transferred.
|
|
196
|
+
- A four-number `(minx, miny, maxx, maxy)` mask activates a fast rectangle
|
|
197
|
+
path. It is possibly dirty, does not guarantee valid output, and can omit a
|
|
198
|
+
line that collapses to a point.
|
|
199
|
+
- `keep_geom_type=False` retains mixed-dimensional outputs; set deliberately.
|
|
200
|
+
- `sort=False` does not promise source order. Preserve IDs and sort explicitly
|
|
201
|
+
if order is part of the contract.
|
|
202
|
+
|
|
203
|
+
Validate clip output. Use overlay intersection when mask attributes or more
|
|
204
|
+
auditable topology are required.
|
|
205
|
+
|
|
206
|
+
## Dissolve
|
|
207
|
+
|
|
208
|
+
`dissolve` performs `groupby.agg` on attributes and `union_all` on geometry:
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
dissolved = parcels.dissolve(
|
|
212
|
+
by="region_id",
|
|
213
|
+
aggfunc={
|
|
214
|
+
"population": "sum",
|
|
215
|
+
"source_date": "max",
|
|
216
|
+
},
|
|
217
|
+
as_index=False,
|
|
218
|
+
dropna=False,
|
|
219
|
+
method="unary",
|
|
220
|
+
grid_size=0.01,
|
|
221
|
+
)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Avoid the default `aggfunc="first"` for meaningful attributes. Specify every
|
|
225
|
+
aggregation and its units. Decide whether null group keys should be dropped
|
|
226
|
+
(`dropna=True`) or retained as a group (`False`).
|
|
227
|
+
|
|
228
|
+
Union methods:
|
|
229
|
+
|
|
230
|
+
- `unary`: robust general default; supports `grid_size`;
|
|
231
|
+
- `coverage`: fast only for proven non-overlapping, edge-matched polygons and
|
|
232
|
+
may produce invalid output otherwise;
|
|
233
|
+
- `disjoint_subset`: Shapely >=2.1, useful for disjoint partitions.
|
|
234
|
+
|
|
235
|
+
For coverage mode, run `is_valid_coverage()` first. After dissolve, compare
|
|
236
|
+
group counts, summed attributes, validity, empty output, and area in a suitable
|
|
237
|
+
projected CRS.
|
|
238
|
+
|
|
239
|
+
## Spatial index
|
|
240
|
+
|
|
241
|
+
GeoPandas uses Shapely's spatial index automatically for joins, clip, and
|
|
242
|
+
overlay. Direct queries are candidate/predicate operations:
|
|
243
|
+
|
|
244
|
+
```python
|
|
245
|
+
predicate_names = gdf.sindex.valid_query_predicates
|
|
246
|
+
indices = gdf.sindex.query(query_geometry, predicate="intersects")
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
GeoPandas 1.0 removed `sindex.query_bulk`; use `query`. GeoPandas 1.1 supports
|
|
250
|
+
indices, dense boolean, and optional SciPy sparse boolean output formats. Do not
|
|
251
|
+
assume the shape/orientation of an undocumented output; set `output_format`
|
|
252
|
+
explicitly and test.
|
|
253
|
+
|
|
254
|
+
Spatial indexing does not fix CRS, invalid geometry, distance units, predicate
|
|
255
|
+
direction, or cardinality.
|
|
256
|
+
|
|
257
|
+
## Area and distance checks
|
|
258
|
+
|
|
259
|
+
For planar metrics:
|
|
260
|
+
|
|
261
|
+
```python
|
|
262
|
+
if gdf.crs is None or gdf.crs.is_geographic:
|
|
263
|
+
raise ValueError("Use a justified projected CRS")
|
|
264
|
+
|
|
265
|
+
area = gdf.geometry.area
|
|
266
|
+
length = gdf.geometry.length
|
|
267
|
+
distance = gdf.geometry.distance(reference_geometry)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Confirm axis unit and conversion factor before labeling values. For
|
|
271
|
+
large/global or cross-zone work, use a geodesic design instead.
|
|
272
|
+
|
|
273
|
+
## Provenance checklist
|
|
274
|
+
|
|
275
|
+
Record:
|
|
276
|
+
|
|
277
|
+
- source hashes, layer names, stable IDs, input row/state counts;
|
|
278
|
+
- source and operation CRS, units, and transform pipeline;
|
|
279
|
+
- predicate direction, boundary semantics, distance/max-distance;
|
|
280
|
+
- join type, attribute restrictions, expected and observed cardinality;
|
|
281
|
+
- validity repair, precision grid, overlay/union method;
|
|
282
|
+
- mask dissolve/rectangle choice, dissolve aggregations;
|
|
283
|
+
- output row/type/state counts and new artifact hash.
|
|
284
|
+
|
|
285
|
+
## Sources (verified 2026-07-23)
|
|
286
|
+
|
|
287
|
+
- [GeoPandas merging data guide](https://geopandas.org/en/stable/docs/user_guide/mergingdata.html).
|
|
288
|
+
- [geopandas.sjoin](https://geopandas.org/en/stable/docs/reference/api/geopandas.sjoin.html).
|
|
289
|
+
- [geopandas.sjoin_nearest](https://geopandas.org/en/stable/docs/reference/api/geopandas.sjoin_nearest.html).
|
|
290
|
+
- [geopandas.overlay](https://geopandas.org/en/stable/docs/reference/api/geopandas.overlay.html).
|
|
291
|
+
- [geopandas.clip](https://geopandas.org/en/stable/docs/reference/api/geopandas.clip.html).
|
|
292
|
+
- [GeoDataFrame.dissolve](https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.dissolve.html).
|
|
293
|
+
- [GeoPandas set operations guide](https://geopandas.org/en/stable/docs/user_guide/set_operations.html).
|
|
294
|
+
- [GeoPandas aggregation with dissolve](https://geopandas.org/en/stable/docs/user_guide/aggregation_with_dissolve.html).
|