Finding features that intersect a bounding box with Hibernate Entity Manager

5 messages Options
Embed this post
Permalink
Andy Bailey-2

Finding features that intersect a bounding box with Hibernate Entity Manager

Reply Threaded More More options
Print post
Permalink

I wanted to find the Roads intersecting a bounding box.

I tried:
@NamedQuery(name = "Road.intersects", query = "select x from Road x
where intersects(x.geometry, :extent)=true")
I got the message ERROR:  parse error - invalid geometry
when I tried to set the parameter of the JPA query to an Envelope or
Polygon.
query.setParameter("extent", ext);

The solution I have found seems too complicated, is there no easier way
of doing this?

in Road.java I have:


In the main application I have

import javax.persistence.Query;
...
Query query = em.createNamedQuery("Road.intersects");
int srid = -1;
Polygon ext = EnvelopeAdapter.toPolygon(getExtents(), srid);
if (query instanceof org.hibernate.ejb.QueryImpl)
{
org.hibernate.ejb.QueryImpl hibernateEjbQuery =
(org.hibernate.ejb.QueryImpl) query;
org.hibernate.Query hibernateQuery =
hibernateEjbQuery.getHibernateQuery();
Type geometryType = new CustomType(GeometryUserType.class, null);

hibernateQuery.setParameter("extent", ext, geometryType);
}
List list = query.getResultList();
int size = list.size();

It works ok but its too many lines of code.

Is there no way to make the first approach work?

Also it seems that the query doesnt use the bounding box to optimize the
query, is it not possible to include the bounding box in named queries
by default?

It appears that the bounding box optimization can only be switched on by
using the criteria api, is that true?

The query generated by hibernate is:
select road0_.gid as gid1_, road0_.the_geom as the2_1_, road0_.tipo_via
as tipo3_1_, road0_.nomb_comun as nomb4_1_, road0_.objectId as
objectId1_, road0_.municipio as municipio1_, road0_.fromLeft as
fromLeft1_, road0_.toLeft as toLeft1_, road0_.fromRight as fromRight1_,
road0_.toRight as toRight1_, road0_.name as name1_, road0_.shape_leng as
shape12_1_, road0_.version as version1_, road0_.prefijo as prefijo1_
from vias road0_ where intersects(road0_.the_geom, ?)=true

Thanks in advance

Andy Bailey
www.hazlorealidad.com


_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Karel Maesen

Re: Finding features that intersect a bounding box with Hibernate Entity Manager

Reply Threaded More More options
Print post
Permalink
Hi Andy,

The Postgis extent function isn't registered as a HQL function.  
You're right that this would be very useful, and I'm going to include  
it when I finish the projection API. This will happen soon.

As regards a work-around, I think the best way for you is to bypass  
Hibernate Spatial completely, and create a _native_ named for the SQL :

select astext(extent(the_geom) from road where the_geom &&  
geomfromtext(?) and intersects(the_geom, geomfromtext(?)).

The parameters needs to be WKT Strings representing you search  
geometry. You can derive them using the jts.io WKTWriter class. The  
result will be a string that you can turn into a JTS geometry using  
the jts.io WKTReader class.

Hope this helps,

Karel Maesen


On 11 Dec 2007, at 08:57, Andy Bailey wrote:

>
> I wanted to find the Roads intersecting a bounding box.
>
> I tried:
> @NamedQuery(name = "Road.intersects", query = "select x from Road x
> where intersects(x.geometry, :extent)=true")
> I got the message ERROR:  parse error - invalid geometry
> when I tried to set the parameter of the JPA query to an Envelope or
> Polygon.
> query.setParameter("extent", ext);
>
> The solution I have found seems too complicated, is there no easier  
> way
> of doing this?
>
> in Road.java I have:
>
>
> In the main application I have
>
> import javax.persistence.Query;
> ...
> Query query = em.createNamedQuery("Road.intersects");
> int srid = -1;
> Polygon ext = EnvelopeAdapter.toPolygon(getExtents(), srid);
> if (query instanceof org.hibernate.ejb.QueryImpl)
> {
> org.hibernate.ejb.QueryImpl hibernateEjbQuery =
> (org.hibernate.ejb.QueryImpl) query;
> org.hibernate.Query hibernateQuery =
> hibernateEjbQuery.getHibernateQuery();
> Type geometryType = new CustomType(GeometryUserType.class, null);
>
> hibernateQuery.setParameter("extent", ext, geometryType);
> }
> List list = query.getResultList();
> int size = list.size();
>
> It works ok but its too many lines of code.
>
> Is there no way to make the first approach work?
>
> Also it seems that the query doesnt use the bounding box to  
> optimize the
> query, is it not possible to include the bounding box in named queries
> by default?
>
> It appears that the bounding box optimization can only be switched  
> on by
> using the criteria api, is that true?
>
> The query generated by hibernate is:
> select road0_.gid as gid1_, road0_.the_geom as the2_1_,  
> road0_.tipo_via
> as tipo3_1_, road0_.nomb_comun as nomb4_1_, road0_.objectId as
> objectId1_, road0_.municipio as municipio1_, road0_.fromLeft as
> fromLeft1_, road0_.toLeft as toLeft1_, road0_.fromRight as  
> fromRight1_,
> road0_.toRight as toRight1_, road0_.name as name1_,  
> road0_.shape_leng as
> shape12_1_, road0_.version as version1_, road0_.prefijo as prefijo1_
> from vias road0_ where intersects(road0_.the_geom, ?)=true
>
> Thanks in advance
>
> Andy Bailey
> www.hazlorealidad.com
>
>
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/ 
> hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Karel Maesen

Re: Finding features that intersect a bounding box with Hibernate Entity Manager

Reply Threaded More More options
Print post
Permalink
Hi Andy,

Just to let you know. I added the extent function to HQL, and added a  
SpatialProjects class that creates an "Extent" projection for spatial  
queries. The changes are in the source trunk and not yet a binary  
release version.

Regards,

Karel Maesen


On 11 Dec 2007, at 13:54, Karel Maesen wrote:

> Hi Andy,
>
> The Postgis extent function isn't registered as a HQL function.
> You're right that this would be very useful, and I'm going to include
> it when I finish the projection API. This will happen soon.
>
> As regards a work-around, I think the best way for you is to bypass
> Hibernate Spatial completely, and create a _native_ named for the  
> SQL :
>
> select astext(extent(the_geom) from road where the_geom &&
> geomfromtext(?) and intersects(the_geom, geomfromtext(?)).
>
> The parameters needs to be WKT Strings representing you search
> geometry. You can derive them using the jts.io WKTWriter class. The
> result will be a string that you can turn into a JTS geometry using
> the jts.io WKTReader class.
>
> Hope this helps,
>
> Karel Maesen
>
>
> On 11 Dec 2007, at 08:57, Andy Bailey wrote:
>
>>
>> I wanted to find the Roads intersecting a bounding box.
>>
>> I tried:
>> @NamedQuery(name = "Road.intersects", query = "select x from Road x
>> where intersects(x.geometry, :extent)=true")
>> I got the message ERROR:  parse error - invalid geometry
>> when I tried to set the parameter of the JPA query to an Envelope or
>> Polygon.
>> query.setParameter("extent", ext);
>>
>> The solution I have found seems too complicated, is there no easier
>> way
>> of doing this?
>>
>> in Road.java I have:
>>
>>
>> In the main application I have
>>
>> import javax.persistence.Query;
>> ...
>> Query query = em.createNamedQuery("Road.intersects");
>> int srid = -1;
>> Polygon ext = EnvelopeAdapter.toPolygon(getExtents(), srid);
>> if (query instanceof org.hibernate.ejb.QueryImpl)
>> {
>> org.hibernate.ejb.QueryImpl hibernateEjbQuery =
>> (org.hibernate.ejb.QueryImpl) query;
>> org.hibernate.Query hibernateQuery =
>> hibernateEjbQuery.getHibernateQuery();
>> Type geometryType = new CustomType(GeometryUserType.class, null);
>>
>> hibernateQuery.setParameter("extent", ext, geometryType);
>> }
>> List list = query.getResultList();
>> int size = list.size();
>>
>> It works ok but its too many lines of code.
>>
>> Is there no way to make the first approach work?
>>
>> Also it seems that the query doesnt use the bounding box to
>> optimize the
>> query, is it not possible to include the bounding box in named  
>> queries
>> by default?
>>
>> It appears that the bounding box optimization can only be switched
>> on by
>> using the criteria api, is that true?
>>
>> The query generated by hibernate is:
>> select road0_.gid as gid1_, road0_.the_geom as the2_1_,
>> road0_.tipo_via
>> as tipo3_1_, road0_.nomb_comun as nomb4_1_, road0_.objectId as
>> objectId1_, road0_.municipio as municipio1_, road0_.fromLeft as
>> fromLeft1_, road0_.toLeft as toLeft1_, road0_.fromRight as
>> fromRight1_,
>> road0_.toRight as toRight1_, road0_.name as name1_,
>> road0_.shape_leng as
>> shape12_1_, road0_.version as version1_, road0_.prefijo as prefijo1_
>> from vias road0_ where intersects(road0_.the_geom, ?)=true
>>
>> Thanks in advance
>>
>> Andy Bailey
>> www.hazlorealidad.com
>>
>>
>> _______________________________________________
>> hibernatespatial-users mailing list
>> [hidden email]
>> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/
>> hibernatespatial-users
>
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/ 
> hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Suray Ph

How to implements intersection and area2d functions?

Reply Threaded More More options
Print post
Permalink
Hello list,

I would like to use 'intersection' and 'area2d' functions.
I don't find how to implements these, could someone help me?

Thank's

Suray Philippe

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Karel Maesen

Re: How to implements intersection and area2d functions?

Reply Threaded More More options
Print post
Permalink
Hi Philippe,

Could you be a bit more specific? What database? How implement? As  
HQL functions?

Regards,

Karel

On 18 Dec 2007, at 18:04, Philippe Suray wrote:

> Hello list,
>
> I would like to use 'intersection' and 'area2d' functions.
> I don't find how to implements these, could someone help me?
>
> Thank's
>
> Suray Philippe
>
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/ 
> hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users