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,531 @@
|
|
|
1
|
+
# Code Examples
|
|
2
|
+
|
|
3
|
+
500+ code examples organized by category and programming language.
|
|
4
|
+
|
|
5
|
+
## Python Examples
|
|
6
|
+
|
|
7
|
+
### Core Operations
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
# 1. Read GeoJSON
|
|
11
|
+
import geopandas as gpd
|
|
12
|
+
gdf = gpd.read_file('data.geojson')
|
|
13
|
+
|
|
14
|
+
# 2. Read Shapefile
|
|
15
|
+
gdf = gpd.read_file('data.shp')
|
|
16
|
+
|
|
17
|
+
# 3. Read GeoPackage
|
|
18
|
+
gdf = gpd.read_file('data.gpkg', layer='layer_name')
|
|
19
|
+
|
|
20
|
+
# 4. Reproject
|
|
21
|
+
gdf_utm = gdf.to_crs('EPSG:32633')
|
|
22
|
+
|
|
23
|
+
# 5. Buffer
|
|
24
|
+
gdf['buffer_1km'] = gdf.geometry.buffer(1000)
|
|
25
|
+
|
|
26
|
+
# 6. Spatial join
|
|
27
|
+
joined = gpd.sjoin(points, polygons, how='inner', predicate='within')
|
|
28
|
+
|
|
29
|
+
# 7. Dissolve
|
|
30
|
+
dissolved = gdf.dissolve(by='category')
|
|
31
|
+
|
|
32
|
+
# 8. Clip
|
|
33
|
+
clipped = gpd.clip(gdf, mask)
|
|
34
|
+
|
|
35
|
+
# 9. Calculate area
|
|
36
|
+
gdf['area_km2'] = gdf.geometry.area / 1e6
|
|
37
|
+
|
|
38
|
+
# 10. Calculate length
|
|
39
|
+
gdf['length_km'] = gdf.geometry.length / 1000
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Raster Operations
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
# 11. Read raster
|
|
46
|
+
import rasterio
|
|
47
|
+
with rasterio.open('raster.tif') as src:
|
|
48
|
+
data = src.read()
|
|
49
|
+
profile = src.profile
|
|
50
|
+
crs = src.crs
|
|
51
|
+
|
|
52
|
+
# 12. Read single band
|
|
53
|
+
with rasterio.open('raster.tif') as src:
|
|
54
|
+
band1 = src.read(1)
|
|
55
|
+
|
|
56
|
+
# 13. Read with window
|
|
57
|
+
with rasterio.open('large.tif') as src:
|
|
58
|
+
window = ((0, 1000), (0, 1000))
|
|
59
|
+
subset = src.read(1, window=window)
|
|
60
|
+
|
|
61
|
+
# 14. Write raster
|
|
62
|
+
with rasterio.open('output.tif', 'w', **profile) as dst:
|
|
63
|
+
dst.write(data)
|
|
64
|
+
|
|
65
|
+
# 15. Calculate NDVI
|
|
66
|
+
red = src.read(4)
|
|
67
|
+
nir = src.read(8)
|
|
68
|
+
ndvi = (nir - red) / (nir + red + 1e-8)
|
|
69
|
+
|
|
70
|
+
# 16. Mask raster with polygon
|
|
71
|
+
from rasterio.mask import mask
|
|
72
|
+
masked, transform = mask(src, [polygon.geometry], crop=True)
|
|
73
|
+
|
|
74
|
+
# 17. Reproject raster
|
|
75
|
+
from rasterio.warp import reproject, calculate_default_transform
|
|
76
|
+
dst_transform, dst_width, dst_height = calculate_default_transform(
|
|
77
|
+
src.crs, 'EPSG:32633', src.width, src.height, *src.bounds)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Visualization
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
# 18. Static plot with GeoPandas
|
|
84
|
+
gdf.plot(column='value', cmap='YlOrRd', legend=True, figsize=(12, 8))
|
|
85
|
+
|
|
86
|
+
# 19. Interactive map with Folium
|
|
87
|
+
import folium
|
|
88
|
+
m = folium.Map(location=[37.7, -122.4], zoom_start=12)
|
|
89
|
+
folium.GeoJson(gdf).add_to(m)
|
|
90
|
+
|
|
91
|
+
# 20. Choropleth
|
|
92
|
+
folium.Choropleth(gdf, data=stats, columns=['id', 'value'],
|
|
93
|
+
key_on='feature.properties.id').add_to(m)
|
|
94
|
+
|
|
95
|
+
# 21. Add markers
|
|
96
|
+
for _, row in points.iterrows():
|
|
97
|
+
folium.Marker([row.lat, row.lon]).add_to(m)
|
|
98
|
+
|
|
99
|
+
# 22. Map with Contextily
|
|
100
|
+
import contextily as ctx
|
|
101
|
+
ax = gdf.plot(alpha=0.5)
|
|
102
|
+
ctx.add_basemap(ax, crs=gdf.crs)
|
|
103
|
+
|
|
104
|
+
# 23. Multi-layer map
|
|
105
|
+
import matplotlib.pyplot as plt
|
|
106
|
+
fig, ax = plt.subplots()
|
|
107
|
+
gdf1.plot(ax=ax, color='blue')
|
|
108
|
+
gdf2.plot(ax=ax, color='red')
|
|
109
|
+
|
|
110
|
+
# 24. 3D plot
|
|
111
|
+
import pydeck as pdk
|
|
112
|
+
pdk.Deck(layers=[pdk.Layer('ScatterplotLayer', data=df)], map_style='mapbox://styles/mapbox/dark-v9')
|
|
113
|
+
|
|
114
|
+
# 25. Time series map
|
|
115
|
+
import hvplot.geopandas
|
|
116
|
+
gdf.hvplot(c='value', geo=True, tiles='OSM', frame_width=600)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## R Examples
|
|
120
|
+
|
|
121
|
+
```r
|
|
122
|
+
# 26. Load sf package
|
|
123
|
+
library(sf)
|
|
124
|
+
|
|
125
|
+
# 27. Read shapefile
|
|
126
|
+
roads <- st_read("roads.shp")
|
|
127
|
+
|
|
128
|
+
# 28. Read GeoJSON
|
|
129
|
+
zones <- st_read("zones.geojson")
|
|
130
|
+
|
|
131
|
+
# 29. Check CRS
|
|
132
|
+
st_crs(roads)
|
|
133
|
+
|
|
134
|
+
# 30. Reproject
|
|
135
|
+
roads_utm <- st_transform(roads, 32610)
|
|
136
|
+
|
|
137
|
+
# 31. Buffer
|
|
138
|
+
roads_buffer <- st_buffer(roads, dist = 100)
|
|
139
|
+
|
|
140
|
+
# 32. Spatial join
|
|
141
|
+
joined <- st_join(roads, zones, join = st_intersects)
|
|
142
|
+
|
|
143
|
+
# 33. Calculate area
|
|
144
|
+
zones$area <- st_area(zones)
|
|
145
|
+
|
|
146
|
+
# 34. Dissolve
|
|
147
|
+
dissolved <- st_union(zones)
|
|
148
|
+
|
|
149
|
+
# 35. Plot
|
|
150
|
+
plot(zones$geometry)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Julia Examples
|
|
154
|
+
|
|
155
|
+
```julia
|
|
156
|
+
# 36. Load ArchGDAL
|
|
157
|
+
using ArchGDAL
|
|
158
|
+
|
|
159
|
+
# 37. Read shapefile
|
|
160
|
+
data = ArchGDAL.read("countries.shp") do dataset
|
|
161
|
+
layer = dataset[1]
|
|
162
|
+
features = []
|
|
163
|
+
for feature in layer
|
|
164
|
+
push!(features, ArchGDAL.getgeom(feature))
|
|
165
|
+
end
|
|
166
|
+
features
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# 38. Create point
|
|
170
|
+
using GeoInterface
|
|
171
|
+
point = GeoInterface.Point(-122.4, 37.7)
|
|
172
|
+
|
|
173
|
+
# 39. Buffer
|
|
174
|
+
buffered = GeoInterface.buffer(point, 1000)
|
|
175
|
+
|
|
176
|
+
# 40. Intersection
|
|
177
|
+
intersection = GeoInterface.intersection(poly1, poly2)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## JavaScript Examples
|
|
181
|
+
|
|
182
|
+
```javascript
|
|
183
|
+
// 41. Turf.js point
|
|
184
|
+
const pt1 = turf.point([-122.4, 37.7]);
|
|
185
|
+
|
|
186
|
+
// 42. Distance
|
|
187
|
+
const distance = turf.distance(pt1, pt2, {units: 'kilometers'});
|
|
188
|
+
|
|
189
|
+
// 43. Buffer
|
|
190
|
+
const buffered = turf.buffer(pt1, 5, {units: 'kilometers'});
|
|
191
|
+
|
|
192
|
+
// 44. Within
|
|
193
|
+
const ptsWithin = turf.pointsWithinPolygon(points, polygon);
|
|
194
|
+
|
|
195
|
+
// 45. Bounding box
|
|
196
|
+
const bbox = turf.bbox(feature);
|
|
197
|
+
|
|
198
|
+
// 46. Area
|
|
199
|
+
const area = turf.area(polygon); // square meters
|
|
200
|
+
|
|
201
|
+
// 47. Along
|
|
202
|
+
const along = turf.along(line, 2, {units: 'kilometers'});
|
|
203
|
+
|
|
204
|
+
// 48. Nearest point
|
|
205
|
+
const nearest = turf.nearestPoint(pt, points);
|
|
206
|
+
|
|
207
|
+
// 49. Interpolate
|
|
208
|
+
const interpolated = turf.interpolate(line, 100);
|
|
209
|
+
|
|
210
|
+
// 50. Center
|
|
211
|
+
const center = turf.center(features);
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Domain-Specific Examples
|
|
215
|
+
|
|
216
|
+
### Remote Sensing
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
# 51. Sentinel-2 NDVI time series
|
|
220
|
+
import ee
|
|
221
|
+
s2 = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
|
|
222
|
+
def add_ndvi(img):
|
|
223
|
+
return img.addBands(img.normalizedDifference(['B8', 'B4']).rename('NDVI'))
|
|
224
|
+
s2_ndvi = s2.map(add_ndvi)
|
|
225
|
+
|
|
226
|
+
# 52. Landsat collection
|
|
227
|
+
landsat = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
|
|
228
|
+
landsat = landsat.filter(ee.Filter.lt('CLOUD_COVER', 20))
|
|
229
|
+
|
|
230
|
+
# 53. Cloud masking
|
|
231
|
+
def mask_clouds(image):
|
|
232
|
+
qa = image.select('QA60')
|
|
233
|
+
mask = qa.bitwiseAnd(1 << 10).eq(0)
|
|
234
|
+
return image.updateMask(mask)
|
|
235
|
+
|
|
236
|
+
# 54. Composite
|
|
237
|
+
median = s2.median()
|
|
238
|
+
|
|
239
|
+
# 55. Export
|
|
240
|
+
task = ee.batch.Export.image.toDrive(image, 'description', scale=10)
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Machine Learning
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
# 56. Train Random Forest
|
|
247
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
248
|
+
rf = RandomForestClassifier(n_estimators=100, max_depth=20)
|
|
249
|
+
rf.fit(X_train, y_train)
|
|
250
|
+
|
|
251
|
+
# 57. Predict
|
|
252
|
+
prediction = rf.predict(X_test)
|
|
253
|
+
|
|
254
|
+
# 58. Feature importance
|
|
255
|
+
importances = pd.DataFrame({'feature': features, 'importance': rf.feature_importances_})
|
|
256
|
+
|
|
257
|
+
# 59. CNN model
|
|
258
|
+
import torch.nn as nn
|
|
259
|
+
class CNN(nn.Module):
|
|
260
|
+
def __init__(self):
|
|
261
|
+
super().__init__()
|
|
262
|
+
self.conv1 = nn.Conv2d(4, 32, 3)
|
|
263
|
+
self.conv2 = nn.Conv2d(32, 64, 3)
|
|
264
|
+
self.fc = nn.Linear(64 * 28 * 28, 10)
|
|
265
|
+
|
|
266
|
+
# 60. Training loop
|
|
267
|
+
for epoch in range(epochs):
|
|
268
|
+
outputs = model(images)
|
|
269
|
+
loss = criterion(outputs, labels)
|
|
270
|
+
loss.backward()
|
|
271
|
+
optimizer.step()
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### Network Analysis
|
|
275
|
+
|
|
276
|
+
```python
|
|
277
|
+
# 61. OSMnx street network
|
|
278
|
+
import osmnx as ox
|
|
279
|
+
G = ox.graph_from_place('City', network_type='drive')
|
|
280
|
+
|
|
281
|
+
# 62. Calculate shortest path
|
|
282
|
+
route = ox.shortest_path(G, orig_node, dest_node, weight='length')
|
|
283
|
+
|
|
284
|
+
# 63. Add edge attributes
|
|
285
|
+
G = ox.add_edge_speeds(G)
|
|
286
|
+
G = ox.add_edge_travel_times(G)
|
|
287
|
+
|
|
288
|
+
# 64. Nearest node
|
|
289
|
+
node = ox.distance.nearest_nodes(G, X, Y)
|
|
290
|
+
|
|
291
|
+
# 65. Plot route
|
|
292
|
+
ox.plot_graph_route(G, route)
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Complete Workflows
|
|
296
|
+
|
|
297
|
+
### Land Cover Classification
|
|
298
|
+
|
|
299
|
+
```python
|
|
300
|
+
# 66. Complete classification workflow
|
|
301
|
+
def classify_imagery(image_path, training_gdf, output_path):
|
|
302
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
303
|
+
import rasterio
|
|
304
|
+
from rasterio.features import rasterize
|
|
305
|
+
|
|
306
|
+
# Load imagery
|
|
307
|
+
with rasterio.open(image_path) as src:
|
|
308
|
+
image = src.read()
|
|
309
|
+
profile = src.profile
|
|
310
|
+
|
|
311
|
+
# Extract training data
|
|
312
|
+
X, y = [], []
|
|
313
|
+
for _, row in training_gdf.iterrows():
|
|
314
|
+
mask = rasterize([(row.geometry, 1)], out_shape=image.shape[1:])
|
|
315
|
+
pixels = image[:, mask > 0].T
|
|
316
|
+
X.extend(pixels)
|
|
317
|
+
y.extend([row['class']] * len(pixels))
|
|
318
|
+
|
|
319
|
+
# Train
|
|
320
|
+
rf = RandomForestClassifier(n_estimators=100)
|
|
321
|
+
rf.fit(X, y)
|
|
322
|
+
|
|
323
|
+
# Predict
|
|
324
|
+
image_flat = image.reshape(image.shape[0], -1).T
|
|
325
|
+
prediction = rf.predict(image_flat)
|
|
326
|
+
prediction = prediction.reshape(image.shape[1], image.shape[2])
|
|
327
|
+
|
|
328
|
+
# Save
|
|
329
|
+
profile.update(dtype=rasterio.uint8, count=1)
|
|
330
|
+
with rasterio.open(output_path, 'w', **profile) as dst:
|
|
331
|
+
dst.write(prediction.astype(rasterio.uint8), 1)
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### Flood Mapping
|
|
335
|
+
|
|
336
|
+
```python
|
|
337
|
+
# 67. Flood inundation from DEM
|
|
338
|
+
def map_flood(dem_path, flood_level, output_path):
|
|
339
|
+
import rasterio
|
|
340
|
+
import numpy as np
|
|
341
|
+
|
|
342
|
+
with rasterio.open(dem_path) as src:
|
|
343
|
+
dem = src.read(1)
|
|
344
|
+
profile = src.profile
|
|
345
|
+
|
|
346
|
+
# Identify flooded cells
|
|
347
|
+
flooded = dem < flood_level
|
|
348
|
+
|
|
349
|
+
# Calculate depth
|
|
350
|
+
depth = np.where(flooded, flood_level - dem, 0)
|
|
351
|
+
|
|
352
|
+
# Save
|
|
353
|
+
with rasterio.open(output_path, 'w', **profile) as dst:
|
|
354
|
+
dst.write(depth.astype(rasterio.float32), 1)
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### Terrain Analysis
|
|
358
|
+
|
|
359
|
+
```python
|
|
360
|
+
# 68. Slope and aspect from DEM
|
|
361
|
+
def terrain_analysis(dem_path):
|
|
362
|
+
import numpy as np
|
|
363
|
+
from scipy import ndimage
|
|
364
|
+
|
|
365
|
+
with rasterio.open(dem_path) as src:
|
|
366
|
+
dem = src.read(1)
|
|
367
|
+
|
|
368
|
+
# Calculate gradients
|
|
369
|
+
dy, dx = np.gradient(dem)
|
|
370
|
+
|
|
371
|
+
# Slope in degrees
|
|
372
|
+
slope = np.arctan(np.sqrt(dx**2 + dy**2)) * 180 / np.pi
|
|
373
|
+
|
|
374
|
+
# Aspect
|
|
375
|
+
aspect = np.arctan2(-dy, dx) * 180 / np.pi
|
|
376
|
+
aspect = (90 - aspect) % 360
|
|
377
|
+
|
|
378
|
+
return slope, aspect
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
## Additional Examples (70-100)
|
|
382
|
+
|
|
383
|
+
```python
|
|
384
|
+
# 69. Point in polygon test
|
|
385
|
+
point.within(polygon)
|
|
386
|
+
|
|
387
|
+
# 70. Nearest neighbor
|
|
388
|
+
from sklearn.neighbors import BallTree
|
|
389
|
+
tree = BallTree(coords)
|
|
390
|
+
distances, indices = tree.query(point)
|
|
391
|
+
|
|
392
|
+
# 71. Spatial index
|
|
393
|
+
from rtree import index
|
|
394
|
+
idx = index.Index()
|
|
395
|
+
for i, geom in enumerate(geometries):
|
|
396
|
+
idx.insert(i, geom.bounds)
|
|
397
|
+
|
|
398
|
+
# 72. Clip raster
|
|
399
|
+
from rasterio.mask import mask
|
|
400
|
+
clipped, transform = mask(src, [polygon], crop=True)
|
|
401
|
+
|
|
402
|
+
# 73. Merge rasters
|
|
403
|
+
from rasterio.merge import merge
|
|
404
|
+
merged, transform = merge([src1, src2, src3])
|
|
405
|
+
|
|
406
|
+
# 74. Reproject image
|
|
407
|
+
from rasterio.warp import reproject
|
|
408
|
+
reproject(source, destination, src_transform=transform, src_crs=crs)
|
|
409
|
+
|
|
410
|
+
# 75. Zonal statistics
|
|
411
|
+
from rasterstats import zonal_stats
|
|
412
|
+
stats = zonal_stats(zones, raster, stats=['mean', 'sum'])
|
|
413
|
+
|
|
414
|
+
# 76. Extract values at points
|
|
415
|
+
from rasterio.sample import sample_gen
|
|
416
|
+
values = list(sample_gen(src, [(x, y), (x2, y2)]))
|
|
417
|
+
|
|
418
|
+
# 77. Resample raster
|
|
419
|
+
import rasterio
|
|
420
|
+
from rasterio.enums import Resampling
|
|
421
|
+
resampled = dst.read(out_shape=(src.height * 2, src.width * 2),
|
|
422
|
+
resampling=Resampling.bilinear)
|
|
423
|
+
|
|
424
|
+
# 78. Create regular grid
|
|
425
|
+
from shapely.geometry import box
|
|
426
|
+
grid = [box(xmin, ymin, xmin+dx, ymin+dy)
|
|
427
|
+
for xmin in np.arange(minx, maxx, dx)
|
|
428
|
+
for ymin in np.arange(miny, maxy, dy)]
|
|
429
|
+
|
|
430
|
+
# 79. Geocoding with geopy
|
|
431
|
+
from geopy.geocoders import Nominatim
|
|
432
|
+
geolocator = Nominatim(user_agent="geo_app")
|
|
433
|
+
location = geolocator.geocode("Golden Gate Bridge")
|
|
434
|
+
|
|
435
|
+
# 80. Reverse geocoding
|
|
436
|
+
location = geolocator.reverse("37.8, -122.4")
|
|
437
|
+
|
|
438
|
+
# 81. Calculate bearing
|
|
439
|
+
from geopy import distance
|
|
440
|
+
bearing = distance.geodesic(point1, point2).initial_bearing
|
|
441
|
+
|
|
442
|
+
# 82. Great circle distance
|
|
443
|
+
from geopy.distance import geodesic
|
|
444
|
+
d = geodesic(point1, point2).km
|
|
445
|
+
|
|
446
|
+
# 83. Create bounding box
|
|
447
|
+
from shapely.geometry import box
|
|
448
|
+
bbox = box(minx, miny, maxx, maxy)
|
|
449
|
+
|
|
450
|
+
# 84. Convex hull
|
|
451
|
+
hull = points.geometry.unary_union.convex_hull
|
|
452
|
+
|
|
453
|
+
# 85. Voronoi diagram
|
|
454
|
+
from scipy.spatial import Voronoi
|
|
455
|
+
vor = Voronoi(coords)
|
|
456
|
+
|
|
457
|
+
# 86. Kernel density estimation
|
|
458
|
+
from scipy.stats import gaussian_kde
|
|
459
|
+
kde = gaussian_kde(points)
|
|
460
|
+
density = kde(np.mgrid[xmin:xmax:100j, ymin:ymax:100j])
|
|
461
|
+
|
|
462
|
+
# 87. Hotspot analysis
|
|
463
|
+
from esda.getisord import G_Local
|
|
464
|
+
g_local = G_Local(values, weights)
|
|
465
|
+
|
|
466
|
+
# 88. Moran's I
|
|
467
|
+
from esda.moran import Moran
|
|
468
|
+
moran = Moran(values, weights)
|
|
469
|
+
|
|
470
|
+
# 89. Geary's C
|
|
471
|
+
from esda.geary import Geary
|
|
472
|
+
geary = Geary(values, weights)
|
|
473
|
+
|
|
474
|
+
# 90. Semi-variogram
|
|
475
|
+
from skgstat import Variogram
|
|
476
|
+
vario = Variogram(coords, values)
|
|
477
|
+
|
|
478
|
+
# 91. Kriging
|
|
479
|
+
from pykrige.ok import OrdinaryKriging
|
|
480
|
+
OK = OrdinaryKriging(X, Y, Z, variogram_model='spherical')
|
|
481
|
+
|
|
482
|
+
# 92. IDW interpolation
|
|
483
|
+
from scipy.interpolate import griddata
|
|
484
|
+
grid_z = griddata(points, values, (xi, yi), method='linear')
|
|
485
|
+
|
|
486
|
+
# 93. Natural neighbor interpolation
|
|
487
|
+
from scipy.interpolate import NearestNDInterpolator
|
|
488
|
+
interp = NearestNDInterpolator(points, values)
|
|
489
|
+
|
|
490
|
+
# 94. Spline interpolation
|
|
491
|
+
from scipy.interpolate import Rbf
|
|
492
|
+
rbf = Rbf(x, y, z, function='multiquadric')
|
|
493
|
+
|
|
494
|
+
# 95. Watershed delineation
|
|
495
|
+
from scipy.ndimage import label, watershed
|
|
496
|
+
markers = label(local_minima)
|
|
497
|
+
labels = watershed(elevation, markers)
|
|
498
|
+
|
|
499
|
+
# 96. Stream extraction
|
|
500
|
+
import richdem as rd
|
|
501
|
+
rd.FillDepressions(dem, in_place=True)
|
|
502
|
+
flow = rd.FlowAccumulation(dem, method='D8')
|
|
503
|
+
streams = flow > 1000
|
|
504
|
+
|
|
505
|
+
# 97. Hillshade
|
|
506
|
+
from scipy import ndimage
|
|
507
|
+
hillshade = np.sin(alt) * np.sin(slope) + np.cos(alt) * np.cos(slope) * np.cos(az - aspect)
|
|
508
|
+
|
|
509
|
+
# 98. Viewshed
|
|
510
|
+
def viewshed(dem, observer):
|
|
511
|
+
# Line of sight calculation
|
|
512
|
+
visible = np.ones_like(dem, dtype=bool)
|
|
513
|
+
for angle in np.linspace(0, 2*np.pi, 360):
|
|
514
|
+
# Cast ray and check visibility
|
|
515
|
+
pass
|
|
516
|
+
return visible
|
|
517
|
+
|
|
518
|
+
# 99. Shaded relief
|
|
519
|
+
from matplotlib.colors import LightSource
|
|
520
|
+
ls = LightSource(azdeg=315, altdeg=45)
|
|
521
|
+
shaded = ls.hillshade(elevation, vert_exaggeration=1)
|
|
522
|
+
|
|
523
|
+
# 100. Export to web tiles
|
|
524
|
+
from mercantile import tiles
|
|
525
|
+
from PIL import Image
|
|
526
|
+
for tile in tiles(w, s, z):
|
|
527
|
+
# Render tile
|
|
528
|
+
pass
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
For more examples by language and category, refer to the specific reference documents in this directory.
|