Search in sources :

Example 6 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class OsmDbAccessor method loadEntityRelation.

public void loadEntityRelation(Relation e, int level) throws SQLException {
    if (e.isDataLoaded()) {
        // data was already loaded, nothing to do
        return;
    }
    Map<EntityId, Entity> map = new LinkedHashMap<EntityId, Entity>();
    if (e.getMembers().isEmpty()) {
        pselectRelation.setLong(1, e.getId());
        pselectRelation.setInt(2, e.getModify() == Entity.MODIFY_DELETED ? 1 : 0);
        if (pselectRelation.execute()) {
            ResultSet rs = pselectRelation.getResultSet();
            while (rs.next()) {
                int ord = rs.getInt(4);
                if (ord == 0) {
                    readTags(e, rs.getBytes(5));
                }
                e.addMember(rs.getLong(1), EntityType.values()[rs.getInt(2)], rs.getString(3));
            }
            rs.close();
        }
    }
    Collection<RelationMember> ids = e.getMembers();
    if (level > 0) {
        for (RelationMember i : ids) {
            if (i.getEntityId().getType() == EntityType.NODE) {
                pselectNode.setLong(1, i.getEntityId().getId());
                if (pselectNode.execute()) {
                    ResultSet rs = pselectNode.getResultSet();
                    Node n = null;
                    while (rs.next()) {
                        if (n == null) {
                            n = new Node(rs.getDouble(1), rs.getDouble(2), i.getEntityId().getId());
                            readTags(n, rs.getBytes(3));
                        }
                    }
                    map.put(i.getEntityId(), n);
                    rs.close();
                }
            } else if (i.getEntityId().getType() == EntityType.WAY) {
                Way way = new Way(i.getEntityId().getId());
                loadEntityWay(way);
                map.put(i.getEntityId(), way);
            } else if (i.getEntityId().getType() == EntityType.RELATION) {
                Relation rel = new Relation(i.getEntityId().getId());
                loadEntityRelation(rel, level - 1);
                map.put(i.getEntityId(), rel);
            }
        }
        e.initializeLinks(map);
        e.entityDataLoaded();
    }
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) Entity(net.osmand.osm.edit.Entity) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) Node(net.osmand.osm.edit.Node) ResultSet(java.sql.ResultSet) Way(net.osmand.osm.edit.Way) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class OsmDbCreator method acceptEntityToLoad.

@Override
public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity e) {
    // put all nodes into temporary db to get only required nodes after loading all data
    if (VALIDATE_DUPLICATES) {
        long l = (e.getId() << 2) + entityId.getType().ordinal();
        if (!idSet.add(l)) {
            throw new IllegalStateException("Duplicate id '" + e.getId() + "' " + entityId.getType());
        }
    }
    try {
        e.removeTags(tagsToIgnore);
        ByteArrayOutputStream tags = new ByteArrayOutputStream();
        try {
            for (Entry<String, String> i : e.getTags().entrySet()) {
                // UTF-8 default
                tags.write(i.getKey().getBytes("UTF-8"));
                tags.write(0);
                tags.write(i.getValue().getBytes("UTF-8"));
                tags.write(0);
            }
        } catch (IOException es) {
            throw new RuntimeException(es);
        }
        long id = convertId(e);
        boolean delete = AugmentedDiffsInspector.OSMAND_DELETE_VALUE.equals(e.getTag(AugmentedDiffsInspector.OSMAND_DELETE_TAG));
        if (e.getTags().isEmpty()) {
            e.putTag(AugmentedDiffsInspector.OSMAND_DELETE_TAG, AugmentedDiffsInspector.OSMAND_DELETE_VALUE);
            delete = true;
        }
        if (ovewriteIds || e instanceof Relation) {
            checkEntityExists(e, id, delete);
        }
        if (e instanceof Node) {
            currentCountNode++;
            if (!e.getTags().isEmpty()) {
                allNodes++;
            }
            prepNode.setLong(1, id);
            prepNode.setDouble(2, ((Node) e).getLatitude());
            prepNode.setDouble(3, ((Node) e).getLongitude());
            prepNode.setBytes(4, tags.toByteArray());
            prepNode.addBatch();
            if (currentCountNode >= BATCH_SIZE_OSM) {
                prepNode.executeBatch();
                // clear memory
                dbConn.commit();
                currentCountNode = 0;
            }
        } else if (e instanceof Way) {
            allWays++;
            int ord = 0;
            TLongArrayList nodeIds = ((Way) e).getNodeIds();
            boolean city = CityType.valueFromString(((Way) e).getTag(OSMTagKey.PLACE)) != null;
            int boundary = ((Way) e).getTag(OSMTagKey.BOUNDARY) != null || city ? 1 : 0;
            for (int j = 0; j < nodeIds.size(); j++) {
                currentWaysCount++;
                if (ord == 0) {
                    prepWays.setBytes(4, tags.toByteArray());
                }
                prepWays.setLong(1, id);
                prepWays.setLong(2, nodeIds.get(j));
                prepWays.setLong(3, ord++);
                prepWays.setInt(5, boundary);
                prepWays.addBatch();
            }
            if (currentWaysCount >= BATCH_SIZE_OSM) {
                prepWays.executeBatch();
                // clear memory
                dbConn.commit();
                currentWaysCount = 0;
            }
        } else {
            // osm change can't handle relations properly
            allRelations++;
            short ord = 0;
            for (RelationMember i : ((Relation) e).getMembers()) {
                currentRelationsCount++;
                if (ord == 0) {
                    prepRelations.setBytes(6, tags.toByteArray());
                }
                prepRelations.setLong(1, id);
                prepRelations.setLong(2, i.getEntityId().getId());
                prepRelations.setLong(3, i.getEntityId().getType().ordinal());
                prepRelations.setString(4, i.getRole());
                prepRelations.setLong(5, ord++);
                prepRelations.setInt(7, delete ? 1 : 0);
                prepRelations.addBatch();
            }
            // System.out.println(id + " " + delete);
            if (currentRelationsCount >= BATCH_SIZE_OSM) {
                prepRelations.executeBatch();
                // clear memory
                dbConn.commit();
                currentRelationsCount = 0;
            }
        }
    } catch (SQLException ex) {
        // $NON-NLS-1$
        log.error("Could not save in db (entity " + entityId + ") ", ex);
    }
    // do not add to storage
    return false;
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) SQLException(java.sql.SQLException) Node(net.osmand.osm.edit.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Way(net.osmand.osm.edit.Way) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember)

Example 8 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class TagsTransformer method handleRelationPropogatedTags.

public void handleRelationPropogatedTags(Relation e, MapRenderingTypesEncoder renderingTypes, OsmDbAccessorContext ctx, EntityConvertApplyType at) throws SQLException {
    Map<MapRulType, Map<MapRulType, String>> propogated = renderingTypes.getRelationPropogatedTags((Relation) e, at);
    if (propogated != null && propogated.size() > 0) {
        if (ctx != null) {
            ctx.loadEntityRelation((Relation) e);
        }
        for (RelationMember ids : ((Relation) e).getMembers()) {
            if (!propogatedTags.containsKey(ids.getEntityId())) {
                propogatedTags.put(ids.getEntityId(), new LinkedHashMap<String, String>());
            }
            Map<String, String> map = propogatedTags.get(ids.getEntityId());
            Iterator<Entry<MapRulType, Map<MapRulType, String>>> itMain = propogated.entrySet().iterator();
            while (itMain.hasNext()) {
                Entry<MapRulType, Map<MapRulType, String>> ev = itMain.next();
                Map<MapRulType, String> pr = ev.getValue();
                MapRulType propagateRule = ev.getKey();
                if (propagateRule.isRelationGroup()) {
                    Iterator<Entry<MapRulType, String>> it = pr.entrySet().iterator();
                    int modifier = 1;
                    String s = propagateRule.getTag() + "__" + propagateRule.getValue() + "_";
                    while (map.containsKey(s + modifier)) {
                        modifier++;
                    }
                    map.put(s + modifier, s);
                    while (it.hasNext()) {
                        Entry<MapRulType, String> es = it.next();
                        String key = es.getKey().getTag();
                        map.put(key + "_" + modifier, es.getValue());
                    }
                } else {
                    Iterator<Entry<MapRulType, String>> it = pr.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<MapRulType, String> es = it.next();
                        String key = es.getKey().getTag();
                        if (es.getKey().isText() && map.containsKey(key)) {
                            String res = sortAndAttachUniqueValue(map.get(key), es.getValue());
                            map.put(key, res);
                        } else {
                            map.put(key, es.getValue());
                        }
                    }
                }
            }
        }
    }
}
Also used : MapRulType(net.osmand.osm.MapRenderingTypes.MapRulType) Relation(net.osmand.osm.edit.Relation) Entry(java.util.Map.Entry) RelationMember(net.osmand.osm.edit.Relation.RelationMember) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class IndexAddressCreator method extractBoundary.

private Boundary extractBoundary(Entity e, OsmDbAccessorContext ctx) throws SQLException {
    if (e instanceof Node) {
        return null;
    }
    long centerId = 0;
    CityType ct = CityType.valueFromString(e.getTag(OSMTagKey.PLACE));
    // if a place that has addr_place is a neighbourhood mark it as a suburb (made for the suburbs of Venice)
    boolean isNeighbourhood = e.getTag(OSMTagKey.ADDR_PLACE) != null && "neighbourhood".equals(e.getTag(OSMTagKey.PLACE));
    if ((ct == null && "townland".equals(e.getTag(OSMTagKey.LOCALITY))) || isNeighbourhood) {
        if (e instanceof Relation) {
            ctx.loadEntityRelation((Relation) e);
        }
        final City city = createMissingCity(e, CityType.SUBURB);
        if (city != null) {
            centerId = city.getId();
            ct = CityType.SUBURB;
        }
    }
    boolean administrative = "administrative".equals(e.getTag(OSMTagKey.BOUNDARY));
    boolean postalCode = "postal_code".equals(e.getTag(OSMTagKey.BOUNDARY));
    if (administrative || postalCode || ct != null) {
        if (e instanceof Way && visitedBoundaryWays.contains(e.getId())) {
            return null;
        }
        String bname = e.getTag(OSMTagKey.NAME);
        MultipolygonBuilder m = new MultipolygonBuilder();
        if (e instanceof Relation) {
            Relation aRelation = (Relation) e;
            ctx.loadEntityRelation(aRelation);
            for (RelationMember es : aRelation.getMembers()) {
                if (es.getEntity() instanceof Way) {
                    // $NON-NLS-1$
                    boolean inner = "inner".equals(es.getRole());
                    if (inner) {
                        m.addInnerWay((Way) es.getEntity());
                    } else {
                        String wName = es.getEntity().getTag(OSMTagKey.NAME);
                        // if name are not equal keep the way for further check (it could be different suburb)
                        if (Algorithms.objectEquals(wName, bname) || wName == null) {
                            visitedBoundaryWays.add(es.getEntity().getId());
                        }
                        m.addOuterWay((Way) es.getEntity());
                    }
                } else if (es.getEntity() instanceof Node && ("admin_centre".equals(es.getRole()) || "admin_center".equals(es.getRole()))) {
                    centerId = es.getEntity().getId();
                } else if (es.getEntity() instanceof Node && ("label".equals(es.getRole()) && centerId == 0)) {
                    centerId = es.getEntity().getId();
                }
            }
        } else if (e instanceof Way) {
            m.addOuterWay((Way) e);
        }
        Boundary boundary = new Boundary(m);
        boundary.setName(bname);
        // Goteborg
        boundary.setAltName(e.getTag("short_name"));
        boundary.setAdminLevel(extractBoundaryAdminLevel(e));
        boundary.setBoundaryId(e.getId());
        boundary.setCityType(ct);
        if (centerId != 0) {
            boundary.setAdminCenterId(centerId);
        }
        return boundary;
    } else {
        return null;
    }
}
Also used : Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) CityType(net.osmand.data.City.CityType) Node(net.osmand.osm.edit.Node) City(net.osmand.data.City) Way(net.osmand.osm.edit.Way) MultipolygonBuilder(net.osmand.data.MultipolygonBuilder) Boundary(net.osmand.data.Boundary)

Example 10 with RelationMember

use of net.osmand.osm.edit.Relation.RelationMember in project OsmAnd-tools by osmandapp.

the class MultipolygonFileTest method buildPolygon.

private Multipolygon buildPolygon(OsmBaseStorage st, long id) {
    Relation r = (Relation) st.getRegisteredEntities().get(new EntityId(EntityType.RELATION, id));
    Iterator<RelationMember> it = r.getMembers().iterator();
    MultipolygonBuilder bld = new MultipolygonBuilder();
    while (it.hasNext()) {
        RelationMember e = it.next();
        if (e.getRole().equals("outer")) {
            bld.addOuterWay((Way) e.getEntity());
        } else if (e.getRole().equals("inner")) {
            bld.addInnerWay((Way) e.getEntity());
        }
    }
    Multipolygon polygon = bld.build();
    return polygon;
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) Way(net.osmand.osm.edit.Way)

Aggregations

Relation (net.osmand.osm.edit.Relation)16 RelationMember (net.osmand.osm.edit.Relation.RelationMember)16 Way (net.osmand.osm.edit.Way)11 Node (net.osmand.osm.edit.Node)9 Entity (net.osmand.osm.edit.Entity)7 EntityId (net.osmand.osm.edit.Entity.EntityId)7 LinkedHashMap (java.util.LinkedHashMap)5 TLongArrayList (gnu.trove.list.array.TLongArrayList)3 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 MultipolygonBuilder (net.osmand.data.MultipolygonBuilder)2 TransportStop (net.osmand.data.TransportStop)2 MapRulType (net.osmand.osm.MapRenderingTypes.MapRulType)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1