sql server - Checking if a Point is inside a Polygon not working -
using sql server 2012 following query keeps telling me point i'm looking not inside polygon i'm using, when know is. using same data in qgis 2.2.0, see point inside polygon (screenshot).
declare @polygon geography = geography::stpolyfromtext('polygon ((-111.0498046875 33.966142265597391, -110.9124755859375 33.472690192666633, -110.94268798828125 32.983324091837417, -111.7364501953125 32.680996432581921, -112.587890625 32.731840896865656, -113.0657958984375 33.307577130152978, -112.9010009765625 33.811102288647007, -112.32147216796875 34.1890858311723, -111.4453125 34.129994745824717, -111.0498046875 33.966142265597391))', 4326); declare @point geography = geography::stpointfromtext('point (-112.0685317 33.4491407)', 4326); select @point.stisvalid() [pointisvalid], --true @polygon.stisvalid() [polygonisvalid], --true @point.stwithin(@polygon) [pointwithinpolygon], --false @point.stintersects(@polygon) [pointintersectspolygon], --false @polygon.stcontains(@point) [polygoncontainspoint]; --false
what need make query tell me point in polygon? i've seen search results talk "direction", have no idea how specify it. coordinates captured google maps javascript api , persisted database using entity framework 6.1.
i think have ring orientation problem. need reverse order of polygon:
using microsoft.sqlserver.types; using system; using system.collections.generic; using system.data.spatial; using system.data.sqltypes; namespace somenamespace { public static class dbgeographyhelper { // 4326 common coordinate system used gps/maps // 4326 format puts longitude first latitude private static int _coordinatesystem = 4326; public static dbgeography createpolygon(string wktstring) { // create polygon same order wktstring var sqlgeography = sqlgeography .stgeomfromtext(new sqlchars(wktstring), _coordinatesystem) .makevalid(); // create polygon in reverse order var invertedsqlgeography = sqlgeography.reorientobject(); // ever 1 big not 1 want if (sqlgeography.starea() > invertedsqlgeography.starea()) { sqlgeography = invertedsqlgeography; } return dbspatialservices.default.geographyfromprovidervalue(sqlgeography); } } }
Comments
Post a Comment