MySQL implementation of ray-casting Algorithm?

后端 未结 7 2006
小蘑菇
小蘑菇 2021-02-01 08:56

We need to figure out a quick and fairly accurate method for point-in-polygon for lat/long values and polygons over google maps. After some research - came across some posts abo

7条回答
  •  死守一世寂寞
    2021-02-01 09:20

    I wanted to use the above mywithin stored procedure on a table of polygons so here are the commands to do just that.

    After importing a shapefile containing polygons into mysql using ogr2ogr as follows

    ogr2ogr -f "mysql" MYSQL:"mydbname,host=localhost,user=root,password=mypassword,port=3306" -nln "mytablename" -a_srs "EPSG:4326" /path/to/shapefile.shp
    

    you can then use MBRwithin to prefilter your table and mywithin to finish as follows

    DROP TEMPORARY TABLE IF EXISTS POSSIBLE_POLYS;
    CREATE TEMPORARY TABLE POSSIBLE_POLYS(OGR_FID INT,SHAPE POLYGON);
    INSERT INTO POSSIBLE_POLYS (OGR_FID, SHAPE) SELECT mytablename.OGR_FID,mytablename.SHAPE FROM mytablename WHERE MBRwithin(@testpoint,mytablename.SHAPE);
    
    DROP TEMPORARY TABLE IF EXISTS DEFINITE_POLY;
    CREATE TEMPORARY TABLE DEFINITE_POLY(OGR_FID INT,SHAPE POLYGON);
    INSERT INTO DEFINITE_POLY (OGR_FID, SHAPE) SELECT POSSIBLE_POLYS.OGR_FID,POSSIBLE_POLYS.SHAPE FROM POSSIBLE_POLYS WHERE mywithin(@testpoint,POSSIBLE_POLYS.SHAPE);
    

    where @testpoint is created, for example, from

    SET @longitude=120;
    SET @latitude=-30;
    SET @testpoint =(PointFromText( concat( "POINT(", @longitude, " ", @latitude, ")" ) ));
    

提交回复
热议问题