I'll admit I'm a little out of my element with PostGIS and spatial geometries in a DB but here's what I'm after: I need to update a field with the determined intersected US State of a geom of an object if it doesn't yet already have one.
The DB structure is as follows:
Accomplishment
id | name | phys_state | poly_point_line_id (fk Accomplishment_Feature) |
---|---|---|---|
1 | Test Accomp 1 | AK | 123 |
2 | Test Accomp 2 | 456 | |
3 | Test Accomp 3 | 789 |
Accomplishment_Feature (technically not needed in the query AFAIK but included here just in case since it is a join table between the Accomplishment and its geometry types)
id |
---|
123 |
456 |
789 |
Accoomplishment_Poly
id (fk to Accomplishment_Feature) | geom |
---|---|
123 | [multipolygon geometry] |
Accoomplishment_Line
id (fk to Accomplishment_Feature) | geom |
---|---|
123 | [multiline geometry] |
Accoomplishment_Point
id (fk to Accomplishment_Feature) | geom |
---|---|
123 | [multipoint geometry] |
I need to determine the intersected US state of each of the geoms for Accomplishments that don't have a value in the physical_state
column.
I currently have a table of US State geometries in another schema that I can use.
I currently have the following but it errors out and I'm obviously misunderstanding how to write the queries.
UPDATE accomplishment a
SET a.phys_state = us_state.abbrev
FROM support_gis.state_g us_state
LEFT JOIN accomplishment_poly poly on a.poly_point_line_id = poly.id
WHERE st_intersects(st_centroid(poly.geom), us_state.geom) AND a.phys_state is null
Any guidance or assistance would be greatly appreciated!