Search in sources :

Example 81 with CoordinateReferenceSystem

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;
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) MathTransform(org.opengis.referencing.operation.MathTransform) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 82 with CoordinateReferenceSystem

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;
}
Also used : StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) MathTransform(org.opengis.referencing.operation.MathTransform) Point(com.vividsolutions.jts.geom.Point) Geometry(com.vividsolutions.jts.geom.Geometry) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Map(java.util.Map) HashMap(java.util.HashMap)

Example 83 with CoordinateReferenceSystem

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);
    }
}
Also used : CsvReader(com.csvreader.CsvReader) MathTransform(org.opengis.referencing.operation.MathTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) DirectPosition2D(org.geotools.geometry.DirectPosition2D)

Example 84 with CoordinateReferenceSystem

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);
}
Also used : CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Test(org.junit.Test)

Example 85 with CoordinateReferenceSystem

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);
}
Also used : CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Envelope(org.locationtech.jts.geom.Envelope) Test(org.junit.Test)

Aggregations

CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)210 Test (org.junit.Test)80 MathTransform (org.opengis.referencing.operation.MathTransform)32 FactoryException (org.opengis.referencing.FactoryException)25 CoordinateOperation (org.opengis.referencing.operation.CoordinateOperation)24 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)23 Geometry (com.vividsolutions.jts.geom.Geometry)21 TransformException (org.opengis.referencing.operation.TransformException)21 DependsOnMethod (org.apache.sis.test.DependsOnMethod)19 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)13 Geometry (org.locationtech.jts.geom.Geometry)11 FactoryException (org.opengis.util.FactoryException)11 SimpleFeature (org.opengis.feature.simple.SimpleFeature)9 DirectPosition (org.opengis.geometry.DirectPosition)9 GeographicCRS (org.opengis.referencing.crs.GeographicCRS)9 VerticalCRS (org.opengis.referencing.crs.VerticalCRS)9 CoordinateSystemAxis (org.opengis.referencing.cs.CoordinateSystemAxis)9 ArrayList (java.util.ArrayList)8 GeometryType (org.opengis.feature.type.GeometryType)8 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)7