use of org.geotools.geometry.DirectPosition2D in project OpenTripPlanner by opentripplanner.
the class CSVPopulation method createIndividuals.
@Override
public void createIndividuals() {
try {
CsvReader reader = new CsvReader(sourceFilename, ',', Charset.forName("UTF8"));
if (skipHeaders) {
reader.readHeaders();
}
// deal with non-WGS84 data
MathTransform mathTransform = null;
boolean transform = false;
CoordinateReferenceSystem destCrs = CRS.decode("EPSG:4326");
Boolean latLon = null;
if (crs != null) {
CoordinateReferenceSystem sourceCrs = CRS.decode(crs);
// lat,lon: geotools default
if (CRS.getAxisOrder(destCrs) == CRS.AxisOrder.NORTH_EAST)
latLon = true;
else if (CRS.getAxisOrder(destCrs) == CRS.AxisOrder.EAST_NORTH)
latLon = false;
else
throw new UnsupportedOperationException("Coordinate axis order for WGS 84 unknown.");
if (!destCrs.equals(sourceCrs)) {
transform = true;
// find the transformation, being strict about datums &c.
mathTransform = CRS.findMathTransform(sourceCrs, destCrs, false);
}
}
while (reader.readRecord()) {
double y = Double.parseDouble(reader.get(yCol));
double x = Double.parseDouble(reader.get(xCol));
double lon, lat;
if (transform) {
DirectPosition2D orig = new DirectPosition2D(x, y);
DirectPosition2D transformed = new DirectPosition2D();
mathTransform.transform(orig, transformed);
// x: lat, y: lon. This seems backwards but is the way Geotools does it.
if (latLon) {
lon = transformed.getY();
lat = transformed.getX();
} else // x: lon, y: lat
{
lon = transformed.getX();
lat = transformed.getY();
}
} else {
lon = x;
lat = y;
}
String label = reader.get(labelCol);
Double input = Double.parseDouble(reader.get(inputCol));
// at this point x and y are expressed in WGS84
Individual individual = new Individual(label, lon, lat, input);
this.addIndividual(individual);
}
reader.close();
} catch (Exception e) {
LOG.error("exception while loading individuals from CSV file", e);
}
}
use of org.geotools.geometry.DirectPosition2D in project collect by openforis.
the class GeoToolsCoordinateOperations method convert.
private Coordinate convert(double x, double y, String fromSrsId, String toSrsId) {
try {
DirectPosition src = new DirectPosition2D(x, y);
MathTransform transform = getOrCreateTransform(fromSrsId, toSrsId);
if (transform == null) {
if (LOG.isErrorEnabled()) {
LOG.error("Unknown CRS: " + toSrsId);
}
return new Coordinate(0d, 0d, toSrsId);
} else {
DirectPosition directPosition = transform.transform(src, null);
double[] coord = directPosition.getCoordinate();
return new Coordinate(coord[0], coord[1], toSrsId);
}
} catch (Throwable t) {
if (LOG.isErrorEnabled()) {
LOG.error("Error converting lat lon to web marcator: lat=" + y + " lon=" + x, t);
}
return new Coordinate(0d, 0d, toSrsId);
}
}
use of org.geotools.geometry.DirectPosition2D in project structr by structr.
the class LatLonToUTMFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (arrayHasLengthAndAllElementsNotNull(sources, 2)) {
final Double lat = getDoubleOrNull(sources[0]);
final Double lon = getDoubleOrNull(sources[1]);
if (lat != null && lon != null) {
try {
final StringBuilder epsg = new StringBuilder("EPSG:32");
final int utmZone = getUTMZone(lat, lon);
if (lat < 0.0) {
// southern hemisphere
epsg.append("7");
} else {
// northern hemisphere
epsg.append("6");
}
if (utmZone < 10) {
epsg.append("0");
}
epsg.append(utmZone);
final CoordinateReferenceSystem src = CRS.decode("EPSG:4326");
final CoordinateReferenceSystem dst = CRS.decode(epsg.toString());
final MathTransform transform = CRS.findMathTransform(src, dst, true);
final DirectPosition sourcePt = new DirectPosition2D(lat, lon);
final DirectPosition targetPt = transform.transform(sourcePt, null);
final String code = dst.getName().getCode();
final int pos = code.lastIndexOf(" ") + 1;
final String zoneName = code.substring(pos, code.length() - 1);
final String band = getLatitudeBand(lat, lon);
final StringBuilder buf = new StringBuilder();
buf.append(zoneName);
buf.append(band);
buf.append(" ");
buf.append((int) Math.rint(targetPt.getOrdinate(0)));
buf.append(" ");
buf.append((int) Math.rint(targetPt.getOrdinate(1)));
// return result
return buf.toString();
} catch (Throwable t) {
logger.warn("", t);
}
} else {
logger.warn("Invalid argument(s), cannot convert to double: {}, {}", new Object[] { sources[0], sources[1] });
}
}
return usage(ctx != null ? ctx.isJavaScriptContext() : false);
}
use of org.geotools.geometry.DirectPosition2D in project sldeditor by robward-scisys.
the class WKTPointModelTest method testPopulateWKTSegmentList.
/**
* Test method for
* {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPointModel#setWKTType(com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTType)}.
* Test method for
* {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPointModel#populate(com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTSegmentList)}.
*/
@Test
public void testPopulateWKTSegmentList() {
WKTSegmentList segmentList = new WKTSegmentList();
DirectPosition pos1 = new DirectPosition2D(1.0, 1.0);
DirectPosition pos2 = new DirectPosition2D(2.0, 2.0);
DirectPosition pos3 = new DirectPosition2D(3.0, 3.0);
DirectPosition pos4 = new DirectPosition2D(4.0, 4.0);
segmentList.addPoint(new WKTPoint(pos1));
segmentList.addPoint(new WKTPoint(pos2));
segmentList.addPoint(new WKTPoint(pos3));
segmentList.addPoint(new WKTPoint(pos4));
WKTPointModel model = new WKTPointModel();
WKTType wktType = new WKTType("name", false, 5, "", false, false);
model.setWKTType(wktType);
model.populate(null);
assertTrue(model.getRowCount() == 0);
model.populate(segmentList);
assertTrue(model.getRowCount() == 4);
model.populate(segmentList);
assertTrue(model.getRowCount() == 4);
segmentList.addPoint(new WKTPoint(pos1));
assertTrue(model.getRowCount() == 5);
// Set WKTType to null is the same as first and last points flag = false
model.setWKTType(null);
model.populate(segmentList);
assertTrue(model.getRowCount() == 5);
}
use of org.geotools.geometry.DirectPosition2D in project sldeditor by robward-scisys.
the class WKTPointTest method testWKTPointDirectPosition.
/**
* Test method for {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPoint#WKTPoint(org.opengis.geometry.DirectPosition)}.
* Test method for {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPoint#WKTPoint()}.
* Test method for {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPoint#getX()}.
* Test method for {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPoint#getY()}.
* Test method for {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPoint#setX(double)}.
* Test method for {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPoint#setY(double)}.
* Test method for {@link com.sldeditor.ui.detail.vendor.geoserver.marker.wkt.WKTPoint#equals(java.lang.Object)}.
*/
@Test
public void testWKTPointDirectPosition() {
WKTPoint point = new WKTPoint();
assertTrue(Math.abs(point.getX() - 0.0) < 0.001);
assertTrue(Math.abs(point.getY() - 0.0) < 0.001);
point = new WKTPoint(null);
assertTrue(Math.abs(point.getX() - 0.0) < 0.001);
assertTrue(Math.abs(point.getY() - 0.0) < 0.001);
double x = 45.2;
double y = -3.1;
DirectPosition pos = new DirectPosition2D(x, y);
point = new WKTPoint(pos);
assertTrue(Math.abs(point.getX() - x) < 0.001);
assertTrue(Math.abs(point.getY() - y) < 0.001);
x = 42.0;
point.setX(x);
assertTrue(Math.abs(point.getX() - x) < 0.001);
y = 42.0;
point.setY(y);
assertTrue(Math.abs(point.getY() - y) < 0.001);
DirectPosition pos2 = new DirectPosition2D(x, y);
WKTPoint point2 = new WKTPoint(pos2);
assertTrue(point.equals(point2));
point2.setX(3.14);
assertFalse(point.equals(point2));
assertFalse(point.equals(null));
assertFalse(point.equals(pos2));
assertTrue(point.equals(point));
assertTrue(point.hashCode() != point2.hashCode());
}
Aggregations