On Wednesday 23 May 2012 08:12:48 Imóveis Nacionais wrote:
> I have a servlet server that receives coordinates from GPS devices, via SMS
> or GPRS. Before storage of the new arrived point int database I need to
> know if that point that come from car Y violates or not geo fences.
> Geofences are defined and stored in db as polygons...
> If the new point violates fences I fire an alarm (and store it in the
> database). the client, on the browser, will fetch all alarms on demand ...
>
Please don't top post.
Here is a simple example. Presumably you create the table of
geofenced zones before hand. So it looks like:
CREATE TABLE areas (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, label TEXT NOT NULL);
SELECT AddGeometryColumn('areas', 'geom', 4326, 'POLYGON', 'XY');
-- basic square
INSERT INTO areas (label, geom) VALUES ("zoneA", PolygonFromText("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", 4326));
-- rectangle with an excluded triangle in the middle
INSERT INTO areas (label, geom) VALUES ("zoneB", PolygonFromText("POLYGON((10 0, 20 0, 20 10, 10 10, 10 0),(14 2, 18 2, 16 8, 14 2))", 4326));
Then you do simple SQL queries to test if a point is inside or outside:
SELECT "is 5,5 within zoneA:", ST_Within(MakePoint(5, 5), (select geom from areas where areas.label == "zoneA"));
SELECT "is 5,5 within zoneB:", ST_Within(MakePoint(5, 5), (select geom from areas where areas.label == "zoneB"));
SELECT "is 15,1 within zoneB:", ST_Within(MakePoint(15, 1), (select geom from areas where areas.label == "zoneB"));
SELECT "is 15,5 within zoneB:", ST_Within(MakePoint(15, 5), (select geom from areas where areas.label == "zoneB"));
Here is what it will look like when you run it.
$ spatialite :memory: < areas.sql
SpatiaLite version ..: 3.1.0-RC2 Supported Extensions:
- 'VirtualShape' [direct Shapefile access]
- 'VirtualDbf' [direct DBF access]
- 'VirtualXL' [direct XLS access]
- 'VirtualText' [direct CSV/TXT access]
- 'VirtualNetwork' [Dijkstra shortest path]
- 'RTree' [Spatial Index - R*Tree]
- 'MbrCache' [Spatial Index - MBR cache]
- 'VirtualSpatialIndex' [R*Tree metahandler]
- 'VirtualFDO' [FDO-OGR interoperability]
- 'SpatiaLite' [Spatial SQL - OGC]
PROJ.4 version ......: Rel. 4.7.1, 23 September 2009
GEOS version ........: 3.4.0dev-CAPI-1.8.0
the SPATIAL_REF_SYS table already contains some row(s)
1
is 5,5 within zoneA:|1
is 5,5 within zoneB:|0
is 15,1 within zoneB:|1
is 15,5 within zoneB:|0
You could make this faster for some operations, but presumably
you know which zone a car is allowed in, so I don't think you'd
benefit much from an spatial index.
HTH
Brad