Fixed it.
The problem was a weird bug that was reversing latitude/longitude at some place.
Anyway, in general JTS can be used for calculations on latitude/longitude data. Although JTS doesn't try to project the coordinates before applying any spatial functions, all of them work fine (including the index trees) for smaller areas... I haven't really tested this with large ones but according to my findings - the larger the area, the less accurate the measures will be. This makes sense taking into account the distortion caused by the usage of spherical coordinates as if they were Cartesian.
Note that PostGIS has a slightly different behaviour. Whenever the coordinates are stored as WG84 and you use a function that is not yet natively implemented for spherical data the following happens - a suitable projection is automatically chosen and applied by PostGIS. Then the function is executed on the resulting projection rather than performing calculations directly on the sphere.
This is a very hard and long winded way of doing it, you really should use GeoTools' datastores (which abstract all this work away)
So assuming that what you actually want is to write some geometries and attributes to PostGIS you can do this:
Decide what is in your dataset and convert your objects to Features (usually SimpleFeatures):
SimpleFeatureType schema = null;
try {
schema = DataUtilities.createType("", "Location",
"locations:LineString:srid=4326," + // <- the geometry
// attribute:
"id:Integer" // a number attribute
//add more attributes here to match your dataset
);
} catch (SchemaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(schema);
//for each feature you have do:
featureBuilder.add(geom);
featureBuilder.add(name);
featureBuilder.add(number);
//+ all the other attributes (probably use a loop)
SimpleFeature feature = featureBuilder.buildFeature(null);
//put them into a collection feats and then convert it to a FeatureCollection
SimpleFeatureCollection collection = new ListFeatureCollection(shpType,
feats);
Now you need a PostGIS datastore (or a CSV or ShapeFile if you prefer):
Map<String,Object> params = new HashMap<String,Object>();
params.put( "dbtype", "postgis");
params.put( "host", "localhost");
params.put( "port", 5432);
params.put( "schema", "public");
params.put( "database", "database");
params.put( "user", "postgres");
params.put( "passwd", "postgres");
DataStore dataStore=DataStoreFinder.getDataStore(params);
Transaction transaction = new DefaultTransaction("create");
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore
.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
List<FeatureId> ids = featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
dataStore.dispose();
return true;
}
Best Answer
They are the same project JTS moved to Location Tech on Nov 3, 2016. AFAIK (and looking at the github repo) there has not yet been a released version from the LocationTech branch.
So you should probably continue to use the Vivid Solution's release until a new version comes out, unless you have to be on the bleeding edge.
UPDATE 12/2018
There is now a JTS 1.15 release from the Location Tech version of the project. Follow this upgrade guide for details of how to make the changeover.