use of org.opengis.referencing.crs.CoordinateReferenceSystem in project spatial-portal by AtlasOfLivingAustralia.
the class Util method createCircle.
public static String createCircle(double x, double y, final double radius, int sides) {
try {
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
String wkt4326 = "GEOGCS[" + "\"WGS 84\"," + " DATUM[" + " \"WGS_1984\"," + " SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," + " TOWGS84[0,0,0,0,0,0,0]," + " AUTHORITY[\"EPSG\",\"6326\"]]," + " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]]," + " UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]]," + " AXIS[\"Lat\",NORTH]," + " AXIS[\"Long\",EAST]," + " AUTHORITY[\"EPSG\",\"4326\"]]";
String wkt900913 = "PROJCS[\"WGS84 / Google Mercator\", " + " GEOGCS[\"WGS 84\", " + " DATUM[\"World Geodetic System 1984\", " + " SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]], " + " AUTHORITY[\"EPSG\",\"6326\"]], " + " PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], " + " UNIT[\"degree\", 0.017453292519943295], " + " AXIS[\"Longitude\", EAST], " + " AXIS[\"Latitude\", NORTH], " + " AUTHORITY[\"EPSG\",\"4326\"]], " + " PROJECTION[\"Mercator_1SP\"], " + " PARAMETER[\"semi_minor\", 6378137.0], " + " PARAMETER[\"latitude_of_origin\", 0.0]," + " PARAMETER[\"central_meridian\", 0.0], " + " PARAMETER[\"scale_factor\", 1.0], " + " PARAMETER[\"false_easting\", 0.0], " + " PARAMETER[\"false_northing\", 0.0], " + " UNIT[\"m\", 1.0], " + " AXIS[\"x\", EAST], " + " AXIS[\"y\", NORTH], " + " AUTHORITY[\"EPSG\",\"3857\"]] ";
CoordinateReferenceSystem wgsCRS = CRS.parseWKT(wkt4326);
CoordinateReferenceSystem googleCRS = CRS.parseWKT(wkt900913);
MathTransform transform = CRS.findMathTransform(wgsCRS, googleCRS);
Point point = geometryFactory.createPoint(new Coordinate(y, x));
Geometry geom = JTS.transform(point, transform);
Point gPoint = geometryFactory.createPoint(new Coordinate(geom.getCoordinate()));
LOGGER.debug("Google point:" + gPoint.getCoordinate().x + "," + gPoint.getCoordinate().y);
MathTransform reverseTransform = CRS.findMathTransform(googleCRS, wgsCRS);
Coordinate[] coords = new Coordinate[sides + 1];
for (int i = 0; i < sides; i++) {
double angle = ((double) i / (double) sides) * Math.PI * 2.0;
double dx = Math.cos(angle) * radius;
double dy = Math.sin(angle) * radius;
geom = JTS.transform(geometryFactory.createPoint(new Coordinate(gPoint.getCoordinate().x + dx, gPoint.getCoordinate().y + dy)), reverseTransform);
coords[i] = new Coordinate(geom.getCoordinate().y, geom.getCoordinate().x);
}
coords[sides] = coords[0];
LinearRing ring = geometryFactory.createLinearRing(coords);
Polygon polygon = geometryFactory.createPolygon(ring, null);
WKTWriter writer = new WKTWriter();
String wkt = writer.write(polygon);
return wkt.replaceAll(StringConstants.POLYGON + " ", StringConstants.POLYGON).replaceAll(", ", ",");
} catch (Exception e) {
LOGGER.debug("Circle fail!");
return StringConstants.NONE;
}
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.
the class WKTPreferencesCRSFactory method createCoordinateReferenceSystem.
/**
* @see CRSAuthorityFactory#createCoordinateReferenceSystem(String)
*/
@Override
public synchronized CoordinateReferenceSystem createCoordinateReferenceSystem(String code) throws FactoryException {
if (code == null) {
return null;
}
if (!code.startsWith(AUTHORITY_PREFIX)) {
throw new NoSuchAuthorityCodeException("This factory only understands EPSG codes", AUTHORITY, // $NON-NLS-1$
code);
}
final String epsgNumber = code.substring(code.indexOf(':') + 1).trim();
if (cache.containsKey(epsgNumber)) {
CoordinateReferenceSystem value = cache.get(epsgNumber);
if (value != null) {
// CRS was already created
return value;
}
}
try {
node.sync();
} catch (BackingStoreException e) {
// $NON-NLS-1$
_log.warn("Error synchronizing preferences", e);
}
String wkt = node.get(epsgNumber, null);
if (wkt == null) {
// $NON-NLS-1$
throw new NoSuchAuthorityCodeException("Unknown EPSG code", AUTHORITY, code);
}
if (wkt.indexOf(epsgNumber) == -1) {
wkt = wkt.trim();
wkt = wkt.substring(0, wkt.length() - 1);
// $NON-NLS-1$ //$NON-NLS-2$
wkt += ",AUTHORITY[\"EPSG\",\"" + epsgNumber + "\"]]";
// $NON-NLS-1$ //$NON-NLS-2$
_log.warn("EPSG:" + epsgNumber + " lacks a proper identifying authority in its Well-Known Text. It is being added programmatically.");
}
try {
CoordinateReferenceSystem crs = crsFactory.createFromWKT(wkt);
cache.put(epsgNumber, crs);
return crs;
} catch (FactoryException fex) {
throw fex;
}
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.
the class PostGISGeometries method convertGeometry.
@Override
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, PGConnection pgconn) throws Exception {
PGgeometry pGeometry = null;
// Transform from sourceCRS to targetCRS
GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
// transform
CoordinateReferenceSystem targetCRS = null;
String authName = columnTypeMetadata.getAuthName();
if (authName != null && authName.equals("EPSG")) {
// PostGIS assumes lon/lat
targetCRS = CRS.decode(authName + ":" + columnTypeMetadata.getSrs(), true);
} else {
String wkt = columnTypeMetadata.getSrsText();
if (wkt != null && !wkt.isEmpty()) {
targetCRS = CRS.parseWKT(wkt);
}
}
Geometry targetGeometry;
if (targetCRS != null) {
MathTransform transform = CRS.findMathTransform(geom.getCRSDefinition().getCRS(), targetCRS);
targetGeometry = JTS.transform(geom.getGeometry(), transform);
} else {
targetGeometry = geom.getGeometry();
}
// Convert the jts Geometry to postgis PGgeometry and set the SRSID
pGeometry = new PGgeometry(targetGeometry.toText());
try {
pGeometry.getGeometry().setSrid(Integer.parseInt(columnTypeMetadata.getSrs()));
} catch (Exception e) {
// ignore
}
return pGeometry;
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.
the class GeotoolsConverter method convert.
/**
* @see GeoConverter#convert(GeoPosition, int)
*/
@Override
public GeoPosition convert(GeoPosition pos, int targetEpsg) throws IllegalGeoPositionException {
if (targetEpsg == pos.getEpsgCode())
return new GeoPosition(pos.getX(), pos.getY(), targetEpsg);
try {
CoordinateReferenceSystem sourceCRS = getCRS(pos.getEpsgCode());
CoordinateReferenceSystem targetCRS = getCRS(targetEpsg);
MathTransform math = getTransform(pos.getEpsgCode(), targetEpsg, sourceCRS, targetCRS);
int dimension = sourceCRS.getCoordinateSystem().getDimension();
DirectPosition pt1;
boolean flipSource = flipCRS(sourceCRS);
switch(dimension) {
case 2:
pt1 = new GeneralDirectPosition((flipSource) ? (pos.getY()) : (pos.getX()), (flipSource) ? (pos.getX()) : (pos.getY()));
break;
case 3:
pt1 = new GeneralDirectPosition((flipSource) ? (pos.getY()) : (pos.getX()), (flipSource) ? (pos.getX()) : (pos.getY()), 0);
break;
default:
log.error("Unsupported dimension: " + dimension);
throw new IllegalArgumentException("Unsupported dimension: " + dimension);
}
DirectPosition pt2 = math.transform(pt1, null);
if (flipCRS(targetCRS))
return new GeoPosition(pt2.getOrdinate(1), pt2.getOrdinate(0), targetEpsg);
else
return new GeoPosition(pt2.getOrdinate(0), pt2.getOrdinate(1), targetEpsg);
} catch (Exception e) {
throw new IllegalGeoPositionException(e);
}
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.
the class GeotoolsConverter method getCRS.
private CoordinateReferenceSystem getCRS(int epsg) throws NoSuchAuthorityCodeException, FactoryException {
CoordinateReferenceSystem r = crsMap.get(epsg);
if (r == null) {
r = CRS.decode("EPSG:" + epsg, true);
crsMap.put(epsg, r);
}
return r;
}
Aggregations