Sql 2008 查询问题 - 地理多边形中存在哪些 LatLong?

发布于 2024-07-09 07:41:02 字数 625 浏览 12 评论 0原文

我有以下两个表:-

GeoShapes

  • GeoShapeId INT IDENTITY
  • Name VARCHAR(100)
  • ShapeFile GEOGRAPHY [这是一个纬度/经度的闭合多边形]

CrimeLocations

  • CrimeLocationId INT IDENTITY
  • LatLong GEOGRAPHY [这是一个纬度/经度点]

现在,我有10K GeoShape 结果和大约 500 个犯罪地点。

我正在尝试找出所有 500 个犯罪纬度/经度点都存在于哪些 GeoShapes 中。

:( 我只是不明白!我正在尝试执行 STIntersects 在子查询上,但没有工作。

编辑

:我不能使用任何 GEOMETRY 函数..因为(如上所述)这些都是地理类型。

1 知道如何使用 STContainsSTIntersects。请不要提供这方面的基本示例,我更好奇的是如何使用上面的表结构进行复杂的查询。

i have the following two tables:-

GeoShapes

  • GeoShapeId INT IDENTITY
  • Name VARCHAR(100)
  • ShapeFile GEOGRAPHY [ this is a closed Polygon of Lat/Longs ]

CrimeLocations

  • CrimeLocationId INT IDENTITY
  • LatLong GEOGRAPHY [ this is a Lat/Long Point ]

Now, i have around 10K GeoShape results and around 500CrimeLocations.

I'm trying to figure out which GeoShapes all 500 crime lat/long points exist inside of.

:( I just don't get it! I was trying to do an STIntersects on a subquery but that didn't work. Any suggestions?

cheers!

EDIT 1: I cannot use any GEOMETRY functions .. because (as stated above) these are all geography types.

EDIT 2: I know how to use STContains and STIntersects. Please don't provide basic examples of that. I'm more curious about to do a complex query with my table structure, above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-07-16 07:41:02

关于您的“编辑”,您很少会看到包含“请不要提供...”的问题。 当然每一点都有帮助吗? 特别是因为您还没有实际向我们展示您对 STContainsSTIntersects(或 Filter())的了解。就此而言)...

无论如何,我有一个方便的邮政编码和商店位置数据库,因此我重命名了表/列以匹配您的(然后我有 6,535 个 CrimeLocatoins 和 3,285 个 GeoShapes)。 我想您现在已经弄清楚了 - 但其他人可能会发现这很有用...

查询返回每个 GeoShapes.ShapeFile 中的 CrimeLocations 数量

SELECT G.Name, COUNT(CL.Id)
FROM   GeoShapes G
INNER JOIN CrimeLocations CL ON G.ShapeFile.STIntersects(CL.LatLong) = 1
GROUP BY G.Name
ORDER BY 2 DESC

以下 年龄(比如 20 分钟),因为我没有设置任何地理空间索引,并且我的 ShapeFile 具有很高的点计数,但它确实运行成功。 如果我想按照您的建议限制结果:

SELECT G.Name, COUNT(CL.Id)
FROM   GeoShapes G
INNER JOIN CrimeLocations CL ON G.ShapeFile.STIntersects(CL.LatLong) = 1
GROUP BY G.Name
HAVING COUNT(CL.Id) = 500

当然您不想对数字 500 进行硬编码 - 所以您可以在那里添加一个 COUNT(*) FROM CrimeLocations 子查询,或者一个带有来自单独查询的总计。

这够复杂吗?

Regarding your 'edits', it's not often you see a question that includes "please don't provide...". Surely every little bit helps? Particularly since you haven't actually shown us what you do know about STContains or STIntersects (or Filter() for that matter)...

Anyway, I had a database of zipcodes and storelocations handy, so I renamed the tables/columns to match yours (I then have 6,535 CrimeLocatoins and 3,285 GeoShapes). I presume you've figured it out by now - but someone else might find this useful...

The following query returns the number of CrimeLocations in each GeoShapes.ShapeFile

SELECT G.Name, COUNT(CL.Id)
FROM   GeoShapes G
INNER JOIN CrimeLocations CL ON G.ShapeFile.STIntersects(CL.LatLong) = 1
GROUP BY G.Name
ORDER BY 2 DESC

It takes ages (like 20 mins) because I haven't setup any geospatial indexes and my ShapeFiles have a high point-count, but it does run successfully. If I wanted to restrict the results as you suggest:

SELECT G.Name, COUNT(CL.Id)
FROM   GeoShapes G
INNER JOIN CrimeLocations CL ON G.ShapeFile.STIntersects(CL.LatLong) = 1
GROUP BY G.Name
HAVING COUNT(CL.Id) = 500

Of course you don't want to hardcode the number 500 - so you could add a COUNT(*) FROM CrimeLocations subquery there, or a variable with the total from a separate query.

Is that complex enough?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文