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.
- 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,363 @@
|
|
|
1
|
+
# Big Data and Cloud Computing
|
|
2
|
+
|
|
3
|
+
Distributed processing, cloud platforms, and GPU acceleration for geospatial data.
|
|
4
|
+
|
|
5
|
+
## Distributed Processing with Dask
|
|
6
|
+
|
|
7
|
+
### Dask-GeoPandas
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import dask_geopandas
|
|
11
|
+
import geopandas as gpd
|
|
12
|
+
import dask.dataframe as dd
|
|
13
|
+
|
|
14
|
+
# Read large GeoPackage in chunks
|
|
15
|
+
dask_gdf = dask_geopandas.read_file('large.gpkg', npartitions=10)
|
|
16
|
+
|
|
17
|
+
# Perform spatial operations
|
|
18
|
+
dask_gdf['area'] = dask_gdf.geometry.area
|
|
19
|
+
dask_gdf['buffer'] = dask_gdf.geometry.buffer(1000)
|
|
20
|
+
|
|
21
|
+
# Compute result
|
|
22
|
+
result = dask_gdf.compute()
|
|
23
|
+
|
|
24
|
+
# Distributed spatial join
|
|
25
|
+
dask_points = dask_geopandas.read_file('points.gpkg', npartitions=5)
|
|
26
|
+
dask_zones = dask_geopandas.read_file('zones.gpkg', npartitions=3)
|
|
27
|
+
|
|
28
|
+
joined = dask_points.sjoin(dask_zones, how='inner', predicate='within')
|
|
29
|
+
result = joined.compute()
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Dask for Raster Processing
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import dask.array as da
|
|
36
|
+
import rasterio
|
|
37
|
+
|
|
38
|
+
# Create lazy-loaded raster array
|
|
39
|
+
def lazy_raster(path, chunks=(1, 1024, 1024)):
|
|
40
|
+
with rasterio.open(path) as src:
|
|
41
|
+
profile = src.profile
|
|
42
|
+
# Create dask array
|
|
43
|
+
raster = da.from_rasterio(src, chunks=chunks)
|
|
44
|
+
|
|
45
|
+
return raster, profile
|
|
46
|
+
|
|
47
|
+
# Process large raster
|
|
48
|
+
raster, profile = lazy_raster('very_large.tif')
|
|
49
|
+
|
|
50
|
+
# Calculate NDVI (lazy operation)
|
|
51
|
+
ndvi = (raster[3] - raster[2]) / (raster[3] + raster[2] + 1e-8)
|
|
52
|
+
|
|
53
|
+
# Apply function to each chunk
|
|
54
|
+
def process_chunk(chunk):
|
|
55
|
+
return (chunk - chunk.min()) / (chunk.max() - chunk.min())
|
|
56
|
+
|
|
57
|
+
normalized = da.map_blocks(process_chunk, ndvi, dtype=np.float32)
|
|
58
|
+
|
|
59
|
+
# Compute and save
|
|
60
|
+
with rasterio.open('output.tif', 'w', **profile) as dst:
|
|
61
|
+
dst.write(normalized.compute())
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Dask Distributed Cluster
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from dask.distributed import Client
|
|
68
|
+
|
|
69
|
+
# Connect to cluster
|
|
70
|
+
client = Client('scheduler-address:8786')
|
|
71
|
+
|
|
72
|
+
# Or create local cluster
|
|
73
|
+
from dask.distributed import LocalCluster
|
|
74
|
+
cluster = LocalCluster(n_workers=4, threads_per_worker=2, memory_limit='4GB')
|
|
75
|
+
client = Client(cluster)
|
|
76
|
+
|
|
77
|
+
# Use Dask-GeoPandas with cluster
|
|
78
|
+
dask_gdf = dask_geopandas.from_geopandas(gdf, npartitions=10)
|
|
79
|
+
dask_gdf = dask_gdf.set_index(calculate_spatial_partitions=True)
|
|
80
|
+
|
|
81
|
+
# Operations are now distributed
|
|
82
|
+
result = dask_gdf.buffer(1000).compute()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Cloud Platforms
|
|
86
|
+
|
|
87
|
+
### Google Earth Engine
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
import ee
|
|
91
|
+
|
|
92
|
+
# Initialize
|
|
93
|
+
ee.Initialize(project='your-project')
|
|
94
|
+
|
|
95
|
+
# Large-scale composite
|
|
96
|
+
def create_annual_composite(year):
|
|
97
|
+
"""Create cloud-free annual composite."""
|
|
98
|
+
|
|
99
|
+
# Sentinel-2 collection
|
|
100
|
+
s2 = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED') \
|
|
101
|
+
.filterBounds(ee.Geometry.Rectangle([-125, 32, -114, 42])) \
|
|
102
|
+
.filterDate(f'{year}-01-01', f'{year}-12-31') \
|
|
103
|
+
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
|
|
104
|
+
|
|
105
|
+
# Cloud masking
|
|
106
|
+
def mask_s2(image):
|
|
107
|
+
qa = image.select('QA60')
|
|
108
|
+
cloud_bit_mask = 1 << 10
|
|
109
|
+
cirrus_bit_mask = 1 << 11
|
|
110
|
+
mask = qa.bitwiseAnd(cloud_bit_mask).eq(0).And(
|
|
111
|
+
qa.bitwiseAnd(cirrus_bit_mask).eq(0))
|
|
112
|
+
return image.updateMask(mask.Not())
|
|
113
|
+
|
|
114
|
+
s2_masked = s2.map(mask_s2)
|
|
115
|
+
|
|
116
|
+
# Median composite
|
|
117
|
+
composite = s2_masked.median().clip(roi)
|
|
118
|
+
|
|
119
|
+
return composite
|
|
120
|
+
|
|
121
|
+
# Export to Google Drive
|
|
122
|
+
task = ee.batch.Export.image.toDrive(
|
|
123
|
+
image=composite,
|
|
124
|
+
description='CA_composite_2023',
|
|
125
|
+
scale=10,
|
|
126
|
+
region=roi,
|
|
127
|
+
crs='EPSG:32611',
|
|
128
|
+
maxPixels=1e13
|
|
129
|
+
)
|
|
130
|
+
task.start()
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Planetary Computer (Microsoft)
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
import pystac_client
|
|
137
|
+
import planetary_computer
|
|
138
|
+
import odc.stac
|
|
139
|
+
import xarray as xr
|
|
140
|
+
|
|
141
|
+
# Search catalog
|
|
142
|
+
catalog = pystac_client.Client.open(
|
|
143
|
+
"https://planetarycomputer.microsoft.com/api/stac/v1",
|
|
144
|
+
modifier=planetary_computer.sign_inplace,
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
# Search NAIP imagery
|
|
148
|
+
search = catalog.search(
|
|
149
|
+
collections=["naip"],
|
|
150
|
+
bbox=[-125, 32, -114, 42],
|
|
151
|
+
datetime="2020-01-01/2023-12-31",
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
items = list(search.get_items())
|
|
155
|
+
|
|
156
|
+
# Load as xarray dataset
|
|
157
|
+
data = odc.stac.load(
|
|
158
|
+
items[:100], # Process in batches
|
|
159
|
+
bands=["image"],
|
|
160
|
+
crs="EPSG:32611",
|
|
161
|
+
resolution=1.0,
|
|
162
|
+
chunkx=1024,
|
|
163
|
+
chunky=1024,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
# Compute statistics lazily
|
|
167
|
+
mean = data.mean().compute()
|
|
168
|
+
std = data.std().compute()
|
|
169
|
+
|
|
170
|
+
# Export to COG
|
|
171
|
+
import rioxarray
|
|
172
|
+
data.isel(time=0).rio.to_raster('naip_composite.tif', compress='DEFLATE')
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Google Cloud Storage
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
from google.cloud import storage
|
|
179
|
+
import rasterio
|
|
180
|
+
from rasterio.session import GSSession
|
|
181
|
+
|
|
182
|
+
# Upload to GCS
|
|
183
|
+
client = storage.Client()
|
|
184
|
+
bucket = client.bucket('my-bucket')
|
|
185
|
+
blob = bucket.blob('geospatial/data.tif')
|
|
186
|
+
blob.upload_from_filename('local_data.tif')
|
|
187
|
+
|
|
188
|
+
# Read directly from GCS
|
|
189
|
+
with rasterio.open(
|
|
190
|
+
'gs://my-bucket/geospatial/data.tif',
|
|
191
|
+
session=GSSession()
|
|
192
|
+
) as src:
|
|
193
|
+
data = src.read()
|
|
194
|
+
|
|
195
|
+
# Use with Rioxarray
|
|
196
|
+
import rioxarray
|
|
197
|
+
da = rioxarray.open_rasterio('gs://my-bucket/geospatial/data.tif')
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## GPU Acceleration
|
|
201
|
+
|
|
202
|
+
### CuPy for Raster Processing
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
import cupy as cp
|
|
206
|
+
import numpy as np
|
|
207
|
+
|
|
208
|
+
def gpu_ndvi(nir, red):
|
|
209
|
+
"""Calculate NDVI on GPU."""
|
|
210
|
+
# Transfer to GPU
|
|
211
|
+
nir_gpu = cp.asarray(nir)
|
|
212
|
+
red_gpu = cp.asarray(red)
|
|
213
|
+
|
|
214
|
+
# Calculate on GPU
|
|
215
|
+
ndvi_gpu = (nir_gpu - red_gpu) / (nir_gpu + red_gpu + 1e-8)
|
|
216
|
+
|
|
217
|
+
# Transfer back
|
|
218
|
+
return cp.asnumpy(ndvi_gpu)
|
|
219
|
+
|
|
220
|
+
# Batch processing
|
|
221
|
+
def batch_process_gpu(raster_path):
|
|
222
|
+
with rasterio.open(raster_path) as src:
|
|
223
|
+
data = src.read() # (bands, height, width)
|
|
224
|
+
|
|
225
|
+
data_gpu = cp.asarray(data)
|
|
226
|
+
|
|
227
|
+
# Process all bands
|
|
228
|
+
for i in range(data.shape[0]):
|
|
229
|
+
data_gpu[i] = (data_gpu[i] - data_gpu[i].min()) / \
|
|
230
|
+
(data_gpu[i].max() - data_gpu[i].min())
|
|
231
|
+
|
|
232
|
+
return cp.asnumpy(data_gpu)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### RAPIDS for Spatial Analysis
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
import cudf
|
|
239
|
+
import cuspatial
|
|
240
|
+
|
|
241
|
+
# Load data to GPU
|
|
242
|
+
gdf_gpu = cuspatial.from_geopandas(gdf)
|
|
243
|
+
|
|
244
|
+
# Spatial join on GPU
|
|
245
|
+
points_gpu = cuspatial.from_geopandas(points_gdf)
|
|
246
|
+
polygons_gpu = cuspatial.from_geopandas(polygons_gdf)
|
|
247
|
+
|
|
248
|
+
joined = cuspatial.join_polygon_points(
|
|
249
|
+
polygons_gpu,
|
|
250
|
+
points_gpu
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
# Convert back
|
|
254
|
+
result = joined.to_pandas()
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### PyTorch for Geospatial Deep Learning
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
import torch
|
|
261
|
+
from torch.utils.data import DataLoader
|
|
262
|
+
|
|
263
|
+
# Custom dataset
|
|
264
|
+
class SatelliteDataset(torch.utils.data.Dataset):
|
|
265
|
+
def __init__(self, image_paths, label_paths):
|
|
266
|
+
self.image_paths = image_paths
|
|
267
|
+
self.label_paths = label_paths
|
|
268
|
+
|
|
269
|
+
def __getitem__(self, idx):
|
|
270
|
+
with rasterio.open(self.image_paths[idx]) as src:
|
|
271
|
+
image = src.read().astype(np.float32)
|
|
272
|
+
|
|
273
|
+
with rasterio.open(self.label_paths[idx]) as src:
|
|
274
|
+
label = src.read(1).astype(np.int64)
|
|
275
|
+
|
|
276
|
+
return torch.from_numpy(image), torch.from_numpy(label)
|
|
277
|
+
|
|
278
|
+
# DataLoader with GPU prefetching
|
|
279
|
+
dataset = SatelliteDataset(images, labels)
|
|
280
|
+
loader = DataLoader(
|
|
281
|
+
dataset,
|
|
282
|
+
batch_size=16,
|
|
283
|
+
shuffle=True,
|
|
284
|
+
num_workers=4,
|
|
285
|
+
pin_memory=True, # Faster transfer to GPU
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
# Training with mixed precision
|
|
289
|
+
from torch.cuda.amp import autocast, GradScaler
|
|
290
|
+
|
|
291
|
+
scaler = GradScaler()
|
|
292
|
+
|
|
293
|
+
for images, labels in loader:
|
|
294
|
+
images, labels = images.to('cuda'), labels.to('cuda')
|
|
295
|
+
|
|
296
|
+
with autocast():
|
|
297
|
+
outputs = model(images)
|
|
298
|
+
loss = criterion(outputs, labels)
|
|
299
|
+
|
|
300
|
+
scaler.scale(loss).backward()
|
|
301
|
+
scaler.step(optimizer)
|
|
302
|
+
scaler.update()
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## Efficient Data Formats
|
|
306
|
+
|
|
307
|
+
### Cloud-Optimized GeoTIFF (COG)
|
|
308
|
+
|
|
309
|
+
```python
|
|
310
|
+
from rio_cogeo.cogeo import cog_translate
|
|
311
|
+
|
|
312
|
+
# Convert to COG
|
|
313
|
+
cog_translate(
|
|
314
|
+
src_path='input.tif',
|
|
315
|
+
dst_path='output_cog.tif',
|
|
316
|
+
dst_kwds={'compress': 'DEFLATE', 'predictor': 2},
|
|
317
|
+
overview_level=5,
|
|
318
|
+
overview_resampling='average',
|
|
319
|
+
config={'GDAL_TIFF_INTERNAL_MASK': True}
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
# Create overviews for faster access
|
|
323
|
+
with rasterio.open('output.tif', 'r+') as src:
|
|
324
|
+
src.build_overviews([2, 4, 8, 16], resampling='average')
|
|
325
|
+
src.update_tags(ns='rio_overview', resampling='average')
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Zarr for Multidimensional Arrays
|
|
329
|
+
|
|
330
|
+
```python
|
|
331
|
+
import xarray as xr
|
|
332
|
+
import zarr
|
|
333
|
+
|
|
334
|
+
# Create Zarr store
|
|
335
|
+
store = zarr.DirectoryStore('data.zarr')
|
|
336
|
+
|
|
337
|
+
# Save datacube to Zarr
|
|
338
|
+
ds.to_zarr(store, consolidated=True)
|
|
339
|
+
|
|
340
|
+
# Read efficiently
|
|
341
|
+
ds = xr.open_zarr('data.zarr', consolidated=True)
|
|
342
|
+
|
|
343
|
+
# Extract subset efficiently
|
|
344
|
+
subset = ds.sel(time='2023-01', latitude=slice(30, 40))
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Parquet for Vector Data
|
|
348
|
+
|
|
349
|
+
```python
|
|
350
|
+
import geopandas as gpd
|
|
351
|
+
|
|
352
|
+
# Write to Parquet (with spatial index)
|
|
353
|
+
gdf.to_parquet('data.parquet', compression='snappy', index=True)
|
|
354
|
+
|
|
355
|
+
# Read efficiently
|
|
356
|
+
gdf = gpd.read_parquet('data.parquet')
|
|
357
|
+
|
|
358
|
+
# Read subset with filtering
|
|
359
|
+
import pyarrow.parquet as pq
|
|
360
|
+
table = pq.read_table('data.parquet', filters=[('column', '==', 'value')])
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
For more big data examples, see [code-examples.md](code-examples.md).
|