Search in sources :

Example 96 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project OpenTripPlanner by opentripplanner.

the class WalkableAreaBuilder method createNamedAreas.

private void createNamedAreas(AreaEdgeList edgeList, Ring ring, Collection<Area> areas) {
    Polygon containingArea = ring.toJtsPolygon();
    for (Area area : areas) {
        Geometry intersection = containingArea.intersection(area.toJTSMultiPolygon());
        if (intersection.getArea() == 0) {
            continue;
        }
        NamedArea namedArea = new NamedArea();
        OSMWithTags areaEntity = area.parent;
        int cls = StreetEdge.CLASS_OTHERPATH;
        cls |= OSMFilter.getStreetClasses(areaEntity);
        namedArea.setStreetClass(cls);
        String id = "way (area) " + areaEntity.getId() + " (splitter linking)";
        I18NString name = __handler.getNameForWay(areaEntity, id);
        namedArea.setName(name);
        WayProperties wayData = wayPropertySet.getDataForWay(areaEntity);
        Double safety = wayData.getSafetyFeatures().first;
        namedArea.setBicycleSafetyMultiplier(safety);
        namedArea.setOriginalEdges(intersection);
        StreetTraversalPermission permission = OSMFilter.getPermissionsForEntity(areaEntity, StreetTraversalPermission.PEDESTRIAN_AND_BICYCLE);
        namedArea.setPermission(permission);
        edgeList.addArea(namedArea);
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) NamedArea(org.opentripplanner.routing.edgetype.NamedArea) I18NString(org.opentripplanner.util.I18NString) OSMWithTags(org.opentripplanner.openstreetmap.model.OSMWithTags) NamedArea(org.opentripplanner.routing.edgetype.NamedArea) StreetTraversalPermission(org.opentripplanner.routing.edgetype.StreetTraversalPermission) LineString(com.vividsolutions.jts.geom.LineString) I18NString(org.opentripplanner.util.I18NString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) VLPolygon(org.opentripplanner.visibility.VLPolygon) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) VisibilityPolygon(org.opentripplanner.visibility.VisibilityPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Point(com.vividsolutions.jts.geom.Point) VLPoint(org.opentripplanner.visibility.VLPoint)

Example 97 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project OpenTripPlanner by opentripplanner.

the class Ring method toJtsPolygon.

public Polygon toJtsPolygon() {
    if (jtsPolygon != null) {
        return jtsPolygon;
    }
    GeometryFactory factory = GeometryUtils.getGeometryFactory();
    LinearRing shell;
    try {
        shell = factory.createLinearRing(toCoordinates(geometry));
    } catch (IllegalArgumentException e) {
        throw new RingConstructionException();
    }
    // we need to merge connected holes here, because JTS does not believe in
    // holes that touch at multiple points (and, weirdly, does not have a method
    // to detect this other than this crazy DE-9IM stuff
    List<Polygon> polygonHoles = new ArrayList<Polygon>();
    for (Ring ring : holes) {
        LinearRing linearRing = factory.createLinearRing(toCoordinates(ring.geometry));
        Polygon polygon = factory.createPolygon(linearRing, new LinearRing[0]);
        for (Iterator<Polygon> it = polygonHoles.iterator(); it.hasNext(); ) {
            Polygon otherHole = it.next();
            if (otherHole.relate(polygon, "F***1****")) {
                polygon = (Polygon) polygon.union(otherHole);
                it.remove();
            }
        }
        polygonHoles.add(polygon);
    }
    ArrayList<LinearRing> lrholelist = new ArrayList<LinearRing>(polygonHoles.size());
    for (Polygon hole : polygonHoles) {
        Geometry boundary = hole.getBoundary();
        if (boundary instanceof LinearRing) {
            lrholelist.add((LinearRing) boundary);
        } else {
            // this is a case of a hole inside a hole. OSM technically
            // allows this, but it would be a giant hassle to get right. So:
            LineString line = hole.getExteriorRing();
            LinearRing ring = factory.createLinearRing(line.getCoordinates());
            lrholelist.add(ring);
        }
    }
    LinearRing[] lrholes = lrholelist.toArray(new LinearRing[lrholelist.size()]);
    jtsPolygon = factory.createPolygon(shell, lrholes);
    return jtsPolygon;
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) ArrayList(java.util.ArrayList) Geometry(com.vividsolutions.jts.geom.Geometry) LineString(com.vividsolutions.jts.geom.LineString) LinearRing(com.vividsolutions.jts.geom.LinearRing) LinearRing(com.vividsolutions.jts.geom.LinearRing) VLPolygon(org.opentripplanner.visibility.VLPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 98 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project OpenTripPlanner by opentripplanner.

the class ShapefilePopulation method createIndividuals.

@Override
public void createIndividuals() {
    String filename = this.sourceFilename;
    LOG.debug("Loading population from shapefile {}", filename);
    LOG.debug("Feature attributes: input data in {}, labeled with {}", inputAttribute, labelAttribute);
    try {
        File file = new File(filename);
        if (!file.exists())
            throw new RuntimeException("Shapefile does not exist.");
        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();
        CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS();
        CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true);
        Query query = new Query();
        query.setCoordinateSystem(sourceCRS);
        query.setCoordinateSystemReproject(WGS84);
        SimpleFeatureCollection featureCollection = featureSource.getFeatures(query);
        SimpleFeatureIterator it = featureCollection.features();
        int i = 0;
        while (it.hasNext()) {
            SimpleFeature feature = it.next();
            Geometry geom = (Geometry) feature.getDefaultGeometry();
            Point point = null;
            if (geom instanceof Point) {
                point = (Point) geom;
            } else if (geom instanceof Polygon) {
                point = ((Polygon) geom).getCentroid();
            } else if (geom instanceof MultiPolygon) {
                point = ((MultiPolygon) geom).getCentroid();
            } else {
                throw new RuntimeException("Shapefile must contain either points or polygons.");
            }
            String label;
            if (labelAttribute == null) {
                label = Integer.toString(i);
            } else {
                label = feature.getAttribute(labelAttribute).toString();
            }
            double input = 0.0;
            if (inputAttribute != null) {
                Number n = (Number) feature.getAttribute(inputAttribute);
                input = n.doubleValue();
            }
            Individual individual = new Individual(label, point.getX(), point.getY(), input);
            this.addIndividual(individual);
            i += 1;
        }
        LOG.debug("loaded {} features", i);
        it.close();
    } catch (Exception ex) {
        LOG.error("Error loading population from shapefile: {}", ex.getMessage());
        throw new RuntimeException(ex);
    }
    LOG.debug("Done loading shapefile.");
}
Also used : Query(org.geotools.data.Query) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Geometry(com.vividsolutions.jts.geom.Geometry) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) File(java.io.File) FileDataStore(org.geotools.data.FileDataStore)

Example 99 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project OpenTripPlanner by opentripplanner.

the class PointFeature method setGeom.

public void setGeom(Geometry geom) throws EmptyPolygonException, UnsupportedGeometryException {
    if (geom instanceof MultiPolygon) {
        if (geom.isEmpty()) {
            throw new EmptyPolygonException();
        }
        if (geom.getNumGeometries() > 1) {
        // LOG.warn("Multiple polygons in MultiPolygon, using only the first.");
        // TODO percolate this warning up somehow
        }
        this.geom = geom.getGeometryN(0);
    } else if (geom instanceof Point || geom instanceof Polygon) {
        this.geom = geom;
    } else {
        throw new UnsupportedGeometryException("Non-point, non-polygon Geometry, not supported.");
    }
    // cache a representative point
    Point point = geom.getCentroid();
    this.lat = point.getY();
    this.lon = point.getX();
}
Also used : MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Point(com.vividsolutions.jts.geom.Point) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 100 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project alliance by codice.

the class NitfImageTransformer method handleSegments.

private void handleSegments(NitfSegmentsFlow nitfSegmentsFlow, Metacard metacard) {
    validateArgument(nitfSegmentsFlow, "nitfSegmentsFlow");
    validateArgument(metacard, "metacard");
    List<Polygon> polygonList = new ArrayList<>();
    List<Date> imageDateAndTimeList = new ArrayList<>();
    nitfSegmentsFlow.forEachImageSegment(segment -> handleImageSegmentHeader(metacard, segment, polygonList, imageDateAndTimeList)).forEachGraphicSegment(segment -> handleSegmentHeader(metacard, segment, GraphicAttribute.values())).forEachTextSegment(segment -> handleSegmentHeader(metacard, segment, TextAttribute.values())).forEachSymbolSegment(segment -> handleSegmentHeader(metacard, segment, SymbolAttribute.values())).forEachLabelSegment(segment -> handleSegmentHeader(metacard, segment, LabelAttribute.values())).end();
    // Set GEOGRAPHY from discovered polygons
    if (polygonList.size() == 1) {
        metacard.setAttribute(new AttributeImpl(Core.LOCATION, polygonList.get(0).toText()));
    } else if (polygonList.size() > 1) {
        Polygon[] polyAry = polygonList.toArray(new Polygon[polygonList.size()]);
        MultiPolygon multiPolygon = GEOMETRY_FACTORY.createMultiPolygon(polyAry);
        metacard.setAttribute(new AttributeImpl(Core.LOCATION, multiPolygon.toText()));
    }
    // Set start, effective, and end from discovered imageDateAndTimes
    if (!imageDateAndTimeList.isEmpty()) {
        LOGGER.trace("Discovered imageDateTimes of the image segments: {}", imageDateAndTimeList);
        final Date firstDateAndTime = imageDateAndTimeList.get(0);
        final Date lastDateAndTime = imageDateAndTimeList.get(imageDateAndTimeList.size() - 1);
        LOGGER.trace(SETTING_THE_METACARD_ATTRIBUTE_TO, Metacard.EFFECTIVE, firstDateAndTime);
        metacard.setAttribute(new AttributeImpl(Metacard.EFFECTIVE, firstDateAndTime));
        LOGGER.trace(SETTING_THE_METACARD_ATTRIBUTE_TO, ddf.catalog.data.types.DateTime.START, firstDateAndTime);
        metacard.setAttribute(new AttributeImpl(ddf.catalog.data.types.DateTime.START, firstDateAndTime));
        LOGGER.trace(SETTING_THE_METACARD_ATTRIBUTE_TO, ddf.catalog.data.types.DateTime.END, lastDateAndTime);
        metacard.setAttribute(new AttributeImpl(ddf.catalog.data.types.DateTime.END, lastDateAndTime));
    }
}
Also used : LinearRing(com.vividsolutions.jts.geom.LinearRing) Coordinate(com.vividsolutions.jts.geom.Coordinate) ImageSegment(org.codice.imaging.nitf.core.image.ImageSegment) StringUtils(org.apache.commons.lang.StringUtils) Logger(org.slf4j.Logger) SegmentHandler(org.codice.alliance.transformer.nitf.common.SegmentHandler) Date(java.util.Date) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) LoggerFactory(org.slf4j.LoggerFactory) PrecisionModel(com.vividsolutions.jts.geom.PrecisionModel) NitfAttributeConverters(org.codice.alliance.transformer.nitf.NitfAttributeConverters) ArrayList(java.util.ArrayList) List(java.util.List) ImageCoordinates(org.codice.imaging.nitf.core.image.ImageCoordinates) NitfSegmentsFlow(org.codice.imaging.nitf.fluent.NitfSegmentsFlow) Metacard(ddf.catalog.data.Metacard) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) DataType(ddf.catalog.data.types.constants.core.DataType) Isr(org.codice.alliance.catalog.core.api.types.Isr) ImageCoordinatesRepresentation(org.codice.imaging.nitf.core.image.ImageCoordinatesRepresentation) Core(ddf.catalog.data.types.Core) Polygon(com.vividsolutions.jts.geom.Polygon) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Date(java.util.Date)

Aggregations

Polygon (com.vividsolutions.jts.geom.Polygon)114 LinearRing (com.vividsolutions.jts.geom.LinearRing)50 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)34 Test (org.junit.Test)32 Coordinate (com.vividsolutions.jts.geom.Coordinate)30 Point (com.vividsolutions.jts.geom.Point)29 PackedCoordinateSequence (com.vividsolutions.jts.geom.impl.PackedCoordinateSequence)27 ArrayList (java.util.ArrayList)26 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)24 Resource (org.openrdf.model.Resource)24 Statement (org.openrdf.model.Statement)24 URI (org.openrdf.model.URI)24 Value (org.openrdf.model.Value)24 ValueFactory (org.openrdf.model.ValueFactory)24 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)24 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)24 Geometry (com.vividsolutions.jts.geom.Geometry)22 LineString (com.vividsolutions.jts.geom.LineString)21 List (java.util.List)20 PersistenceManager (javax.jdo.PersistenceManager)15