I have a table that has more than 15k rows.
The table has locationID, isEmpty, longitude and latitude fields.
I want to calculate the distance between each point has isEmpty field = 1 and the others and insert the result into new table using Postgis.
Please advise what is the best practice for writing sql statement executing this requirement
Ok, thanks for explanations - now it's clearer.
In Postgis you don't have to count all distances to find how many other points are within some distance.
Add extension to database
CREATE EXTENSION postgis;
Now add a column of type geometry
ALTER TABLE mytable ADD COLUMN geom geometry;
You'll need some index to
CREATE INDEX mytable_geom_idx ON mytable USING gist(geom);
Now populate new column
UPDATE mytable SET geom = ST_SetSRID(ST_MakePoint(lon, lat),4326);
And now the query:
Select a.locationID, count(*) from mytable a join mytable b on a.locationID!=b.locationID and ST_Dwithin(a.geom::geography,b.geom::geography,152) where a.isEmpty=1 group by 1;
ST_DWithin with geography type is taking distance in meters and 152m~500ft
I didn't test it, so if something will not work please write me a comment
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.