Just to know if a point is inside a polygon (spherical coordinates)

1,455 views
Skip to first unread message

Imóveis Nacionais

unread,
May 22, 2012, 5:19:31 PM5/22/12
to spatiali...@googlegroups.com
Hi all,
I am looking for some Java lib that could tell me if a point is inside a polygon .
Is Spatialite a good option. Does spetialite supports it. By the way, my server application uses a SQLite db but (till now) it does not stores spatial data (no need).

Thanks a lot

Alex

daniele....@jrc.ec.europa.eu

unread,
May 22, 2012, 5:35:54 PM5/22/12
to spatiali...@googlegroups.com, Imóveis Nacionais
Well, it can for sure; but I wouldn't buy an howitzer to shoot at flies.
My personal experince is to avoid the burden of a complete super
featured framework, when you really need one single feature.
Such algorithms are really simple, in the end.
I started with the sum of corners between the point and every
couple of adjacent vertices: very funny. Anyway, it is much better to
estimate every intersection between the polygon sides and a horizontal
line passing for the point itself.

Daniele

Imóveis Nacionais <imoveisn...@gmail.com> ha scritto:
> --
> You received this message because you are subscribed to the Google
> Groups "SpatiaLite Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/spatialite-users/-/SgJsI5PvwGYJ.
> To post to this group, send email to spatiali...@googlegroups.com.
> To unsubscribe from this group, send email to
> spatialite-use...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/spatialite-users?hl=en.
>
>

Brad Hards

unread,
May 22, 2012, 5:39:23 PM5/22/12
to spatiali...@googlegroups.com
Without a more detailed description of what you are trying to do (in terms of
architecture, data sources and output) its difficult to provide any meaningful
advice.

SpatiaLite is certainly capable of this kind of query, but the inputs to the
function (ST_Within) may require some manipulation.

Another option would be Java Topology Suite (JTS).

Brad

Imóveis Nacionais

unread,
May 22, 2012, 6:12:48 PM5/22/12
to spatiali...@googlegroups.com
Hi

The architecture is simple:

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 ...

Alex

Imóveis Nacionais

unread,
May 22, 2012, 6:23:37 PM5/22/12
to spatiali...@googlegroups.com
I did it already in JAvascript and OpenLayers, but this is client side code. I need server side


function Testingpoint()
{
var p = new OpenLayers.Geometry.Point(positions[0].longi, positions[0].lat);

if(map.layers[1].features[0].geometry.containsPoint(p)) 
{
alert('yes, point contained');
else 
{
alert('no, point not contained or containsPoint-method not defined');
}

}


I just need this at server side and nothing more

Alex

Brad Hards

unread,
May 22, 2012, 9:27:34 PM5/22/12
to spatiali...@googlegroups.com
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

Imóveis Nacionais

unread,
May 26, 2012, 4:13:12 PM5/26/12
to SpatiaLite Users
Hi, using Java, how to get the contents of a spatialite field?

Like this:


statement.executeUpdate("SELECT AddGeometryColumn('areas', 'geom',
4326, 'POLYGON', 'XY')");

//Basic square
String strSt="INSERT INTO areas VALUES (1, 'zoneA',
GeomFromText('POLYGON (0 0, 10 0, 10 10, 0 10)',4326))";
statement.executeUpdate(strSt);



strSt="select geom from areas where areas.label == 'zoneA'";
ResultSet rs=statement.executeQuery(strSt);
if(rs.next())
{

str=rs.getString("geom");


and it says that geom field does not exists, but the felds exists!

Thanks a lot

Alex

a.fu...@lqt.it

unread,
May 26, 2012, 5:24:42 PM5/26/12
to spatiali...@googlegroups.com
> str=rs.getString("geom");
>
> and it says that geom field does not exists, but the felds exists!
>

Hi Alex,

a Geometry value surely isn't of the Java String type ;-)
it's a binary object, aka BLOB.
so the JDBC expected syntax is something like:

Blob geom = rs.getBlob("geom");

Alternatively, if you absolutely need to retrieve a String
for some good reason, you must modify your query so to
include the AsText() or ST_AsText() SQL function: e.g.

Sting strSt = "select AsText(geom) from areas where areas.label ==
'zoneA'";
ResultSet rs=statement.executeQuery(strSt);

bye Sandro


--
Il messaggio e' stato analizzato alla ricerca di virus o
contenuti pericolosi da MailScanner, ed e'
risultato non infetto.

Imóveis Nacionais

unread,
May 26, 2012, 5:52:45 PM5/26/12
to spatiali...@googlegroups.com
Thank you.
I am using this code:

strSt="select geom from areas where areas.label == 'zoneA'";
ResultSet rs=statement.executeQuery(strSt);
if(rs.next())
{
   Blob geom = rs.getBlob("geom");

And an exception is thrown saying that:

"Not implemented by SQLite JDBC Driver"


I am using sqlite-jdbc-3.7.7.1-20110713.014305-1.jar and libspatialite-2.dll

Please, do you know any driver that supports GetBlob?

Thanks a lot

Alex

a.fu...@lqt.it

unread,
May 26, 2012, 6:07:14 PM5/26/12
to spatiali...@googlegroups.com
Please Alex,

avoid to send back the whole previous message yet another time
(it's very disturbing) ;-)

I suppose you'll find useful to read this two
documents about SpatiaLite and Java:

http://www.gaia-gis.it/spatialite-2.4.0-4/splite-jdbc.html
http://www.gaia-gis.it/gaia-sins/java-test/JavaTestCoverage.pdf
Reply all
Reply to author
Forward
0 new messages