开发者

How would I write this spatial query?

开发者 https://www.devze.com 2023-03-14 16:31 出处:网络
I have the following table structure: column names : celllongitude latitude bcchbsic data types: varchar doubledoubledouble double

I have the following table structure:

column names : cell    longitude latitude bcch   bsic
data types   : varchar double    double   double double 
Keys         : x       

I want to know all the cells which are

  • in 10 km range of each other AND
  • have the same bcch+bsic.

What would be the spatial sql query for the above requirement?

Due to my limited understanding of postgis feel free to start your answer with "use this database table structure instead开发者_JAVA百科" so that it is more GIS oriented (I believe there is a concept of points rather than lat/long colums). I haven't written spatial queries before and am considering buying the "PostGIS In Action" book but need to know if what I am trying to do is possible and how.

Additionally I would like mention that I know how to do it in standard sql. I need a spatial query because there are round about 10000 records and using a standard sql method I would need to generate 10000*10000 (all the other cells for each cell) records and then query them which would be highly inefficient.


Denis,

that is not true. A gist index would help here.

Basarat I'm not quite clear what output you expect. Here is a query that for each cell would return those at that are within 10km. First you want to add a geography column and then create a gist index of it. That's covered in first chapter of PostGIS in Action.

So lets say you have this new column called geog that you have put a gist index on.

Then your query would be

SELECT c.cell, array_agg(n.cell) As cells_close
FROM cells As c INNER JOIN cells As n ON ST_DWithin(c.geog, n.geog, 10000)
WHERE c.bsic = n.bsic  --other criteria go here
GROUP BY c.cell;

If you don't want the output as an array -- you can do

array_to_string(array_agg(n.cell),',') As cell_comma_sep
0

精彩评论

暂无评论...
验证码 换一张
取 消