Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting lat/long to JTS?

Tags:

I am trying to integrate hibernate spatial with JPA for Geo searches. I have been referencing the tutorial on the official site (I am not associated with hibernatespatial).

The tutorial, unfortunately, does not cover how to create a Point instance from a latitude/longitude pair. I'm attempting to do this here, but I am still not sure if this is this the right way to convert a latitude/longitude pair to a JTS Point instance:

import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.Point; import org.geotools.geometry.jts.JTSFactoryFinder; import org.hibernate.annotations.Type; import javax.persistence.*;  @Entity public class Location {      private Double latitude;      private Double longitude;      @Type(type = "org.hibernatespatial.GeometryUserType")     private Point coordinates;      private final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);      @PrePersist     @PreUpdate     public void updateCoordinate() {         if (this.latitude == null || this.longitude == null) {             this.coordinates = null;         } else {             this.coordinates = geometryFactory.createPoint(new Coordinate(latitude, longitude));         }     }      public Double getLatitude() {         return latitude;     }      public void setLatitude(Double latitude) {         this.latitude = latitude;     }      public Double getLongitude() {         return longitude;     }      public void setLongitude(Double longitude) {         this.longitude = longitude;     } } 
like image 208
Werner Altewischer Avatar asked Dec 06 '11 17:12

Werner Altewischer


1 Answers

JTS doesn't care what your point's units or coordinate system is.

However, it does assume that the coordinates are on a Cartesian plane, so some geometry operations such as distance calculations may be inaccurate over long distances. (They don't yet support geodetic calculations.)

It should be fine for simple storage uses.

However, an important point to note is that the longitude is the X value and the latitude the Y value. So we say "lat/long", but JTS will expect it in the order "long/lat". So you should be using geometryFactory.createPoint(new Coordinate(longitude, latitude))

like image 123
Gnat Avatar answered Nov 18 '22 00:11

Gnat