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);
}
}
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;
}
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.");
}
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();
}
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));
}
}
Aggregations