Is it possible to have a very quick non-resource-intensive method to Spatial Query with True False returned if there are any features/points within a shape?
Spatial Query with True False returned if features are within a Shape
Gregory,
Unfortunately, there is no guarantee for a very quick non-resource-intensive reliable method. How quick the spatial query is going to be is based on the complexity of the polygon. Doing a spatial query to know if a point is inside a triangle or rectangle is going to be very quick. On the other hand, the same spatial query on a complex concave polygon made of thousands of vertices is going to be a lot more intensive operation. Nonetheless, there are some tricks to gain some efficiency. For example, to know if a point is inside a polygon or not, it makes sense to check first if the point is inside the bounding box (a rectangle) of the polygon before doing the actual spatial query on the polygon itself. Those tricks for efficiency are under the hood of our API, so I don’t think you can do any thing on your side to gain efficiency. Thank you.
How do you check if the first point is inside the bounding box ?
Gregory,
To check if a point is inside a boundingbox, you simply get the boundingbox (RectangleShape type) of the shape and use the Contains function of the RectangleShape. You can see the sample code for checking if a point is inside the bounding box of a polygon. Also, I am intrigued by why you say the "first" point, which leads me to believe that you are up to accomplish something within a context I am unaware of.
RectangleShape boundingBox = myPolygonShape.GetBoundingBox();
bool IsInside = boundingBox.Contains(myPointShape);
Ok an example might explain better.
I have a SQLTablel of Points (Geometry Format)
I need to return a true if ANY of those points are found within a specified area.
And i need to return a False if none are found.
There are over 1.5 million points so i need a fast way of performing this task.
Currently i perform a Spatial Query and then check if the Count is > 0 then true else false
Gregory,
Ok, now I understand about your scenario. If you need a quick and efficient method to return true if any of the points are within a specified area, I will use the following method:
-Loop thru all the points to select only the points that are within the bounding box of the specified area.
-Loop thru the select points and stop at the first that is completely within the specified area.
Another method that might be faster if those 1.5 millions points don't change too often, is to write all those points into a layer such as a shapefile and perform the spatial query with the different specified areas.
If anyone in the Map Suite community has any other ideas, please participate to this post. Thank you.
How would you convert points into a shapefile ?
Gregory,
To build a shape file with point type, you can follow the steps below:
1), create a shape file with type "ShapeFileType.Point"
ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Point, "shapefile.shp", new DbfColumn[] { new DbfColumn("name", DbfColumnType.String, 10, 0) });
2), Open this shape file and edit it.
ShapeFileFeatureSource featureSource = new ShapeFileFeatureSource("shapefile.shp", ShapeFileReadWriteMode.ReadWrite);
featureSource.Open();
featureSource.BeginTransaction();
//features is the point type features
foreach (var feature in features)
{
featureSource.AddFeature(feature);
}
featureSource.CommitTransaction();
featureSource.Close();
Now the shape file with point type has been created.
Thanks,
Johnny.
Hi,
Why not using the sql server geo builtin feature like within…??? also make sure that the geometry or geography column has been indexed using the tesselation process. Of course this should work if sql 2008 or above is your current platform.
remember that you have to get the shape for your query as WKT then you used this shape inside your SQL query to retrieve all the data point within. Doing this leverage to SQL server geospatial and not Thinkgeo geospatial. I am using both for my projects and I think that having the data base doing the eavy lifting work make a lot of sense.
jm.
PS: this is an example for TERADATA data base server but the same can be done with MS-SQL. has you can see the poligon of the query have been inserted inside the sql and used with the ‘within’ to retrieve all the data point belonging to a specific area or shape.
SELECT TOP 10 T1.NAICS_CODE, TRIM(BOTH FROM T1.NAICS_DESC) AS NAICS_DESC, SUM(T1.ANNL_SLS_DLR_AMT) AS ANNL_SLS_DLR_AMT_SUM FROM (SELECT * FROM DBIPAD02.GIS_GEO_LOC_DUNS WHERE TG_LON <= -107.683586 AND TG_LON >= -116.416466 AND TG_LAT <= 44.030162 AND TG_LAT >= 35.396917 ) AS T0 INNER JOIN DDUNSV01.VDUNS_DNB_FACT AS T1 ON (T0.DUNS_NBR = T1.DUNS_NBR) WHERE T0.GEOM.ST_Within(NEW ST_GEOMETRY(‘MULTIPOLYGON(((-107.696931 44.010531,-107.705171 44.030162,-107.683586 44.02982,-107.696931 44.010531)),((-109.681351 41.504813,-109.800043 41.770772,-109.386737 41.47221,-109.681351 41.504813)),((-109.74044 39.770183,-109.651555 39.973651,-109.502291 39.738404,-109.74044 39.770183)),((-109.681888 41.925854,-109.595845 41.903992,-109.727192 41.833146,-109.681888 41.925854)),((-109.994812 41.620819,-110.03493 41.55559,-110.046467 41.686284,-109.994812 41.620819)),((-110.614606 39.812363,-110.341825 39.806738,-110.341011 39.802526,-110.614606 39.812363)),((-110.341825 39.806738,-110.315107 39.817524,-110.075949 39.806358,-110.341825 39.806738)),((-110.40532 41.38566,-110.413156 41.393854,-110.396276 41.397785,-110.40532 41.38566)),((-110.405998 41.459996,-110.431316 41.428303,-110.491568 41.429744,-110.405998 41.459996)),((-110.573376 38.710573,-110.575626 38.712567,-110.570815 38.713959,-110.573376 38.710573)),((-111.282693 39.46728,-111.133157 39.947863,-110.573201 39.579296,-111.034984 39.184001,-111.299666 39.275933,-111.282693 39.46728)),((-110.706928 37.540712,-110.656224 37.537475,-110.678817 37.512631,-110.668778 37.495774,-110.663165 37.493091,-110.651069 37.493115,-110.649774 37.489972,-110.694369 37.486209,-110.75163 37.53933,-110.706928 37.540712)),((-111.612576 37.624421,-111.773964 37.867423,-111.495315 38.150425,-111.612576 37.624421)),((-111.917729 40.173196,-111.917036 40.168441,-111.924205 40.170336,-111.917729 40.173196)),((-112.116145 39.253983,-112.09751 39.257806,-112.039017 39.099143,-112.041053 39.038662,-112.083854 39.022187,-112.116145 39.253983)),((-112.74111 39.250889,-112.726612 39.50918,-112.196274 39.542053,-112.467517 38.58816,-112.74111 39.250889)),((-112.285667 40.025784,-112.240182 40.009955,-112.271134 39.995574,-112.236372 39.961284,-112.260611 39.942695,-112.285667 40.025784)),((-112.306277 39.72595,-112.316262 39.726288,-112.297631 39.747589,-112.306277 39.72595)),((-112.407837 40.012364,-112.487725 40.099637,-112.390718 40.244419,-112.407837 40.012364)),((-112.477162 38.257421,-112.439856 38.236142,-112.433114 38.225799,-112.434583 38.218081,-112.486489 38.251442,-112.558818 38.20232,-112.591194 38.232709,-112.477162 38.257421)),((-113.084269 38.265447,-113.024005 38.543202,-112.526261 38.348813,-113.084269 38.265447)),((-112.590467 38.549,-112.566452 38.503992,-112.615773 38.501223,-112.590467 38.549)),((-113.376061 39.52415,-113.063771 39.552656,-113.02677 39.516015,-113.376061 39.52415)),((-114.047461 39.667678,-114.016544 40.428313,-113.036469 39.880754,-114.047461 39.667678)),((-113.430978 38.484793,-113.352232 38.574242,-113.173086 38.523456,-113.430978 38.484793)),((-113.435146 38.3662,-113.43487 38.394045,-113.414432 38.39268,-113.435146 38.3662)),((-113.939239 39.147662,-114.043259 38.773964,-114.047277 39.461335,-113.609976 39.418376,-113.939239 39.147662)),((-114.186361 40.579593,-114.181172 40.575087,-114.151768 40.579729,-114.138084 40.577858,-114.186959 40.570455,-114.178869 40.574137,-114.184389 40.576245,-114.185987 40.579209,-114.190701 40.58035,-114.199296 40.584604,-114.21216 40.585349,-114.218691 40.59004,-114.229864 40.590245,-114.234912 40.591292,-114.243675 40.594543,-114.250217 40.599221,-114.252014 40.603757,-114.242322 40.594362,-114.218252 40.590418,-114.198929 40.585136,-114.186361 40.579593)),((-116.416466 35.651468,-116.285111 35.622874,-116.287888 35.396917,-116.416466 35.651468)))’)) = 1 AND T1.ANNL_SLS_DLR_AMT > 0 AND T1.YEAR_3_SLS_GROW_PC > 3 GROUP BY T1.NAICS_CODE, T1.NAICS_DESC ORDER BY ANNL_SLS_DLR_AMT_SUM DESC .GEOM.ST_Within(NEW ST_GEOMETRY(‘MULTIPOLYGON(((-107.696931 44.010531,-107.705171 44.030162,-107.683586 44.02982,-107.696931 44.010531)),((-109.681351 41.504813,-109.800043 41.770772,-109.386737 41.47221,-109.681351 41.504813)),((-109.74044 39.770183,-109.651555 39.973651,-109.502291 39.738404,-109.74044 39.770183)),((-109.681888 41.925854,-109.595845 41.903992,-109.727192 41.833146,-109.681888 41.925854)),((-109.994812 41.620819,-110.03493 41.55559,-110.046467 41.686284,-109.994812 41.620819)),((-110.614606 39.812363,-110.341825 39.806738,-110.341011 39.802526,-110.614606 39.812363)),((-110.341825 39.806738,-110.315107 39.817524,-110.075949 39.806358,-110.341825 39.806738)),((-110.40532 41.38566,-110.413156 41.393854,-110.396276 41.397785,-110.40532 41.38566)),((-110.405998 41.459996,-110.431316 41.428303,-110.491568 41.429744,-110.405998 41.459996)),((-110.573376 38.710573,-110.575626 38.712567,-110.570815 38.713959,-110.573376 38.710573)),((-111.282693 39.46728,-111.133157 39.947863,-110.573201 39.579296,-111.034984 39.184001,-111.299666 39.275933,-111.282693 39.46728)),((-110.706928 37.540712,-110.656224 37.537475,-110.678817 37.512631,-110.668778 37.495774,-110.663165 37.493091,-110.651069 37.493115,-110.649774 37.489972,-110.694369 37.486209,-110.75163 37.53933,-110.706928 37.540712)),((-111.612576 37.624421,-111.773964 37.867423,-111.495315 38.150425,-111.612576 37.624421)),((-111.917729 40.173196,-111.917036 40.168441,-111.924205 40.170336,-111.917729 40.173196)),((-112.116145 39.253983,-112.09751 39.257806,-112.039017 39.099143,-112.041053 39.038662,-112.083854 39.022187,-112.116145 39.253983)),((-112.74111 39.250889,-112.726612 39.50918,-112.196274 39.542053,-112.467517 38.58816,-112.74111 39.250889)),((-112.285667 40.025784,-112.240182 40.009955,-112.271134 39.995574,-112.236372 39.961284,-112.260611 39.942695,-112.285667 40.025784)),((-112.306277 39.72595,-112.316262 39.726288,-112.297631 39.747589,-112.306277 39.72595)),((-112.407837 40.012364,-112.487725 40.099637,-112.390718 40.244419,-112.407837 40.012364)),((-112.477162 38.257421,-112.439856 38.236142,-112.433114 38.225799,-112.434583 38.218081,-112.486489 38.251442,-112.558818 38.20232,-112.591194 38.232709,-112.477162 38.257421)),((-113.084269 38.265447,-113.024005 38.543202,-112.526261 38.348813,-113.084269 38.265447)),((-112.590467 38.549,-112.566452 38.503992,-112.615773 38.501223,-112.590467 38.549)),((-113.376061 39.52415,-113.063771 39.552656,-113.02677 39.516015,-113.376061 39.52415)),((-114.047461 39.667678,-114.016544 40.428313,-113.036469 39.880754,-114.047461 39.667678)),((-113.430978 38.484793,-113.352232 38.574242,-113.173086 38.523456,-113.430978 38.484793)),((-113.435146 38.3662,-113.43487 38.394045,-113.414432 38.39268,-113.435146 38.3662)),((-113.939239 39.147662,-114.043259 38.773964,-114.047277 39.461335,-113.609976 39.418376,-113.939239 39.147662)),((-114.186361 40.579593,-114.181172 40.575087,-114.151768 40.579729,-114.138084 40.577858,-114.186959 40.570455,-114.178869 40.574137,-114.184389 40.576245,-114.185987 40.579209,-114.190701 40.58035,-114.199296 40.584604,-114.21216 40.585349,-114.218691 40.59004,-114.229864 40.590245,-114.234912 40.591292,-114.243675 40.594543,-114.250217 40.599221,-114.252014 40.603757,-114.242322 40.594362,-114.218252 40.590418,-114.198929 40.585136,-114.186361 40.579593)),((-116.416466 35.651468,-116.285111 35.622874,-116.287888 35.396917,-116.416466 35.651468)))’)) = 1
one more thing, GEOM is my geometry column. translate “to find all rows within the new shape.”
.GEOM.ST_Within(NEW ST_GEOMETRY('MULTIPOLYGON(((-107.696931 44.010531,-107.705171 44.030162…
Jean,
Have you ever try our built-in API? such as MsSql2008FeatureSource.GetFeaturesWithin or MsSql2008FeatureLayer.QueryTools.GetFeaturesWithin
Thanks,
James