use of org.openstreetmap.osmosis.core.container.v0_6.RelationContainer in project OSMWrangle by SLIPO-EU.
the class OsmPbfParser method process.
/**
* Processes an OSM element (node, way, or relation).
* @param entityContainer Container of an OSM node, way, or relation.
*/
@Override
public void process(EntityContainer entityContainer) {
// Will become true only if a tag related to user-specified filters is found for the current OSM element
keepIndexed = false;
if ((!scanWays) && (!scanRelations) && (entityContainer instanceof NodeContainer)) {
// Create a new OSM node object and populate it with the appropriate values
// Mark position of the parser
inNode = true;
inWay = false;
inRelation = false;
numNodes++;
Node myNode = ((NodeContainer) entityContainer).getEntity();
nodeTmp = new OSMNode();
nodeTmp.setID("" + myNode.getId());
// Collect tags associated with this OSM element
for (Tag myTag : myNode.getTags()) {
nodeTmp.setTagKeyValue(myTag.getKey(), myChecker.removeIllegalChars(myTag.getValue()));
if ((tags == null) || (tags.contains(myTag.getKey()))) {
// CAUTION! Filter out any OSM elements not related to tags specified by the user
// In case of no tags specified for filtering, index all nodes
keepIndexed = true;
}
}
// Create geometry object with original WGS84 coordinates
Geometry geom = geometryFactory.createPoint(new Coordinate(myNode.getLongitude(), myNode.getLatitude()));
nodeTmp.setGeometry(geom);
// Convert entity
if (keepIndexed) {
if (keepUnnamed)
myConverter.parse(recBuilder.createOSMRecord(nodeTmp), classification, reproject, targetSRID);
else if (nodeTmp.getTagKeyValue().containsKey("name")) {
// CAUTION! Only named entities will be transformed
myConverter.parse(recBuilder.createOSMRecord(nodeTmp), classification, reproject, targetSRID);
numNamedEntities++;
}
}
if (recBuilder.nodeIndex.containsKey(nodeTmp.getID()))
// Keep a dictionary of node geometries, only if referenced by OSM ways
recBuilder.nodeIndex.put(nodeTmp.getID(), nodeTmp.getGeometry());
nodeTmp = null;
} else if ((!scanRelations) && (entityContainer instanceof WayContainer)) {
// Create a new OSM way object and populate it with the appropriate values
Way myWay = ((WayContainer) entityContainer).getEntity();
for (Tag myTag : myWay.getTags()) {
if ((tags == null) || (tags.contains(myTag.getKey()))) {
// CAUTION! Filter out any OSM elements not related to tags specified by the user
// In case of no tags specified for filtering, index all ways
keepIndexed = true;
break;
}
}
if (scanWays) {
// Either this OSM way is filtered or referenced by a relation, so its nodes should be kept in the index
if ((keepIndexed) || (recBuilder.wayIndex.containsKey("" + myWay.getId()))) {
for (WayNode entry : myWay.getWayNodes()) {
// ...initially with NULL geometry, to be replaced once nodes will be parsed
recBuilder.nodeIndex.put("" + entry.getNodeId(), null);
}
}
} else {
if (inNode)
System.out.println("\nFinished parsing OSM nodes.");
// Mark position of the parser
inNode = false;
inWay = true;
inRelation = false;
numWays++;
// Skip parsing if this way is filtered out or not referenced by other relations
if ((!keepIndexed) && (!recBuilder.wayIndex.containsKey("" + myWay.getId())))
return;
wayTmp = new OSMWay();
wayTmp.setID("" + myWay.getId());
// Collect tags associated with this OSM element
for (Tag myTag : myWay.getTags()) {
wayTmp.setTagKeyValue(myTag.getKey(), myChecker.removeIllegalChars(myTag.getValue()));
}
for (WayNode entry : myWay.getWayNodes()) {
if (recBuilder.nodeIndex.containsKey("" + entry.getNodeId())) {
// get the geometry of the node with ID=entry
Geometry geometry = recBuilder.nodeIndex.get("" + entry.getNodeId());
// add the node geometry in this way
wayTmp.addNodeGeometry(geometry);
} else
System.out.println("Missing node " + entry.getNodeId() + " in referencing way " + wayTmp.getID());
}
Geometry geom = geometryFactory.buildGeometry(wayTmp.getNodeGeometries());
// These nodes must be more than 3, because JTS does not allow construction of a linear ring with less than 3 points
if ((closedRings2Polygons) && (wayTmp.getNodeGeometries().size() > 3) && wayTmp.getNodeGeometries().get(0).equals(wayTmp.getNodeGeometries().get(wayTmp.getNodeGeometries().size() - 1))) {
// Always construct a polygon when a linear ring is detected
LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates());
Polygon poly = new Polygon(linear, null, geometryFactory);
wayTmp.setGeometry(poly);
/**
***********************************************
* //OPTION NOT USED: Construct a linear ring geometry when this feature is either a barrier or a road
* if (!((wayTmp.getTagKeyValue().containsKey("barrier")) || wayTmp.getTagKeyValue().containsKey("highway"))){
* //this is not a barrier nor a road, so construct a polygon geometry
*
* LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates());
* Polygon poly = new Polygon(linear, null, geometryFactory);
* wayTmp.setGeometry(poly);
* }
* else { //it is either a barrier or a road, so construct a linear ring geometry
* LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates());
* wayTmp.setGeometry(linear);
* }
*************************************************
*/
} else if (wayTmp.getNodeGeometries().size() > 1) {
// it is an open geometry with more than one nodes, make it linestring
LineString lineString = geometryFactory.createLineString(geom.getCoordinates());
wayTmp.setGeometry(lineString);
} else {
// we assume that any other geometries are points
// some ways happen to have only one point. Construct a Point.
Point point = geometryFactory.createPoint(geom.getCoordinate());
wayTmp.setGeometry(point);
}
// Convert this entity
if (keepIndexed) {
if (keepUnnamed)
myConverter.parse(recBuilder.createOSMRecord(wayTmp), classification, reproject, targetSRID);
else if (wayTmp.getTagKeyValue().containsKey("name")) {
// CAUTION! Only named entities will be transformed
myConverter.parse(recBuilder.createOSMRecord(wayTmp), classification, reproject, targetSRID);
numNamedEntities++;
}
}
if (recBuilder.wayIndex.containsKey(wayTmp.getID()))
// Keep a dictionary of way geometries, only for those referenced by OSM relations
recBuilder.wayIndex.put(wayTmp.getID(), wayTmp.getGeometry());
wayTmp = null;
}
} else if ((!scanWays) && (entityContainer instanceof RelationContainer)) {
// Create a new OSM relation object and populate it with the appropriate values
Relation myRelation = ((RelationContainer) entityContainer).getEntity();
for (Tag myTag : myRelation.getTags()) {
if ((tags == null) || (tags.contains(myTag.getKey()))) {
// CAUTION! Filter out any OSM elements not related to tags specified by the user
// In case of no tags specified for filtering, index all nodes
keepIndexed = true;
break;
}
}
if (scanRelations) {
// Either only filtered relations will be indexed or those referenced by other relations
if ((keepIndexed) || (recBuilder.relationIndex.containsKey("" + myRelation.getId()))) {
for (RelationMember m : myRelation.getMembers()) {
if (m.getMemberType().name().equalsIgnoreCase("node"))
// This node is referenced by a relation; keep it in the index, and its geometry will be filled in when parsing the nodes
recBuilder.nodeIndex.put("" + m.getMemberId(), null);
else if (m.getMemberType().name().equalsIgnoreCase("way"))
// This way is referenced by a relation; keep it in the index, and its geometry will be filled in when parsing the ways
recBuilder.wayIndex.put("" + m.getMemberId(), null);
else if (m.getMemberType().name().equalsIgnoreCase("relation")) {
// This relation is referenced by another relation; keep it in the index, and its geometry will be filled in when parsing the relations
recBuilder.relationIndex.put("" + m.getMemberId(), null);
// Relations need to be scanned once more, as they reference other relations
rescanRelations = true;
}
}
}
} else {
if (inWay)
System.out.println("\nFinished parsing OSM ways.");
// Mark position of the parser
inNode = false;
inWay = false;
inRelation = true;
numRelations++;
// Skip parsing if this relation is filtered out or not referenced by others
if ((!keepIndexed) && (!recBuilder.relationIndex.containsKey("" + myRelation.getId())))
return;
relationTmp = new OSMRelation();
relationTmp.setID("" + myRelation.getId());
// Collect tags associated with this OSM element
for (Tag myTag : myRelation.getTags()) {
relationTmp.setTagKeyValue(myTag.getKey(), myChecker.removeIllegalChars(myTag.getValue()));
}
// System.out.println("Relation " + myRelation.getId() + " Number of members: " + myRelation.getMembers().size());
for (RelationMember m : myRelation.getMembers()) relationTmp.addMemberReference("" + m.getMemberId(), m.getMemberType().name(), m.getMemberRole());
OSMRecord rec = recBuilder.createOSMRecord(relationTmp);
if (// No records created for incomplete relations during the first pass
rec != null) {
// Convert entity
if (keepIndexed) {
if (keepUnnamed)
myConverter.parse(rec, classification, reproject, targetSRID);
else if (relationTmp.getTagKeyValue().containsKey("name")) {
// CAUTION! Only named entities will be transformed
myConverter.parse(rec, classification, reproject, targetSRID);
numNamedEntities++;
}
}
if (recBuilder.relationIndex.containsKey(relationTmp.getID()))
// Keep a dictionary of relation geometries, only for those referenced by other OSM relations
recBuilder.relationIndex.put(relationTmp.getID(), rec.getGeometry());
}
relationTmp = null;
}
}
}
use of org.openstreetmap.osmosis.core.container.v0_6.RelationContainer in project osmosis by openstreetmap.
the class AreaFilter method selectChildRelationsPass.
/**
* Select all relation members of type relation for existing selected relations. This may need
* to be called several times until all children are selected.
*
* @return True if additional selections were made an another pass is needed.
*/
private boolean selectChildRelationsPass() {
try (ReleasableIterator<RelationContainer> i = allRelations.iterate()) {
int selectionCount;
selectionCount = 0;
while (i.hasNext()) {
Relation relation = i.next().getEntity();
long relationId = relation.getId();
// Only examine available relations.
if (availableRelations.get(relationId)) {
// Select the child if it hasn't already been selected.
for (RelationMember member : relation.getMembers()) {
if (member.getMemberType().equals(EntityType.Relation)) {
long memberId = member.getMemberId();
if (!availableRelations.get(memberId)) {
availableRelations.set(memberId);
selectionCount++;
}
}
}
}
}
return selectionCount > 0;
}
}
use of org.openstreetmap.osmosis.core.container.v0_6.RelationContainer in project osmosis by openstreetmap.
the class AreaFilter method emitRelation.
/**
* Sends a relation to the sink. This will perform any necessary transformations on the way before
* sending it.
*
* @param relationContainer
* Relation to be sent.
*/
private void emitRelation(RelationContainer relationContainer) {
if (clipIncompleteEntities) {
RelationContainer filteredRelationContainer;
Relation filteredRelation;
filteredRelationContainer = relationContainer.getWriteableInstance();
filteredRelation = filteredRelationContainer.getEntity();
// Remove members for entities that are unavailable.
for (Iterator<RelationMember> i = filteredRelation.getMembers().iterator(); i.hasNext(); ) {
RelationMember member = i.next();
EntityType memberType;
long memberId;
memberType = member.getMemberType();
memberId = member.getMemberId();
switch(memberType) {
case Node:
if (!availableNodes.get(memberId)) {
i.remove();
}
break;
case Way:
if (!availableWays.get(memberId)) {
i.remove();
}
break;
case Relation:
if (!availableRelations.get(memberId)) {
i.remove();
}
break;
default:
break;
}
}
// Only add relations that contain entities.
if (filteredRelation.getMembers().size() > 0) {
sink.process(filteredRelationContainer);
}
} else {
sink.process(relationContainer);
}
}
use of org.openstreetmap.osmosis.core.container.v0_6.RelationContainer in project osmosis by openstreetmap.
the class UsedWayFilter method complete.
/**
* {@inheritDoc}
*/
public void complete() {
// send on all nodes
ReleasableIterator<NodeContainer> nodeIterator = allNodes.iterate();
while (nodeIterator.hasNext()) {
sink.process(nodeIterator.next());
}
nodeIterator.close();
nodeIterator = null;
// send on all required ways
ReleasableIterator<WayContainer> wayIterator = allWays.iterate();
while (wayIterator.hasNext()) {
WayContainer wayContainer = wayIterator.next();
long wayId = wayContainer.getEntity().getId();
if (!requiredWays.get(wayId)) {
continue;
}
sink.process(wayContainer);
}
wayIterator.close();
wayIterator = null;
// send on all relations
ReleasableIterator<RelationContainer> relationIterator = allRelations.iterate();
while (relationIterator.hasNext()) {
sink.process(relationIterator.next());
}
relationIterator.close();
relationIterator = null;
// done
sink.complete();
}
use of org.openstreetmap.osmosis.core.container.v0_6.RelationContainer in project osmosis by openstreetmap.
the class TagFilterTest method setUp.
/**
* Performs pre-test activities.
*/
@Before
public void setUp() {
OsmUser user;
List<Tag> tags;
user = new OsmUser(12, "OsmosisTest");
tags = Arrays.asList(new Tag("amenity", "bank"), new Tag("Akey", "Avalue"));
amenityNode = new Node(new CommonEntityData(1101, 0, new Date(), user, 0, tags), 1, 2);
amenityNodeContainer = new NodeContainer(amenityNode);
tags = new ArrayList<Tag>();
taglessNode = new Node(new CommonEntityData(1102, 0, new Date(), user, 0, tags), 3, 4);
taglessNodeContainer = new NodeContainer(taglessNode);
tags = Arrays.asList(new Tag("highway", "motorway"), new Tag("Bkey", "Bvalue"));
motorwayWay = new Way(new CommonEntityData(2201, 0, new Date(), user, 0, tags), new ArrayList<WayNode>());
motorwayWayContainer = new WayContainer(motorwayWay);
tags = Arrays.asList(new Tag("highway", "residential"), new Tag("Ckey", "Cvalue"));
residentialWay = new Way(new CommonEntityData(2202, 0, new Date(), user, 0, tags), new ArrayList<WayNode>());
residentialWayContainer = new WayContainer(residentialWay);
tags = Arrays.asList(new Tag("Dkey", "Dvalue"));
testRelation = new Relation(new CommonEntityData(3301, 0, new Date(), user, 0, tags), new ArrayList<RelationMember>());
testRelationContainer = new RelationContainer(testRelation);
}
Aggregations