use of org.opengis.referencing.crs.CoordinateReferenceSystem in project OpenTripPlanner by opentripplanner.
the class DynamicTile method getSamples.
public Sample[] getSamples() {
Sample[] ret = new Sample[width * height];
long t0 = System.currentTimeMillis();
CoordinateReferenceSystem crs = gg.getCoordinateReferenceSystem2D();
try {
MathTransform tr = CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84);
// grid coordinate object to be reused for examining each cell
GridCoordinates2D coord = new GridCoordinates2D();
int i = 0, ns = 0;
for (int gy = 0; gy < height; gy++) {
for (int gx = 0; gx < width; gx++) {
coord.x = gx;
coord.y = gy;
// find coordinates for current raster cell in tile CRS
DirectPosition sourcePos = gg.gridToWorld(coord);
// convert coordinates in tile CRS to WGS84
tr.transform(sourcePos, sourcePos);
// axis order can vary
double lon = sourcePos.getOrdinate(0);
double lat = sourcePos.getOrdinate(1);
Sample s = ss.getSample(lon, lat);
if (s != null)
ns++;
ret[i] = s;
i++;
}
}
LOG.debug("finished preparing tile. number of samples: {}", ns);
} catch (Exception e) {
LOG.error(e.getMessage());
return null;
}
long t1 = System.currentTimeMillis();
LOG.debug("filled in tile image from SPT in {}msec", t1 - t0);
return ret;
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project OpenTripPlanner by opentripplanner.
the class SimpleIsochrone method makeContours.
/**
* @return a map from contour (isochrone) thresholds in seconds to geometries.
*/
private Map<Integer, Geometry> makeContours() throws Exception {
Map<Vertex, Double> points = makePoints();
if (points == null || points.isEmpty()) {
LOG.error("No destinations were reachable.");
return null;
}
/* Set up transforms into projected coordinates (necessary for buffering in meters) */
/* We could avoid projection by equirectangular approximation */
CoordinateReferenceSystem wgs = DefaultGeographicCRS.WGS84;
CoordinateReferenceSystem crs = CRS.decode(crsCode);
MathTransform toMeters = CRS.findMathTransform(wgs, crs, false);
MathTransform fromMeters = CRS.findMathTransform(crs, wgs, false);
GeometryFactory geomf = JTSFactoryFinder.getGeometryFactory();
/* One list of geometries for each contour */
Multimap<Integer, Geometry> bufferLists = ArrayListMultimap.create();
for (int c = 0; c < nContours; ++c) {
int thresholdSeconds = (c + 1) * contourSpacingMinutes * 60;
for (Map.Entry<Vertex, Double> vertexSeconds : points.entrySet()) {
double remainingSeconds = thresholdSeconds - vertexSeconds.getValue();
if (remainingSeconds > 60) {
// avoid degenerate geometries
double remainingMeters = remainingSeconds * request.walkSpeed;
Geometry point = geomf.createPoint(vertexSeconds.getKey().getCoordinate());
point = JTS.transform(point, toMeters);
Geometry buffer = point.buffer(remainingMeters);
bufferLists.put(thresholdSeconds, buffer);
}
}
}
/* Union the geometries at each contour threshold. */
Map<Integer, Geometry> contours = Maps.newHashMap();
for (Integer threshold : bufferLists.keys()) {
Collection<Geometry> buffers = bufferLists.get(threshold);
// make geometry collection
Geometry geom = geomf.buildGeometry(buffers);
// combine all individual buffers in this contour into one
geom = geom.union();
if (!resultsProjected)
geom = JTS.transform(geom, fromMeters);
contours.put(threshold, geom);
}
return contours;
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem 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.opengis.referencing.crs.CoordinateReferenceSystem in project jena by apache.
the class SRSInfoTest method testCheckAxisXY_WGS84.
/**
* Test of checkAxisXY method, of class SRSInfo.
*
* @throws org.opengis.util.FactoryException
*/
@Test
public void testCheckAxisXY_WGS84() throws FactoryException {
CoordinateReferenceSystem crs = CRS.forCode(SRS_URI.WGS84_CRS);
Boolean expResult = false;
Boolean result = SRSInfo.checkAxisXY(crs);
assertEquals(expResult, result);
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project jena by apache.
the class SRSInfoTest method testBuildDomainEnvelope_WGS84.
/**
* Test of buildDomainEnvelope method, of class SRSInfo.
*
* @throws org.opengis.util.FactoryException
*/
@Test
public void testBuildDomainEnvelope_WGS84() throws FactoryException {
String srsURI = SRS_URI.WGS84_CRS;
CoordinateReferenceSystem crs = CRS.forCode(srsURI);
Boolean isAxisXY = SRSInfo.checkAxisXY(crs);
Envelope expResult = new Envelope(-180, 180, -90, 90);
Envelope result = SRSInfo.buildDomainEnvelope(crs, isAxisXY);
assertEquals(expResult, result);
}
Aggregations