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,494 @@
1
+ ---
2
+ name: design-postgis-tables
3
+ description: Comprehensive PostGIS spatial table design reference covering geometry types, coordinate systems, spatial indexing, and performance patterns for location-based applications
4
+ license: Apache-2.0
5
+ compatibility: Requires PostgreSQL 15+ with the PostGIS extension
6
+ metadata:
7
+ author: tigerdata
8
+ ---
9
+
10
+ # PostGIS Spatial Table Design
11
+
12
+ ## Before You Start (5 Questions)
13
+
14
+ 1. What is the geographic scope (single city/region vs global)?
15
+ 2. What are your primary query patterns (within-radius, bbox, intersects, nearest-neighbor)?
16
+ 3. What units do you need for distance/area (meters vs CRS units), and how accurate must they be?
17
+ 4. What is the expected scale (rows, write rate), and is the data mostly append-only?
18
+ 5. Do you need 3D (Z) or measures (M), or is 2D enough?
19
+
20
+ **SQL injection note:** When turning these patterns into application code, use parameterized queries for user-provided values (WKT/WKB, coordinates, IDs, radii). Avoid string-concatenating untrusted input into SQL; for dynamic identifiers, use safe identifier quoting/whitelisting.
21
+
22
+ ## Core Rules
23
+
24
+ - **Always use PostGIS geometry/geography types** instead of PostgreSQL's built-in geometric types (`POINT`, `LINE`, `POLYGON`, `CIRCLE`). PostGIS types provide true spatial capabilities.
25
+ - **Choose between GEOMETRY and GEOGRAPHY** based on your use case: GEOMETRY for projected/local data with Cartesian math; GEOGRAPHY for global data requiring accurate spherical calculations.
26
+ - **Always specify SRID** (Spatial Reference Identifier) when creating geometry columns. Use `4326` (WGS84) for GPS/global data, appropriate local projections for regional data.
27
+ - **Create spatial indexes** on all geometry/geography columns using GiST (default). Consider BRIN only for very large **GEOMETRY** tables where rows are naturally ordered on disk and you can tolerate coarser filtering.
28
+ - **Use constraint-based type enforcement** with `GEOMETRY(type, SRID)` syntax to ensure data integrity.
29
+
30
+ ## Geometry vs Geography
31
+
32
+ ### When to Use GEOMETRY
33
+
34
+ - **Local/regional data** within a single coordinate system
35
+ - **Projected coordinates** (meters, feet) for accurate area/distance calculations
36
+ - **Complex spatial operations** (buffering, unions, intersections)
37
+ - **Performance-critical queries** (Cartesian math is faster)
38
+ - **Data already in a projected CRS** (UTM, State Plane, etc.)
39
+
40
+ ```sql
41
+ -- Regional data with projected coordinates (UTM Zone 10N for California)
42
+ CREATE TABLE local_parcels (
43
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
44
+ parcel_number TEXT NOT NULL,
45
+ boundary GEOMETRY(POLYGON, 26910), -- UTM Zone 10N (meters)
46
+ area_sqm DOUBLE PRECISION GENERATED ALWAYS AS (ST_Area(boundary)) STORED
47
+ );
48
+ ```
49
+
50
+ ### When to Use GEOGRAPHY
51
+
52
+ - **Global data** spanning multiple continents/hemispheres
53
+ - **GPS coordinates** (latitude/longitude in decimal degrees)
54
+ - **Accurate distance calculations** on Earth's surface (great circle)
55
+ - **Simple spatial operations** (distance, containment)
56
+ - **Data from GPS devices, geocoding services, or web maps**
57
+
58
+ ```sql
59
+ -- Global data with geodetic calculations
60
+ CREATE TABLE global_offices (
61
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
62
+ name TEXT NOT NULL,
63
+ city TEXT NOT NULL,
64
+ location GEOGRAPHY(POINT, 4326) -- WGS84 (lat/lon)
65
+ );
66
+
67
+ -- Distance in meters (accurate spherical calculation)
68
+ SELECT
69
+ a.name AS office_a,
70
+ b.name AS office_b,
71
+ ST_Distance(a.location, b.location) / 1000 AS distance_km
72
+ FROM global_offices a
73
+ CROSS JOIN global_offices b
74
+ WHERE a.id < b.id;
75
+ ```
76
+
77
+ ### Comparison Table
78
+
79
+ | Aspect | GEOMETRY | GEOGRAPHY |
80
+ | ----------------- | ------------------------------------- | ------------------------- |
81
+ | Coordinate system | Any SRID (projected or geodetic) | WGS84 (SRID 4326) only |
82
+ | Distance units | CRS units (degrees, meters, feet) | Meters (always) |
83
+ | Distance accuracy | Depends on projection | True spheroidal distance |
84
+ | Area accuracy | Accurate in projected CRS | Accurate on sphere |
85
+ | Function support | Full (300+ functions) | Limited (~40 functions) |
86
+ | Performance | Faster (Cartesian math) | Slower (spherical math) |
87
+ | Index type | GiST, BRIN, SP-GiST | GiST only |
88
+ | Best for | Regional/local data, complex analysis | Global data, GPS tracking |
89
+
90
+ ## Geometry Types
91
+
92
+ ### Point Types
93
+
94
+ ```sql
95
+ -- Single location (stores, sensors, events)
96
+ location GEOMETRY(POINT, 4326)
97
+
98
+ -- Multiple discrete locations (multi-branch business)
99
+ locations GEOMETRY(MULTIPOINT, 4326)
100
+
101
+ -- 3D point with elevation
102
+ location_3d GEOMETRY(POINTZ, 4326)
103
+
104
+ -- Point with measure value (linear referencing)
105
+ location_m GEOMETRY(POINTM, 4326)
106
+ ```
107
+
108
+ **Use POINT for:** Store locations, sensor positions, event coordinates, addresses, POIs
109
+ **Use MULTIPOINT for:** Multiple related locations stored as single feature
110
+
111
+ ### Line Types
112
+
113
+ ```sql
114
+ -- Single path (road segment, river, route)
115
+ path GEOMETRY(LINESTRING, 4326)
116
+
117
+ -- Multiple paths (road network, transit lines)
118
+ network GEOMETRY(MULTILINESTRING, 4326)
119
+
120
+ -- 3D line with elevation profile
121
+ trail_3d GEOMETRY(LINESTRINGZ, 4326)
122
+ ```
123
+
124
+ **Use LINESTRING for:** Roads, rivers, pipelines, GPS tracks, routes
125
+ **Use MULTILINESTRING for:** Disconnected road segments, river systems
126
+
127
+ ### Polygon Types
128
+
129
+ ```sql
130
+ -- Single area (parcel, building footprint, zone)
131
+ boundary GEOMETRY(POLYGON, 4326)
132
+
133
+ -- Multiple areas (archipelago, fragmented habitat)
134
+ territories GEOMETRY(MULTIPOLYGON, 4326)
135
+
136
+ -- 3D polygon (building with height)
137
+ footprint_3d GEOMETRY(POLYGONZ, 4326)
138
+ ```
139
+
140
+ **Use POLYGON for:** Property boundaries, administrative areas, service zones
141
+ **Use MULTIPOLYGON for:** Countries with islands, fragmented regions
142
+
143
+ ### Generic Types
144
+
145
+ ```sql
146
+ -- Any geometry type (flexible schema)
147
+ geom GEOMETRY(GEOMETRY, 4326)
148
+
149
+ -- Collection of mixed types
150
+ features GEOMETRY(GEOMETRYCOLLECTION, 4326)
151
+ ```
152
+
153
+ **Use GEOMETRY for:** Flexible schemas accepting multiple types
154
+ **Avoid GEOMETRYCOLLECTION:** Prefer homogeneous types for better indexing
155
+
156
+ ## Coordinate Systems (SRID)
157
+
158
+ ### Common SRIDs
159
+
160
+ | SRID | Name | Use Case | Units |
161
+ | ----------- | ----------------- | ---------------------------- | ------- |
162
+ | 4326 | WGS84 | GPS, global data, web maps | Degrees |
163
+ | 3857 | Web Mercator | Web map tiles (display only) | Meters |
164
+ | 26910-26919 | UTM Zones (US) | Regional analysis | Meters |
165
+ | 32601-32660 | UTM Zones (North) | Regional analysis | Meters |
166
+ | 32701-32760 | UTM Zones (South) | Regional analysis | Meters |
167
+
168
+ ### SRID Best Practices
169
+
170
+ - **Store in WGS84 (4326)** for interoperability and GPS data
171
+ - **Transform to projected CRS** for accurate measurements
172
+ - **Never mix SRIDs** in spatial operations without explicit transformation
173
+ - **Use appropriate local CRS** for area/distance calculations requiring high precision
174
+
175
+ ```sql
176
+ -- Store in WGS84, calculate in UTM
177
+ CREATE TABLE survey_points (
178
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
179
+ location GEOMETRY(POINT, 4326), -- Storage: WGS84
180
+ CONSTRAINT valid_location CHECK (ST_IsValid(location))
181
+ );
182
+
183
+ -- Calculate distance in meters using UTM projection
184
+ SELECT
185
+ a.id AS point_a,
186
+ b.id AS point_b,
187
+ ST_Distance(
188
+ ST_Transform(a.location, 26910), -- Transform to UTM
189
+ ST_Transform(b.location, 26910)
190
+ ) AS distance_meters
191
+ FROM survey_points a
192
+ CROSS JOIN survey_points b
193
+ WHERE a.id < b.id;
194
+ ```
195
+
196
+ ## Spatial Indexing
197
+
198
+ ### GiST Index (Default)
199
+
200
+ Most versatile spatial index. Use for all geometry/geography columns.
201
+
202
+ ```sql
203
+ -- Geometry (most common)
204
+ CREATE INDEX idx_your_table_geom_gist ON your_table_name USING GIST (geom);
205
+
206
+ -- Geography (GiST is the supported option)
207
+ CREATE INDEX idx_your_table_geog_gist ON your_table_name USING GIST (geog);
208
+
209
+ -- Analyze after index creation
210
+ VACUUM ANALYZE your_table_name;
211
+ ```
212
+
213
+ **Supports:** All spatial operators (`&&`, `@>`, `<@`, `~=`, `<->`)
214
+ **Best for:** General-purpose spatial queries, mixed query patterns
215
+
216
+ ### BRIN Index
217
+
218
+ Block Range Index for very large, naturally ordered datasets.
219
+
220
+ ```sql
221
+ -- BRIN for very large, append-only GEOMETRY tables (geography uses GiST)
222
+ CREATE INDEX idx_your_table_geom_brin
223
+ ON your_table_name
224
+ USING BRIN (geom)
225
+ WITH (pages_per_range = 128);
226
+ ```
227
+
228
+ **Supports:** Bounding box operators (`&&`, `@>`, `<@`)
229
+ **Best for:** Append-only tables, time-series spatial data, very large datasets (>100M rows)
230
+ **Trade-off:** Much smaller than GiST, but less precise filtering
231
+
232
+ ### SP-GiST Index
233
+
234
+ Space-partitioned GiST for point data with specific distributions.
235
+
236
+ ```sql
237
+ -- SP-GiST for GEOMETRY(POINT, ...) only
238
+ CREATE INDEX idx_sensors_location_spgist
239
+ ON sensors
240
+ USING SPGIST (location);
241
+ ```
242
+
243
+ **Best for:** Point-only data, quadtree-friendly distributions
244
+ **Not for:** Complex geometries, mixed types
245
+
246
+ ### Index Selection Guide
247
+
248
+ | Scenario | Index Type | Reasoning |
249
+ | -------------------------------- | ------------- | ------------------------------------------ |
250
+ | General spatial queries | GiST | Most versatile, supports all operators |
251
+ | Very large, append-only | BRIN | Tiny footprint, good for time-ordered data |
252
+ | Point-only, uniform distribution | SP-GiST | Efficient for point lookups |
253
+ | Geography columns | GiST | Only supported option |
254
+ | Composite spatial + attribute | GiST + B-tree | Separate indexes or expression index |
255
+
256
+ ## Table Design Examples
257
+
258
+ ### Points of Interest (POI)
259
+
260
+ ```sql
261
+ CREATE TABLE pois (
262
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
263
+ name TEXT NOT NULL,
264
+ category TEXT NOT NULL,
265
+ location GEOGRAPHY(POINT, 4326) NOT NULL,
266
+ address TEXT,
267
+ metadata JSONB DEFAULT '{}',
268
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
269
+ CONSTRAINT valid_category CHECK (category IN (
270
+ 'restaurant', 'hotel', 'gas_station', 'hospital', 'school'
271
+ ))
272
+ );
273
+
274
+ -- Spatial index
275
+ CREATE INDEX idx_pois_location ON pois USING GIST (location);
276
+
277
+ -- Category + location for filtered spatial queries
278
+ CREATE INDEX idx_pois_category ON pois (category);
279
+
280
+ -- Find restaurants within 1km
281
+ SELECT name, address,
282
+ ST_Distance(
283
+ location,
284
+ ST_SetSRID(ST_MakePoint(-122.4194, 37.7749), 4326)::GEOGRAPHY
285
+ ) AS distance_m
286
+ FROM pois
287
+ WHERE category = 'restaurant'
288
+ AND ST_DWithin(
289
+ location,
290
+ ST_SetSRID(ST_MakePoint(-122.4194, 37.7749), 4326)::GEOGRAPHY,
291
+ 1000
292
+ )
293
+ ORDER BY distance_m;
294
+ ```
295
+
296
+ ### Property Parcels
297
+
298
+ ```sql
299
+ CREATE TABLE parcels (
300
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
301
+ parcel_id TEXT NOT NULL UNIQUE,
302
+ owner_name TEXT,
303
+ boundary GEOMETRY(MULTIPOLYGON, 4326) NOT NULL,
304
+ centroid GEOMETRY(POINT, 4326) GENERATED ALWAYS AS (ST_Centroid(boundary)) STORED,
305
+ area_sqm DOUBLE PRECISION GENERATED ALWAYS AS (
306
+ ST_Area(boundary::GEOGRAPHY)
307
+ ) STORED,
308
+ perimeter_m DOUBLE PRECISION GENERATED ALWAYS AS (
309
+ ST_Perimeter(boundary::GEOGRAPHY)
310
+ ) STORED,
311
+ CONSTRAINT valid_boundary CHECK (ST_IsValid(boundary)),
312
+ CONSTRAINT closed_boundary CHECK (ST_IsClosed(ST_ExteriorRing(ST_GeometryN(boundary, 1))))
313
+ );
314
+
315
+ CREATE INDEX idx_parcels_boundary ON parcels USING GIST (boundary);
316
+ CREATE INDEX idx_parcels_centroid ON parcels USING GIST (centroid);
317
+
318
+ -- Find parcels intersecting a search area
319
+ SELECT parcel_id, owner_name, area_sqm
320
+ FROM parcels
321
+ WHERE ST_Intersects(boundary, ST_MakeEnvelope(-122.5, 37.7, -122.4, 37.8, 4326));
322
+ ```
323
+
324
+ ### GPS Tracking
325
+
326
+ ```sql
327
+ CREATE TABLE gps_tracks (
328
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
329
+ device_id TEXT NOT NULL,
330
+ recorded_at TIMESTAMPTZ NOT NULL,
331
+ location GEOGRAPHY(POINT, 4326) NOT NULL,
332
+ speed_kmh DOUBLE PRECISION,
333
+ heading DOUBLE PRECISION,
334
+ accuracy_m DOUBLE PRECISION
335
+ );
336
+
337
+ -- Composite index for device + time queries
338
+ CREATE INDEX idx_gps_device_time ON gps_tracks (device_id, recorded_at DESC);
339
+
340
+ -- Spatial index for location queries
341
+ CREATE INDEX idx_gps_location ON gps_tracks USING GIST (location);
342
+
343
+ -- Note: GEOGRAPHY supports GiST; BRIN is for GEOMETRY (when appropriate).
344
+
345
+ -- Create linestring from track points
346
+ SELECT
347
+ device_id,
348
+ ST_MakeLine(location::GEOMETRY ORDER BY recorded_at) AS track_line,
349
+ MIN(recorded_at) AS start_time,
350
+ MAX(recorded_at) AS end_time
351
+ FROM gps_tracks
352
+ WHERE device_id = 'device_001'
353
+ AND recorded_at >= '2024-01-01'
354
+ GROUP BY device_id;
355
+ ```
356
+
357
+ ### Service Areas / Coverage Zones
358
+
359
+ ```sql
360
+ CREATE TABLE service_zones (
361
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
362
+ zone_name TEXT NOT NULL,
363
+ zone_type TEXT NOT NULL,
364
+ boundary GEOMETRY(POLYGON, 4326) NOT NULL,
365
+ population INTEGER,
366
+ active BOOLEAN NOT NULL DEFAULT true,
367
+ CONSTRAINT valid_zone_type CHECK (zone_type IN ('delivery', 'service', 'coverage')),
368
+ CONSTRAINT valid_boundary CHECK (ST_IsValid(boundary))
369
+ );
370
+
371
+ CREATE INDEX idx_zones_boundary ON service_zones USING GIST (boundary);
372
+ CREATE INDEX idx_zones_active ON service_zones (active) WHERE active = true;
373
+
374
+ -- Check if location is within any active service zone
375
+ SELECT zone_name, zone_type
376
+ FROM service_zones
377
+ WHERE active = true
378
+ AND ST_Contains(boundary, ST_SetSRID(ST_MakePoint(-122.4194, 37.7749), 4326));
379
+ ```
380
+
381
+ ## Performance Patterns
382
+
383
+ ### Use ST_DWithin Instead of ST_Distance
384
+
385
+ ```sql
386
+ -- SLOW: calculates distance for all rows
387
+ SELECT * FROM pois
388
+ WHERE ST_Distance(location, ref_point) < 1000;
389
+
390
+ -- FAST: uses spatial index
391
+ SELECT * FROM pois
392
+ WHERE ST_DWithin(location, ref_point, 1000);
393
+ ```
394
+
395
+ ### Use && for Bounding Box Pre-filtering
396
+
397
+ ```sql
398
+ -- Bounding box operator leverages spatial index
399
+ SELECT * FROM parcels
400
+ WHERE boundary && ST_MakeEnvelope(-122.5, 37.7, -122.4, 37.8, 4326)
401
+ AND ST_Intersects(boundary, search_polygon);
402
+ ```
403
+
404
+ ### Avoid Functions on Indexed Columns
405
+
406
+ ```sql
407
+ -- SLOW: function prevents index usage
408
+ SELECT * FROM parcels WHERE ST_Area(boundary) > 10000;
409
+
410
+ -- FAST: use generated column with regular index
411
+ ALTER TABLE parcels ADD COLUMN area_sqm DOUBLE PRECISION
412
+ GENERATED ALWAYS AS (ST_Area(boundary::GEOGRAPHY)) STORED;
413
+ CREATE INDEX idx_parcels_area ON parcels (area_sqm);
414
+ SELECT * FROM parcels WHERE area_sqm > 10000;
415
+ ```
416
+
417
+ ### Simplify Geometries for Display
418
+
419
+ ```sql
420
+ -- Reduce complexity for web display (tolerance in CRS units)
421
+ SELECT
422
+ id,
423
+ name,
424
+ ST_AsGeoJSON(ST_Simplify(boundary, 0.0001)) AS geojson
425
+ FROM parcels;
426
+ ```
427
+
428
+ ### Use Appropriate Precision
429
+
430
+ ```sql
431
+ -- Reduce coordinate precision for storage efficiency
432
+ UPDATE locations SET geom = ST_ReducePrecision(geom, 0.000001);
433
+
434
+ -- GeoJSON with limited decimal places
435
+ SELECT ST_AsGeoJSON(location, 6) AS geojson FROM pois;
436
+ ```
437
+
438
+ ## Data Validation
439
+
440
+ ### Geometry Validity Checks
441
+
442
+ ```sql
443
+ -- Add validity constraint
444
+ ALTER TABLE parcels ADD CONSTRAINT valid_geom CHECK (ST_IsValid(boundary));
445
+
446
+ -- Find and fix invalid geometries
447
+ SELECT id, ST_IsValidReason(boundary) AS reason
448
+ FROM parcels
449
+ WHERE NOT ST_IsValid(boundary);
450
+
451
+ -- Attempt to fix invalid geometries
452
+ UPDATE parcels
453
+ SET boundary = ST_MakeValid(boundary)
454
+ WHERE NOT ST_IsValid(boundary);
455
+ ```
456
+
457
+ ### SRID Consistency
458
+
459
+ ```sql
460
+ -- Verify SRID consistency
461
+ SELECT DISTINCT ST_SRID(geom) FROM spatial_table;
462
+
463
+ -- Enforce SRID with constraint
464
+ ALTER TABLE locations ADD CONSTRAINT enforce_srid
465
+ CHECK (ST_SRID(location) = 4326);
466
+ ```
467
+
468
+ ### Coordinate Range Validation
469
+
470
+ ```sql
471
+ -- Ensure coordinates are within valid WGS84 bounds
472
+ ALTER TABLE global_locations ADD CONSTRAINT valid_coords CHECK (
473
+ ST_X(location::GEOMETRY) BETWEEN -180 AND 180 AND
474
+ ST_Y(location::GEOMETRY) BETWEEN -90 AND 90
475
+ );
476
+ ```
477
+
478
+ ## Do Not Use
479
+
480
+ - **PostgreSQL built-in types** (`POINT`, `LINE`, `POLYGON`, `CIRCLE`) - use PostGIS types instead
481
+ - **SRID 0** (undefined) - always specify the correct SRID
482
+ - **ST_Distance for filtering** - use ST_DWithin for index-supported distance queries
483
+ - **Mixed SRIDs** in operations - always transform to common SRID first
484
+ - **GEOGRAPHY for complex analysis** - use GEOMETRY with appropriate projection
485
+ - **Over-precise coordinates** - GPS accuracy is ~3-5m, 6 decimal places (0.1m) is sufficient
486
+
487
+ ## Common Pitfalls
488
+
489
+ 1. **Longitude/Latitude order**: PostGIS uses `(longitude, latitude)` = `(X, Y)`, not `(lat, lon)`
490
+ 2. **GEOGRAPHY distance units**: Always in meters, regardless of display
491
+ 3. **Index not used**: Run `EXPLAIN ANALYZE` to verify spatial index usage
492
+ 4. **Transform performance**: Cache transformed geometries for repeated queries
493
+ 5. **Large geometries**: Consider ST_Subdivide for very complex polygons
494
+ 6. **SQL injection / unsafe dynamic SQL**: Don't concatenate untrusted input into SQL. Parameterize values; for dynamic identifiers use safe quoting (`quote_ident`, `format('%I', ...)`) or strict allowlists.
@@ -0,0 +1,21 @@
1
+ # Upstream Attribution
2
+
3
+ This skill is derived from the `design-postgis-tables` skill in `timescale/pg-aiguide`.
4
+
5
+ - **Upstream repository:** https://github.com/timescale/pg-aiguide
6
+ - **Upstream path:** `skills/design-postgis-tables/`
7
+ - **License:** Apache-2.0 (see [third_party/timescale-pg-aiguide/LICENSE](../../third_party/timescale-pg-aiguide/LICENSE))
8
+ - **Imported commit:** `b4f11a45907af3abda0f79e784aff9a6d5eef468`
9
+ - **Import date:** 2026-08-25
10
+ - **Local path:** `skills/design-postgis-tables/`
11
+
12
+ ## Imported Files
13
+
14
+ - `SKILL.md`
15
+
16
+ ## Local modifications
17
+
18
+ Initial import:
19
+ - Local behavioral modifications: none (faithful vendoring, byte-for-byte identical to upstream).
20
+
21
+ Any future AgentLayer modifications must be documented here.
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "design-postgis-tables",
3
+ "layer": "implementation",
4
+ "domains": [
5
+ "database",
6
+ "geospatial"
7
+ ],
8
+ "capabilities": [
9
+ "postgis-schema",
10
+ "spatial-indexing",
11
+ "geometry-types",
12
+ "spatial-sql"
13
+ ],
14
+ "triggers": [
15
+ "postgis",
16
+ "spatial database",
17
+ "geometry column",
18
+ "geography",
19
+ "gist index",
20
+ "spatial sql",
21
+ "st_contains",
22
+ "st_intersects",
23
+ "st_dwithin"
24
+ ],
25
+ "recommended": [],
26
+ "requires": [],
27
+ "conflicts": [],
28
+ "explanation": "Architects PostGIS spatial tables, geometry/geography types, and spatial indexes.",
29
+ "sections": [
30
+ {
31
+ "id": "before-you-start-5-questions",
32
+ "title": "Before You Start (5 Questions)",
33
+ "source": "SKILL.md#before-you-start-5-questions"
34
+ },
35
+ {
36
+ "id": "core-rules",
37
+ "title": "Core Rules",
38
+ "source": "SKILL.md#core-rules"
39
+ },
40
+ {
41
+ "id": "geometry-vs-geography",
42
+ "title": "Geometry vs Geography",
43
+ "source": "SKILL.md#geometry-vs-geography"
44
+ },
45
+ {
46
+ "id": "geometry-types",
47
+ "title": "Geometry Types",
48
+ "source": "SKILL.md#geometry-types"
49
+ },
50
+ {
51
+ "id": "coordinate-systems-srid",
52
+ "title": "Coordinate Systems (SRID)",
53
+ "source": "SKILL.md#coordinate-systems-srid"
54
+ },
55
+ {
56
+ "id": "spatial-indexing",
57
+ "title": "Spatial Indexing",
58
+ "source": "SKILL.md#spatial-indexing"
59
+ },
60
+ {
61
+ "id": "table-design-examples",
62
+ "title": "Table Design Examples",
63
+ "source": "SKILL.md#table-design-examples"
64
+ },
65
+ {
66
+ "id": "performance-patterns",
67
+ "title": "Performance Patterns",
68
+ "source": "SKILL.md#performance-patterns"
69
+ },
70
+ {
71
+ "id": "data-validation",
72
+ "title": "Data Validation",
73
+ "source": "SKILL.md#data-validation"
74
+ },
75
+ {
76
+ "id": "do-not-use",
77
+ "title": "Do Not Use",
78
+ "source": "SKILL.md#do-not-use"
79
+ },
80
+ {
81
+ "id": "common-pitfalls",
82
+ "title": "Common Pitfalls",
83
+ "source": "SKILL.md#common-pitfalls"
84
+ }
85
+ ],
86
+ "checks": [
87
+ "crs.consistent",
88
+ "geometry.valid"
89
+ ]
90
+ }