Question

I'm working on a project that has to do with poi. I store some pois in a SQL database , but before I store a poi I want to check that there is no poi with the coordinates that I have given in the database and also there is no poi with the same name and the same type around the area of 1km , of the poi that I'm about to add. I have searched the web but I haven't figure out something help full ! Here is a part of my method that try to check if there is another poi in a radius of 1km !

PS : GPS is the name of the table

        for(i=0.0;i<=1.0;i=i+0.0001){
        try{
            query = "SELECT TYPE FROM GPS WHERE NAME='"+name+"' AND TYPE='"+type+"' AND     X='"+(x+i)+"' AND Y='"+(y+i)+"'";
            rs=state.executeQuery(query);
            if(!rs.next()){
                flag=1;
            }
            else{
                flag=0;
                break;
            }

        }catch(SQLException err){
            err.printStackTrace();
        }
        }
Was it helpful?

Solution 2

If you ignore curvature (and use a square box) you could do it a lot more efficiently in a single query, something like:

query = "SELECT TYPE FROM GPS 
WHERE NAME='"+name+"' 
AND TYPE='"+type+"' 
AND X < x+1 AND X > x-1
AND Y < y+1 AND Y > y-1;

If need be, you can then iterate through the result set and filter out pois in the box that wouldn't be in a circle with some maths.

OTHER TIPS

Ignoring curvature, a distance formula can be used to find the distance between two points (x1, y1) and (x2, y2).

enter image description here

So, if you start with a point (A, B) and your table has columns for X and Y, this becomes:

SELECT *
FROM TABLE
WHERE   root( sqr(X - A) + sqr(Y - B) ) <= 1

Replace "root" and "sqr" with the DB-specific functions.

Warning: In theory, this breaks down near the international date line (since longitude of 179 is close to -179), but that should not be an issue in a typical application.

I guess a GIS like PostGIS could give you that functionality out of the box.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top