terraproof 0.0.0-stage → 0.4.1

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.
Files changed (182) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +356 -2
  3. package/bin/terraproof.js +4 -0
  4. package/dist/router/apply.js +147 -0
  5. package/dist/router/classify.js +111 -0
  6. package/dist/router/explain.js +9 -0
  7. package/dist/router/index.js +272 -0
  8. package/dist/router/resolve.js +239 -0
  9. package/dist/router/score.js +60 -0
  10. package/dist/router/types.js +1 -0
  11. package/dist/router/validate.js +81 -0
  12. package/instructions/engineering.md +14 -0
  13. package/instructions/security.md +14 -0
  14. package/instructions/simplicity.md +13 -0
  15. package/package.json +65 -4
  16. package/routing/DOMAIN_ROUTING.md +161 -0
  17. package/routing/ROUTER_DESIGN.md +173 -0
  18. package/routing/SKILL_MANIFEST.md +86 -0
  19. package/routing/checks.json +121 -0
  20. package/routing/collections.json +47 -0
  21. package/routing/registry.json +2211 -0
  22. package/routing/rules.json +64 -0
  23. package/skills/accuracy-assessment/SKILL.md +213 -0
  24. package/skills/accuracy-assessment/UPSTREAM.md +7 -0
  25. package/skills/accuracy-assessment/references/stratified-estimator.md +127 -0
  26. package/skills/accuracy-assessment/skill.json +103 -0
  27. package/skills/brainstorming/SKILL.md +250 -0
  28. package/skills/brainstorming/UPSTREAM.md +29 -0
  29. package/skills/brainstorming/scripts/frame-template.html +213 -0
  30. package/skills/brainstorming/scripts/helper.js +167 -0
  31. package/skills/brainstorming/scripts/server.cjs +723 -0
  32. package/skills/brainstorming/scripts/start-server.sh +209 -0
  33. package/skills/brainstorming/scripts/stop-server.sh +120 -0
  34. package/skills/brainstorming/skill.json +73 -0
  35. package/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
  36. package/skills/brainstorming/visual-companion.md +299 -0
  37. package/skills/crs-and-units/SKILL.md +200 -0
  38. package/skills/crs-and-units/UPSTREAM.md +7 -0
  39. package/skills/crs-and-units/skill.json +74 -0
  40. package/skills/design-postgis-tables/SKILL.md +494 -0
  41. package/skills/design-postgis-tables/UPSTREAM.md +21 -0
  42. package/skills/design-postgis-tables/skill.json +90 -0
  43. package/skills/gdal/SKILL.md +125 -0
  44. package/skills/gdal/UPSTREAM.md +22 -0
  45. package/skills/gdal/references/gdal-recipes.md +103 -0
  46. package/skills/gdal/skill.json +66 -0
  47. package/skills/geomaster/README.md +105 -0
  48. package/skills/geomaster/SKILL.md +366 -0
  49. package/skills/geomaster/UPSTREAM.md +21 -0
  50. package/skills/geomaster/references/advanced-gis.md +376 -0
  51. package/skills/geomaster/references/big-data.md +363 -0
  52. package/skills/geomaster/references/code-examples.md +531 -0
  53. package/skills/geomaster/references/coordinate-systems.md +364 -0
  54. package/skills/geomaster/references/core-libraries.md +273 -0
  55. package/skills/geomaster/references/data-sources.md +330 -0
  56. package/skills/geomaster/references/gis-software.md +369 -0
  57. package/skills/geomaster/references/industry-applications.md +420 -0
  58. package/skills/geomaster/references/machine-learning.md +462 -0
  59. package/skills/geomaster/references/programming-languages.md +456 -0
  60. package/skills/geomaster/references/remote-sensing.md +370 -0
  61. package/skills/geomaster/references/scientific-domains.md +416 -0
  62. package/skills/geomaster/references/specialized-topics.md +428 -0
  63. package/skills/geomaster/references/troubleshooting.md +439 -0
  64. package/skills/geomaster/skill.json +157 -0
  65. package/skills/geopandas/SKILL.md +250 -0
  66. package/skills/geopandas/UPSTREAM.md +22 -0
  67. package/skills/geopandas/references/crs-management.md +231 -0
  68. package/skills/geopandas/references/data-io.md +323 -0
  69. package/skills/geopandas/references/data-structures.md +207 -0
  70. package/skills/geopandas/references/geometric-operations.md +262 -0
  71. package/skills/geopandas/references/spatial-analysis.md +294 -0
  72. package/skills/geopandas/references/visualization.md +230 -0
  73. package/skills/geopandas/scripts/_common.py +605 -0
  74. package/skills/geopandas/scripts/crs_reprojection_plan.py +210 -0
  75. package/skills/geopandas/scripts/export_plan.py +305 -0
  76. package/skills/geopandas/scripts/geometry_validity_report.py +227 -0
  77. package/skills/geopandas/scripts/sensitive_coordinates_checklist.py +230 -0
  78. package/skills/geopandas/scripts/spatial_join_audit.py +368 -0
  79. package/skills/geopandas/scripts/vector_inventory.py +140 -0
  80. package/skills/geopandas/skill.json +116 -0
  81. package/skills/geoparquet-validation/SKILL.md +125 -0
  82. package/skills/geoparquet-validation/UPSTREAM.md +24 -0
  83. package/skills/geoparquet-validation/references/distribution-best-practices.md +118 -0
  84. package/skills/geoparquet-validation/references/gpio-commands.md +264 -0
  85. package/skills/geoparquet-validation/references/tool-comparison.md +155 -0
  86. package/skills/geoparquet-validation/skill.json +71 -0
  87. package/skills/geospatial-data-discovery/SKILL.md +49 -0
  88. package/skills/geospatial-data-discovery/UPSTREAM.md +15 -0
  89. package/skills/geospatial-data-discovery/skill.json +51 -0
  90. package/skills/geospatial-frontend/SKILL.md +86 -0
  91. package/skills/geospatial-frontend/UPSTREAM.md +23 -0
  92. package/skills/geospatial-frontend/references/design-system.md +283 -0
  93. package/skills/geospatial-frontend/references/user-preferences.md +96 -0
  94. package/skills/geospatial-frontend/skill.json +87 -0
  95. package/skills/geozarr/SKILL.md +244 -0
  96. package/skills/geozarr/UPSTREAM.md +21 -0
  97. package/skills/geozarr/skill.json +97 -0
  98. package/skills/networkx/SKILL.md +440 -0
  99. package/skills/networkx/UPSTREAM.md +22 -0
  100. package/skills/networkx/references/algorithms.md +384 -0
  101. package/skills/networkx/references/generators.md +385 -0
  102. package/skills/networkx/references/graph-basics.md +284 -0
  103. package/skills/networkx/references/io.md +457 -0
  104. package/skills/networkx/references/visualization.md +531 -0
  105. package/skills/networkx/skill.json +93 -0
  106. package/skills/raster-processing/SKILL.md +222 -0
  107. package/skills/raster-processing/UPSTREAM.md +7 -0
  108. package/skills/raster-processing/skill.json +81 -0
  109. package/skills/sar-basics/SKILL.md +202 -0
  110. package/skills/sar-basics/UPSTREAM.md +7 -0
  111. package/skills/sar-basics/references/calibration-lut.md +98 -0
  112. package/skills/sar-basics/skill.json +97 -0
  113. package/skills/satellite-preprocessing/SKILL.md +241 -0
  114. package/skills/satellite-preprocessing/UPSTREAM.md +7 -0
  115. package/skills/satellite-preprocessing/skill.json +84 -0
  116. package/skills/scientific-visualization/SKILL.md +285 -0
  117. package/skills/scientific-visualization/UPSTREAM.md +25 -0
  118. package/skills/scientific-visualization/assets/color_palettes.py +263 -0
  119. package/skills/scientific-visualization/assets/nature.mplstyle +68 -0
  120. package/skills/scientific-visualization/assets/presentation.mplstyle +68 -0
  121. package/skills/scientific-visualization/assets/publication.mplstyle +77 -0
  122. package/skills/scientific-visualization/assets/publisher_profiles.json +269 -0
  123. package/skills/scientific-visualization/references/color_palettes.md +227 -0
  124. package/skills/scientific-visualization/references/journal_requirements.md +169 -0
  125. package/skills/scientific-visualization/references/matplotlib_examples.md +336 -0
  126. package/skills/scientific-visualization/references/publication_guidelines.md +196 -0
  127. package/skills/scientific-visualization/references/sources.md +76 -0
  128. package/skills/scientific-visualization/scripts/_common.py +136 -0
  129. package/skills/scientific-visualization/scripts/export_plan.py +493 -0
  130. package/skills/scientific-visualization/scripts/figure_export.py +642 -0
  131. package/skills/scientific-visualization/scripts/image_metadata.py +731 -0
  132. package/skills/scientific-visualization/scripts/palette_audit.py +327 -0
  133. package/skills/scientific-visualization/scripts/style_presets.py +501 -0
  134. package/skills/scientific-visualization/scripts/style_preview.py +232 -0
  135. package/skills/scientific-visualization/skill.json +95 -0
  136. package/skills/spectral-indices/SKILL.md +235 -0
  137. package/skills/spectral-indices/UPSTREAM.md +7 -0
  138. package/skills/spectral-indices/skill.json +88 -0
  139. package/skills/stac-and-cloud-access/SKILL.md +209 -0
  140. package/skills/stac-and-cloud-access/UPSTREAM.md +7 -0
  141. package/skills/stac-and-cloud-access/references/run-manifest.md +94 -0
  142. package/skills/stac-and-cloud-access/skill.json +98 -0
  143. package/skills/statistical-analysis/SKILL.md +446 -0
  144. package/skills/statistical-analysis/UPSTREAM.md +24 -0
  145. package/skills/statistical-analysis/references/assumptions_and_diagnostics.md +379 -0
  146. package/skills/statistical-analysis/references/bayesian_statistics.md +686 -0
  147. package/skills/statistical-analysis/references/effect_sizes_and_power.md +649 -0
  148. package/skills/statistical-analysis/references/reporting_standards.md +482 -0
  149. package/skills/statistical-analysis/references/test_selection_guide.md +129 -0
  150. package/skills/statistical-analysis/scripts/assumption_checks.py +652 -0
  151. package/skills/statistical-analysis/skill.json +127 -0
  152. package/skills/systematic-debugging/SKILL.md +283 -0
  153. package/skills/systematic-debugging/UPSTREAM.md +28 -0
  154. package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
  155. package/skills/systematic-debugging/condition-based-waiting.md +115 -0
  156. package/skills/systematic-debugging/defense-in-depth.md +122 -0
  157. package/skills/systematic-debugging/find-polluter.sh +72 -0
  158. package/skills/systematic-debugging/root-cause-tracing.md +169 -0
  159. package/skills/systematic-debugging/skill.json +88 -0
  160. package/skills/time-series-and-change/SKILL.md +217 -0
  161. package/skills/time-series-and-change/UPSTREAM.md +7 -0
  162. package/skills/time-series-and-change/references/change-methods.md +42 -0
  163. package/skills/time-series-and-change/skill.json +116 -0
  164. package/skills/vector-quality/SKILL.md +204 -0
  165. package/skills/vector-quality/UPSTREAM.md +7 -0
  166. package/skills/vector-quality/skill.json +82 -0
  167. package/skills/verification/SKILL.md +120 -0
  168. package/skills/verification/UPSTREAM.md +31 -0
  169. package/skills/verification/skill.json +67 -0
  170. package/skills/writing-plans/SKILL.md +171 -0
  171. package/skills/writing-plans/UPSTREAM.md +23 -0
  172. package/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
  173. package/skills/writing-plans/skill.json +80 -0
  174. package/sources/SOURCES.md +374 -0
  175. package/third_party/isaaccorley-geospatial-skills/LICENSE +176 -0
  176. package/third_party/isaaccorley-geospatial-skills/NOTICE.md +17 -0
  177. package/third_party/kdense-scientific-agent-skills/LICENSE +21 -0
  178. package/third_party/kdense-scientific-agent-skills/NOTICE.md +19 -0
  179. package/third_party/superpowers/LICENSE +21 -0
  180. package/third_party/superpowers/NOTICE.md +17 -0
  181. package/third_party/timescale-pg-aiguide/LICENSE +176 -0
  182. package/third_party/timescale-pg-aiguide/NOTICE.md +14 -0
@@ -0,0 +1,364 @@
1
+ # Coordinate Reference Systems (CRS)
2
+
3
+ Complete guide to coordinate systems, projections, and transformations for geospatial data.
4
+
5
+ ## Table of Contents
6
+
7
+ 1. [Fundamentals](#fundamentals)
8
+ 2. [Common CRS Codes](#common-crs-codes)
9
+ 3. [Projected vs Geographic](#projected-vs-geographic)
10
+ 4. [UTM Zones](#utm-zones)
11
+ 5. [Transformations](#transformations)
12
+ 6. [Best Practices](#best-practices)
13
+
14
+ ## Fundamentals
15
+
16
+ ### What is a CRS?
17
+
18
+ A Coordinate Reference System defines how coordinates relate to positions on Earth:
19
+
20
+ - **Geographic CRS**: Uses latitude/longitude (degrees)
21
+ - **Projected CRS**: Uses Cartesian coordinates (meters, feet)
22
+ - **Vertical CRS**: Defines height/depth (e.g., ellipsoidal heights)
23
+
24
+ ### Components
25
+
26
+ 1. **Datum**: Mathematical model of Earth's shape
27
+ - WGS 84 (EPSG:4326) - Global GPS
28
+ - NAD 83 (EPSG:4269) - North America
29
+ - ETRS89 (EPSG:4258) - Europe
30
+
31
+ 2. **Projection**: Transformation from curved to flat surface
32
+ - Cylindrical (Mercator)
33
+ - Conic (Lambert Conformal)
34
+ - Azimuthal (Polar Stereographic)
35
+
36
+ 3. **Units**: Degrees, meters, feet, etc.
37
+
38
+ ## Common CRS Codes
39
+
40
+ ### Geographic CRS (Lat/Lon)
41
+
42
+ | EPSG | Name | Area | Notes |
43
+ |------|------|------|-------|
44
+ | 4326 | WGS 84 | Global | GPS default, use for storage |
45
+ | 4269 | NAD83 | North America | USGS data, slightly different from WGS84 |
46
+ | 4258 | ETRS89 | Europe | European reference frame |
47
+ | 4612 | GDA94 | Australia | Australian datum |
48
+
49
+ ### Projected CRS (Meters)
50
+
51
+ | EPSG | Name | Area | Distortion | Notes |
52
+ |------|------|------|------------|-------|
53
+ | 3857 | Web Mercator | Global (85°S-85°N) | High near poles | Web maps (Google, OSM) |
54
+ | 32601-32660 | UTM Zone N | Global (1° bands) | <1% per zone | Metric calculations |
55
+ | 32701-32760 | UTM Zone S | Global (1° bands) | <1% per zone | Southern hemisphere |
56
+ | 3395 | Mercator | World | Moderate | World maps |
57
+ | 5070 | CONUS Albers | USA (conterminous) | Low | US national mapping |
58
+ | 2154 | Lambert-93 | France | Very low | French national projection |
59
+
60
+ ### Regional Projections
61
+
62
+ **United States:**
63
+ - EPSG:5070 - US National Atlas Equal Area (CONUS)
64
+ - EPSG:6350 - US National Atlas (Alaska)
65
+ - EPSG:102003 - USA Contiguous Albers Equal Area
66
+ - EPSG:2227 - California Zone 3 (US Feet)
67
+
68
+ **Europe:**
69
+ - EPSG:3035 - Europe Equal Area 2001
70
+ - EPSG:3857 - Web Mercator (web mapping)
71
+ - EPSG:2154 - Lambert 93 (France)
72
+ - EPSG:25832-25836 - UTM zones (ETRS89)
73
+
74
+ **Other:**
75
+ - EPSG:3112 - GDA94 / MGA zone 52 (Australia)
76
+ - EPSG:2056 - CH1903+ / LV95 (Switzerland)
77
+ - EPSG:4326 - WGS 84 (global default)
78
+
79
+ ## Projected vs Geographic
80
+
81
+ ### When to Use Geographic (EPSG:4326)
82
+
83
+ ✅ Storing data (databases, files)
84
+ ✅ Global datasets
85
+ ✅ Web APIs (GeoJSON, KML)
86
+ ✅ Latitude/longitude queries
87
+ ✅ GPS coordinates
88
+
89
+ ```python
90
+ # Bad: Distance calculation in geographic CRS
91
+ gpd.geographic_crs = "EPSG:4326"
92
+ distance = gdf.geometry.length # WRONG! Returns degrees, not meters
93
+
94
+ # Good: Calculate distance in projected CRS
95
+ gdf_projected = gdf.to_crs("EPSG:32633") # UTM Zone 33N
96
+ distance_m = gdf_projected.geometry.length # Correct: meters
97
+ ```
98
+
99
+ ### When to Use Projected
100
+
101
+ ✅ Area/distance calculations
102
+ ✅ Buffer operations
103
+ ✅ Spatial analysis
104
+ ✅ High-resolution mapping
105
+ ✅ Engineering applications
106
+
107
+ ```python
108
+ import geopandas as gpd
109
+
110
+ # Project to appropriate UTM zone
111
+ gdf = gpd.to_crs(gdf.estimate_utm_crs())
112
+
113
+ # Now area and distance are accurate
114
+ area_sqm = gdf.geometry.area
115
+ buffer_1km = gdf.geometry.buffer(1000) # 1000 meters
116
+ ```
117
+
118
+ ### Web Mercator Warning
119
+
120
+ ⚠️ **EPSG:3857 (Web Mercator) for visualization only**
121
+
122
+ ```python
123
+ # DON'T use Web Mercator for area calculations
124
+ gdf_web = gdf.to_crs("EPSG:3857")
125
+ area = gdf_web.geometry.area # WRONG! Significant distortion
126
+
127
+ # DO use appropriate projection
128
+ gdf_utm = gdf.to_crs("EPSG:32633") # or estimate_utm_crs()
129
+ area = gdf_utm.geometry.area # Correct
130
+ ```
131
+
132
+ ## UTM Zones
133
+
134
+ ### Understanding UTM Zones
135
+
136
+ Earth is divided into 60 zones (6° longitude each):
137
+ - Zones 1-60: West to East
138
+ - Each zone divided into North (326xx) and South (327xx)
139
+
140
+ ### Finding Your UTM Zone
141
+
142
+ ```python
143
+ def get_utm_zone(longitude, latitude):
144
+ """Get UTM zone EPSG code from coordinates."""
145
+ import math
146
+
147
+ zone = math.floor((longitude + 180) / 6) + 1
148
+
149
+ if latitude >= 0:
150
+ epsg = 32600 + zone # Northern hemisphere
151
+ else:
152
+ epsg = 32700 + zone # Southern hemisphere
153
+
154
+ return f"EPSG:{epsg}"
155
+
156
+ # Example
157
+ get_utm_zone(-122.4, 37.7) # Returns 'EPSG:32610' (Zone 10N)
158
+ ```
159
+
160
+ ### Auto-Detect UTM Zone with GeoPandas
161
+
162
+ ```python
163
+ import geopandas as gpd
164
+
165
+ # Load data
166
+ gdf = gpd.read_file('data.geojson')
167
+
168
+ # Estimate best UTM zone
169
+ utm_crs = gdf.estimate_utm_crs()
170
+ print(f"Best UTM CRS: {utm_crs}")
171
+
172
+ # Reproject
173
+ gdf_projected = gdf.to_crs(utm_crs)
174
+ ```
175
+
176
+ ### Special UTM Cases
177
+
178
+ **UPS (Universal Polar Stereographic):**
179
+ - EPSG:5041 - UPS North (Arctic)
180
+ - EPSG:5042 - UPS South (Antarctic)
181
+
182
+ **UTM Non-standard:**
183
+ - EPSG:31466-31469 - German Gauss-Krüger zones
184
+ - EPSG:2056 - Swiss LV95 (based on UTM principles)
185
+
186
+ ## Transformations
187
+
188
+ ### Basic Transformation
189
+
190
+ ```python
191
+ from pyproj import Transformer
192
+
193
+ # Create transformer
194
+ transformer = Transformer.from_crs(
195
+ "EPSG:4326", # WGS 84 (lat/lon)
196
+ "EPSG:32633", # UTM Zone 33N (meters)
197
+ always_xy=True # Input: x=lon, y=lat (not y=lat, x=lon)
198
+ )
199
+
200
+ # Transform single point
201
+ lon, lat = -122.4, 37.7
202
+ x, y = transformer.transform(lon, lat)
203
+ print(f"Easting: {x:.2f}, Northing: {y:.2f}")
204
+ ```
205
+
206
+ ### Batch Transformation
207
+
208
+ ```python
209
+ import numpy as np
210
+ from pyproj import Transformer
211
+
212
+ # Arrays of coordinates
213
+ lon_array = [-122.4, -122.3]
214
+ lat_array = [37.7, 37.8]
215
+
216
+ transformer = Transformer.from_crs("EPSG:4326", "EPSG:32610", always_xy=True)
217
+ xs, ys = transformer.transform(lon_array, lat_array)
218
+ ```
219
+
220
+ ### Transformation with PyProj CRS
221
+
222
+ ```python
223
+ from pyproj import CRS
224
+
225
+ # Get CRS information
226
+ crs = CRS.from_epsg(32633)
227
+
228
+ print(f"Name: {crs.name}")
229
+ print(f"Type: {crs.type_name}")
230
+ print(f"Area of use: {crs.area_of_use.name}")
231
+ print(f"Datum: {crs.datum.name}")
232
+ print(f"Ellipsoid: {crs.ellipsoid_name}")
233
+ ```
234
+
235
+ ## Best Practices
236
+
237
+ ### 1. Always Know Your CRS
238
+
239
+ ```python
240
+ import geopandas as gpd
241
+
242
+ gdf = gpd.read_file('data.geojson')
243
+
244
+ # Check CRS immediately
245
+ print(f"CRS: {gdf.crs}") # Should never be None!
246
+
247
+ # If None, set it
248
+ if gdf.crs is None:
249
+ gdf.set_crs("EPSG:4326", inplace=True)
250
+ ```
251
+
252
+ ### 2. Verify CRS Before Operations
253
+
254
+ ```python
255
+ def ensure_same_crs(gdf1, gdf2):
256
+ """Ensure two GeoDataFrames have same CRS."""
257
+ if gdf1.crs != gdf2.crs:
258
+ gdf2 = gdf2.to_crs(gdf1.crs)
259
+ print(f"Reprojected gdf2 to {gdf1.crs}")
260
+ return gdf1, gdf2
261
+
262
+ # Use before spatial operations
263
+ zones, points = ensure_same_crs(zones_gdf, points_gdf)
264
+ result = gpd.sjoin(points, zones, predicate='within')
265
+ ```
266
+
267
+ ### 3. Use Appropriate Projections
268
+
269
+ ```python
270
+ # For local analysis (< 500km extent)
271
+ gdf_local = gdf.to_crs(gdf.estimate_utm_crs())
272
+
273
+ # For national/regional analysis
274
+ gdf_us = gdf.to_crs("EPSG:5070") # US National Atlas Equal Area
275
+ gdf_eu = gdf.to_crs("EPSG:3035") # Europe Equal Area
276
+
277
+ # For web visualization
278
+ gdf_web = gdf.to_crs("EPSG:3857") # Web Mercator
279
+ ```
280
+
281
+ ### 4. Preserve Original CRS
282
+
283
+ ```python
284
+ # Keep original as backup
285
+ gdf_original = gdf.copy()
286
+ original_crs = gdf.crs
287
+
288
+ # Do analysis in projected CRS
289
+ gdf_projected = gdf.to_crs(gdf.estimate_utm_crs())
290
+ result = gdf_projected.geometry.buffer(1000)
291
+
292
+ # Convert back if needed
293
+ result = result.to_crs(original_crs)
294
+ ```
295
+
296
+ ## Common Pitfalls
297
+
298
+ ### Mistake 1: Area in Degrees
299
+
300
+ ```python
301
+ # WRONG: Area in square degrees
302
+ gdf = gpd.read_file('data.geojson')
303
+ area = gdf.geometry.area # Wrong!
304
+
305
+ # CORRECT: Use projected CRS
306
+ gdf_proj = gdf.to_crs(gdf.estimate_utm_crs())
307
+ area_sqm = gdf_proj.geometry.area
308
+ area_sqkm = area_sqm / 1_000_000
309
+ ```
310
+
311
+ ### Mistake 2: Buffer in Geographic CRS
312
+
313
+ ```python
314
+ # WRONG: Buffer of 1000 degrees
315
+ gdf['buffer'] = gdf.geometry.buffer(1000)
316
+
317
+ # CORRECT: Project first
318
+ gdf_proj = gdf.to_crs("EPSG:32610")
319
+ gdf_proj['buffer_km'] = gdf_proj.geometry.buffer(1000) # 1000 meters
320
+ ```
321
+
322
+ ### Mistake 3: Mixing CRS
323
+
324
+ ```python
325
+ # WRONG: Spatial join without checking CRS
326
+ result = gpd.sjoin(gdf1, gdf2, predicate='intersects')
327
+
328
+ # CORRECT: Ensure same CRS
329
+ if gdf1.crs != gdf2.crs:
330
+ gdf2 = gdf2.to_crs(gdf1.crs)
331
+ result = gpd.sjoin(gdf1, gdf2, predicate='intersects')
332
+ ```
333
+
334
+ ## Quick Reference
335
+
336
+ ```python
337
+ # Common operations
338
+
339
+ # Check CRS
340
+ gdf.crs
341
+ rasterio.open('file.tif').crs
342
+
343
+ # Reproject
344
+ gdf.to_crs("EPSG:32633")
345
+
346
+ # Auto-detect UTM
347
+ gdf.estimate_utm_crs()
348
+
349
+ # Transform single point
350
+ from pyproj import Transformer
351
+ tx = Transformer.from_crs("EPSG:4326", "EPSG:32610", always_xy=True)
352
+ x, y = tx.transform(lon, lat)
353
+
354
+ # Create custom CRS
355
+ from pyproj import CRS
356
+ custom_crs = CRS.from_proj4(
357
+ "+proj=utm +zone=10 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
358
+ )
359
+ ```
360
+
361
+ For more information, see:
362
+ - [EPSG Registry](https://epsg.org/)
363
+ - [PROJ documentation](https://proj.org/)
364
+ - [pyproj documentation](https://pyproj4.github.io/pyproj/)
@@ -0,0 +1,273 @@
1
+ # Core Geospatial Libraries
2
+
3
+ This reference covers the fundamental Python libraries for geospatial data processing.
4
+
5
+ ## GDAL (Geospatial Data Abstraction Library)
6
+
7
+ GDAL is the foundation for geospatial I/O in Python.
8
+
9
+ ```python
10
+ from osgeo import gdal
11
+
12
+ # Open a raster file
13
+ ds = gdal.Open('raster.tif')
14
+ band = ds.GetRasterBand(1)
15
+ data = band.ReadAsArray()
16
+
17
+ # Get geotransform
18
+ geotransform = ds.GetGeoTransform()
19
+ origin_x = geotransform[0]
20
+ pixel_width = geotransform[1]
21
+
22
+ # Get projection
23
+ proj = ds.GetProjection()
24
+ ```
25
+
26
+ ## Rasterio
27
+
28
+ Rasterio provides a cleaner interface to GDAL.
29
+
30
+ ```python
31
+ import rasterio
32
+ import numpy as np
33
+
34
+ # Basic reading
35
+ with rasterio.open('raster.tif') as src:
36
+ data = src.read() # All bands
37
+ band1 = src.read(1) # Single band
38
+ profile = src.profile # Metadata
39
+
40
+ # Windowed reading (memory efficient)
41
+ with rasterio.open('large.tif') as src:
42
+ window = ((0, 100), (0, 100))
43
+ subset = src.read(1, window=window)
44
+
45
+ # Writing
46
+ with rasterio.open('output.tif', 'w',
47
+ driver='GTiff',
48
+ height=data.shape[0],
49
+ width=data.shape[1],
50
+ count=1,
51
+ dtype=data.dtype,
52
+ crs=src.crs,
53
+ transform=src.transform) as dst:
54
+ dst.write(data, 1)
55
+
56
+ # Masking
57
+ with rasterio.open('raster.tif') as src:
58
+ masked_data, mask = rasterio.mask.mask(src, shapes=[polygon], crop=True)
59
+ ```
60
+
61
+ ## Fiona
62
+
63
+ Fiona handles vector data I/O.
64
+
65
+ ```python
66
+ import fiona
67
+
68
+ # Read features
69
+ with fiona.open('data.geojson') as src:
70
+ for feature in src:
71
+ geom = feature['geometry']
72
+ props = feature['properties']
73
+
74
+ # Get schema and CRS
75
+ with fiona.open('data.shp') as src:
76
+ schema = src.schema
77
+ crs = src.crs
78
+
79
+ # Write data
80
+ schema = {'geometry': 'Point', 'properties': {'name': 'str'}}
81
+ with fiona.open('output.geojson', 'w', driver='GeoJSON',
82
+ schema=schema, crs='EPSG:4326') as dst:
83
+ dst.write({
84
+ 'geometry': {'type': 'Point', 'coordinates': [0, 0]},
85
+ 'properties': {'name': 'Origin'}
86
+ })
87
+ ```
88
+
89
+ ## Shapely
90
+
91
+ Shapely provides geometric operations.
92
+
93
+ ```python
94
+ from shapely.geometry import Point, LineString, Polygon
95
+ from shapely.ops import unary_union
96
+
97
+ # Create geometries
98
+ point = Point(0, 0)
99
+ line = LineString([(0, 0), (1, 1)])
100
+ poly = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
101
+
102
+ # Geometric operations
103
+ buffered = point.buffer(1) # Buffer
104
+ simplified = poly.simplify(0.01) # Simplify
105
+ centroid = poly.centroid # Centroid
106
+ intersection = poly1.intersection(poly2) # Intersection
107
+
108
+ # Spatial relationships
109
+ point.within(poly) # True if point inside polygon
110
+ poly1.intersects(poly2) # True if geometries intersect
111
+ poly1.contains(poly2) # True if poly2 inside poly1
112
+
113
+ # Unary union
114
+ combined = unary_union([poly1, poly2, poly3])
115
+
116
+ # Buffer with different joins
117
+ buffer_round = point.buffer(1, quad_segs=16)
118
+ buffer_mitre = point.buffer(1, mitre_limit=1, join_style=2)
119
+ ```
120
+
121
+ ## PyProj
122
+
123
+ PyProj handles coordinate transformations.
124
+
125
+ ```python
126
+ from pyproj import Transformer, CRS
127
+
128
+ # Coordinate transformation
129
+ transformer = Transformer.from_crs('EPSG:4326', 'EPSG:32633')
130
+ x, y = transformer.transform(lat, lon)
131
+ x_inv, y_inv = transformer.transform(x, y, direction='INVERSE')
132
+
133
+ # Batch transformation
134
+ lon_array = [-122.4, -122.3]
135
+ lat_array = [37.7, 37.8]
136
+ x_array, y_array = transformer.transform(lon_array, lat_array)
137
+
138
+ # Always z/height if available
139
+ transformer_always_z = Transformer.from_crs(
140
+ 'EPSG:4326', 'EPSG:32633', always_z=True
141
+ )
142
+
143
+ # Get CRS info
144
+ crs = CRS.from_epsg(4326)
145
+ print(crs.name) # WGS 84
146
+ print(crs.axis_info) # Axis info
147
+
148
+ # Custom transformation
149
+ transformer = Transformer.from_pipeline(
150
+ 'proj=pipeline step inv proj=utm zone=32 ellps=WGS84 step proj=unitconvert xy_in=rad xy_out=deg'
151
+ )
152
+ ```
153
+
154
+ ## GeoPandas
155
+
156
+ GeoPandas combines pandas with geospatial capabilities.
157
+
158
+ ```python
159
+ import geopandas as gpd
160
+
161
+ # Reading data
162
+ gdf = gpd.read_file('data.geojson')
163
+ gdf = gpd.read_file('data.shp', encoding='utf-8')
164
+ gdf = gpd.read_postgis('SELECT * FROM data', con=engine)
165
+
166
+ # Writing data
167
+ gdf.to_file('output.geojson', driver='GeoJSON')
168
+ gdf.to_file('output.gpkg', layer='data', use_arrow=True)
169
+
170
+ # CRS operations
171
+ gdf.crs # Get CRS
172
+ gdf = gdf.to_crs('EPSG:32633') # Reproject
173
+ gdf = gdf.set_crs('EPSG:4326') # Set CRS
174
+
175
+ # Geometric operations
176
+ gdf['area'] = gdf.geometry.area
177
+ gdf['length'] = gdf.geometry.length
178
+ gdf['buffer'] = gdf.geometry.buffer(100)
179
+ gdf['centroid'] = gdf.geometry.centroid
180
+
181
+ # Spatial joins
182
+ joined = gpd.sjoin(gdf1, gdf2, how='inner', predicate='intersects')
183
+ joined = gpd.sjoin_nearest(gdf1, gdf2, max_distance=1000)
184
+
185
+ # Overlay operations
186
+ intersection = gpd.overlay(gdf1, gdf2, how='intersection')
187
+ union = gpd.overlay(gdf1, gdf2, how='union')
188
+ difference = gpd.overlay(gdf1, gdf2, how='difference')
189
+
190
+ # Dissolve
191
+ dissolved = gdf.dissolve(by='region', aggfunc='sum')
192
+
193
+ # Clipping
194
+ clipped = gpd.clip(gdf, mask_gdf)
195
+
196
+ # Spatial indexing (for performance)
197
+ idx = gdf.sindex
198
+ possible_matches = idx.intersection(polygon.bounds)
199
+ ```
200
+
201
+ ## Common Workflows
202
+
203
+ ### Batch Reprojection
204
+
205
+ ```python
206
+ import geopandas as gpd
207
+ from pathlib import Path
208
+
209
+ input_dir = Path('input')
210
+ output_dir = Path('output')
211
+
212
+ for shp in input_dir.glob('*.shp'):
213
+ gdf = gpd.read_file(shp)
214
+ gdf = gdf.to_crs('EPSG:32633')
215
+ gdf.to_file(output_dir / shp.name)
216
+ ```
217
+
218
+ ### Raster to Vector Conversion
219
+
220
+ ```python
221
+ import rasterio.features
222
+ import geopandas as gpd
223
+ from shapely.geometry import shape
224
+
225
+ with rasterio.open('raster.tif') as src:
226
+ image = src.read(1)
227
+ results = (
228
+ {'properties': {'value': v}, 'geometry': s}
229
+ for s, v in rasterio.features.shapes(image, transform=src.transform)
230
+ )
231
+
232
+ geoms = list(results)
233
+ gdf = gpd.GeoDataFrame.from_features(geoms, crs=src.crs)
234
+ ```
235
+
236
+ ### Vector to Raster Conversion
237
+
238
+ ```python
239
+ from rasterio.features import rasterize
240
+ import geopandas as gpd
241
+
242
+ gdf = gpd.read_file('polygons.gpkg')
243
+ shapes = ((geom, 1) for geom in gdf.geometry)
244
+
245
+ raster = rasterize(
246
+ shapes,
247
+ out_shape=(height, width),
248
+ transform=transform,
249
+ fill=0,
250
+ dtype=np.uint8
251
+ )
252
+ ```
253
+
254
+ ### Combining Multiple Rasters
255
+
256
+ ```python
257
+ import rasterio.merge
258
+ import rasterio as rio
259
+
260
+ files = ['tile1.tif', 'tile2.tif', 'tile3.tif']
261
+ datasets = [rio.open(f) for f in files]
262
+
263
+ merged, transform = rasterio.merge.merge(datasets)
264
+
265
+ # Save
266
+ profile = datasets[0].profile
267
+ profile.update(transform=transform, height=merged.shape[1], width=merged.shape[2])
268
+
269
+ with rio.open('merged.tif', 'w', **profile) as dst:
270
+ dst.write(merged)
271
+ ```
272
+
273
+ For more detailed examples, see [code-examples.md](code-examples.md).