我有两张桌子想要加入:
我希望创建一个视图来查找英国的哪些位置与哪些县相交。
这是我想出的脚本:
--Define list of uk locations and their lat, longs
WITH geo AS (
SELECT
uk_locations,
ST_SetSRID(ST_Point(latitude ,longitude),4326) AS lat_long --set to SRID 4326
FROM
sd_lat_long
),
--Defining the shapefile and transforming the geometry
shp AS (
SELECT
county,
ST_Transform(geometry, 4326) AS geometry --Transformed to SRID 4326 from 27700
FROM
sd_geo_uk_counties
WHERE 1=1
)
--Join on intersection of lat_long and the county geometry to assign a county to the UK locations
SELECT
geo.uk_locations,
shp.county
FROM
geo
LEFT JOIN
shp
ON
ST_Intersects(shp.geometry, geo.lat_long)
对于 shp CTE,我使用这篇文章来提供帮助:为什么在使用 PostGIS 时,英国县的几何图形会出现在几内亚湾?
我面临的问题是,我不断收到一个错误,该错误似乎与我在脚本中使用的几何图形的 SRID 有关。请参阅下面的错误。
我首先查看了上面脚本的 geo 和 shp CTE 中几何字段的 SRID。
地理: lat_long 字段看起来不错 - 即 SRID = 4326 测试脚本:
WITH geo AS (
SELECT
uk_locations,
ST_SetSRID(ST_Point(latitude ,longitude),4326) AS lat_long --set to SRID 4326
FROM
sd_lat_long
),
SELECT
uk_locations AS geo_field,
ST_SRID(lat_long) AS srid,
'geo' AS cte
FROM
geo
GROUP BY
1,2, 3
HAVING
srid <> 4326
这没有返回任何错误或 SRID <> 4326 的条目
SHP: 测试脚本:
WITH shp AS (
SELECT
ctyua23nm AS county,
ST_Transform(geometry, 4326) AS geometry
FROM
sd_geo_uk_counties
WHERE 1=1
)
SELECT
county AS geo_field,
ST_SRID(geometry) AS srid,
'shp' AS cte
FROM
shp
GROUP BY
1,2,3
我不知道从这里去哪里,非常感谢任何帮助!
srid
中的sd_geo_uk_counties
未设置,因此假设为0
,并且它是st_transform
的无效值。
先尝试设置一下:
SELECT
ctyua23nm AS county,
ST_Transform(st_srid(geometry,27700), 4326) AS geometry
FROM
sd_geo_uk_counties
理想情况下,您可以在列级别设置 srid 并更新(一次)每个几何图形以设置 srid。
顺便说一句,您在构建点时交换了纬度和经度,应该首先是经度:
ST_SetSRID(ST_Point(longitude, latitude),4326) AS lat_long