Spatial Query in Eclipse

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Spatial Query in Eclipse

JPHespanha-3
FYI:
 
I have managed to run the second part of the Hibernate Spatial Tutorial, the Spatial Query, on Eclipse.
The results on the Console were:

[java] Filter is : POLYGON ((1 1, 20 1, 20 20, 1 20, 1 1))

[java] Hibernate: select this_.EVENT_ID as EVENT1_0_0_, this_.EVENT_DATE as EVENT2_0_0_, this_.title as title0_0_, this_.LOC as LOC0_0_ from EVENTS this_ where (this_.LOC && ? AND within(this_.LOC, ?))

[java] 09:48:56,262 DEBUG LongType:122 - returning '1' as column: EVENT1_0_0_

[java] 09:48:56,262 DEBUG TimestampType:122 - returning '2007-11-23 18:21:16' as column: EVENT2_0_0_

[java] 09:48:56,262 DEBUG StringType:122 - returning 'My Event' as column: title0_0_

[java] Event: My Event, Time: 2007-11-23 18:21:16.245, Location: POINT (10 15)

[java] 09:48:56,293 INFO SessionFactoryImpl:767 - closing

[java] 09:48:56,293 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:postgresql://localhost:5432/events

BUILD SUCCESSFUL

Total time: 4 seconds

However, I had to introduce some changes to the EventManager class in order to compile under Eclipse (perhaps the difference lies on the version of the SDK). Here is the code I used on the main method:

if (args[0].equals("store")) {

mgr.createAndStoreEvent("My Event", new Date(), args[1]);

} else if (args[0].equals("find")){

List<Event> events = mgr.find(args[1]);

for (int i = 0; i < events.size(); i++ ){

Event event = events.get(i);

System.out.println("Event: " + event.getTitle() +

", Time: " + event.getDate() +

", Location: " + event.getLocation());

}

And on the find method (added the specification of the list type):

/**

* @param wktFilter

* @return List of Event objects obeying to a Criteria

* <p>This example code was taken from the Spatial Queries

* section of the Tutorial.</p>

*/

private List<Event> find(String wktFilter){

WKTReader fromText = new WKTReader();

Geometry filter = null;

try{

filter = fromText.read(wktFilter);

} catch(ParseException e){

throw new RuntimeException("Not a WKT String:" + wktFilter);

}

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

System.out.println("Filter is : " + filter);

Criteria testCriteria = session.createCriteria(Event.class);

testCriteria.add(SpatialRestrictions.within("location", filter, filter));

List<Event> results = testCriteria.list();

session.getTransaction().commit();

return results;

}

Regards,

J.P.Hespanha

 


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