Search in sources :

Example 6 with Relation

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

the class IndexVectorMapCreator method createMultipolygonBuilder.

private MultipolygonBuilder createMultipolygonBuilder(Entity e, OsmDbAccessorContext ctx) throws SQLException {
    ctx.loadEntityRelation((Relation) e);
    // create a multipolygon object for this
    MultipolygonBuilder original = new MultipolygonBuilder();
    original.setId(e.getId());
    // fill the multipolygon with all ways from the Relation
    for (RelationMember es : ((Relation) e).getMembers()) {
        if (es.getEntity() instanceof Way) {
            // $NON-NLS-1$
            boolean inner = "inner".equals(es.getRole());
            if (inner) {
                original.addInnerWay((Way) es.getEntity());
            } else if ("outer".equals(es.getRole())) {
                original.addOuterWay((Way) es.getEntity());
            }
        }
    }
    return original;
}
Also used : Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) Way(net.osmand.osm.edit.Way) MultipolygonBuilder(net.osmand.data.MultipolygonBuilder)

Example 7 with Relation

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

the class BasemapProcessor method processEntity.

public void processEntity(Entity e) {
    if (e instanceof Way) {
        if ("reverse_coastline".equals(((Way) e).getModifiableTags().get("natural"))) {
            ((Way) e).putTag("natural", "coastline");
            Collections.reverse(((Way) e).getNodes());
            ((Way) e).getNodeIds().reverse();
        }
    }
    long refId = -Math.abs(e.getId());
    for (int level = 0; level < mapZooms.getLevels().size(); level++) {
        boolean mostDetailed = level == 0;
        MapZoomPair zoomPair = mapZooms.getLevel(level);
        int zoomToEncode = mostDetailed ? Math.max(MOST_DETAILED_APPROXIMATION, zoomPair.getMinZoom() + 1) : zoomPair.getMaxZoom();
        if (mostDetailed && zoomPair.getMaxZoom() < 10) {
            throw new IllegalStateException("Zoom pair is not detailed " + zoomPair);
        }
        renderingTypes.encodeEntityWithType(e, zoomToEncode, typeUse, addtypeUse, namesUse, tempNameUse);
        if (typeUse.isEmpty()) {
            continue;
        }
        if (e instanceof Relation) {
            Relation r = (Relation) e;
            Iterator<RelationMember> it = r.getMembers().iterator();
            List<Node> outer = null;
            List<List<Node>> inner = new ArrayList<List<Node>>();
            while (it.hasNext()) {
                RelationMember n = it.next();
                if (n.getRole().equals("outer")) {
                    if (outer != null) {
                        throw new IllegalStateException("2 outer lines for relation = " + e.getId());
                    }
                    outer = ((Way) n.getEntity()).getNodes();
                } else if (n.getRole().equals("inner")) {
                    inner.add(((Way) n.getEntity()).getNodes());
                }
            }
            if (OsmMapUtils.polygonAreaPixels(outer, zoomToEncode) < PIXELS_THRESHOLD_AREA) {
                continue;
            }
            addObject(refId, level, zoomPair, zoomToEncode, outer, inner);
        } else if (e instanceof Way) {
            if (((Way) e).getNodes().size() < 2) {
                continue;
            }
            double dist = OsmMapUtils.getDistance(((Way) e).getFirstNode(), ((Way) e).getLastNode());
            boolean polygon = dist < 100;
            if ("coastline".equals(e.getTag("natural"))) {
                if (polygon && !mostDetailed) {
                    if (OsmMapUtils.polygonAreaPixels(((Way) e).getNodes(), zoomToEncode) < PIXELS_THRESHOLD_AREA) {
                        continue;
                    }
                }
                splitContinuousWay(((Way) e).getNodes(), typeUse.toArray(), !addtypeUse.isEmpty() ? addtypeUse.toArray() : null, zoomPair, zoomToEncode, quadTrees[level], refId);
            } else {
                List<Node> ns = ((Way) e).getNodes();
                if (!polygon) {
                    QuadRect qr = ((Way) e).getLatLonBBox();
                    if (qr == null) {
                        continue;
                    }
                    double mult = 1 / MapUtils.getPowZoom(Math.max(31 - (zoomToEncode + 8), 0));
                    int rx = MapUtils.get31TileNumberX(qr.right);
                    int lx = MapUtils.get31TileNumberX(qr.left);
                    int by = MapUtils.get31TileNumberY(qr.bottom);
                    int ty = MapUtils.get31TileNumberY(qr.top);
                    if (mult * (rx - lx) < PIXELS_THRESHOLD_AREA && mult * (by - ty) < PIXELS_THRESHOLD_AREA) {
                        continue;
                    }
                } else {
                    if (OsmMapUtils.polygonAreaPixels(ns, zoomToEncode) < PIXELS_THRESHOLD_AREA) {
                        continue;
                    }
                }
                addObject(refId, level, zoomPair, zoomToEncode, ns, null);
            }
        } else {
            int z = getViewZoom(zoomPair.getMinZoom(), zoomToEncode);
            int tilex = (int) MapUtils.getTileNumberX(z, ((Node) e).getLongitude());
            int tiley = (int) MapUtils.getTileNumberY(z, ((Node) e).getLatitude());
            addRawData(Collections.singletonList((Node) e), null, typeUse.toArray(), !addtypeUse.isEmpty() ? addtypeUse.toArray() : null, zoomPair, quadTrees[level], z, tilex, tiley, namesUse.isEmpty() ? null : new LinkedHashMap<MapRulType, String>(namesUse), refId);
        }
    }
}
Also used : Node(net.osmand.osm.edit.Node) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) QuadRect(net.osmand.data.QuadRect) Way(net.osmand.osm.edit.Way) MapRulType(net.osmand.osm.MapRenderingTypes.MapRulType) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 8 with Relation

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

the class OsmBaseStoragePbf method parseOSMPbf.

public synchronized void parseOSMPbf(final InputStream stream, final IProgress progress, final boolean entityInfo) throws IOException {
    BinaryParser parser = new BinaryParser() {

        public void updateProgress(int count) {
            progressEntity += count;
            if (progress != null && progressEntity > moduleProgress && !progress.isIndeterminate()) {
                try {
                    progressEntity = 0;
                    progress.remaining(stream.available());
                } catch (IOException e) {
                    progress.startWork(-1);
                }
            }
        }

        public void registerEntity(EntityType type, Entity e, EntityInfo info) {
            EntityId entityId = new EntityId(type, e.getId());
            if (acceptEntityToLoad(entityId, e)) {
                Entity oldEntity = entities.put(entityId, e);
                if (info != null) {
                    OsmBaseStoragePbf.this.entityInfo.put(entityId, info);
                }
                if (!supressWarnings && oldEntity != null) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    throw new UnsupportedOperationException("Entity with id=" + oldEntity.getId() + " is duplicated in osm map");
                }
            }
        }

        @Override
        protected void parse(HeaderBlock header) {
        }

        // $NON-NLS-1$
        private DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

        @Override
        protected void parseDense(DenseNodes n) {
            EntityInfo info = null;
            long changeset = 0;
            long timestamp = 0;
            int uid = 0;
            int user = 0;
            long id = 0;
            long lat = 0;
            long lon = 0;
            int keyInd = 0;
            boolean tagsEmpty = n.getKeysValsCount() == 0;
            for (int i = 0; i < n.getIdCount(); i++) {
                id += n.getId(i);
                lat += n.getLat(i);
                lon += n.getLon(i);
                Node node = new Node(parseLat(lat), parseLon(lon), id);
                if (entityInfo && n.getDenseinfo() != null) {
                    info = new EntityInfo();
                    changeset += n.getDenseinfo().getChangeset(i);
                    timestamp += n.getDenseinfo().getTimestamp(i);
                    uid += n.getDenseinfo().getUid(i);
                    user += n.getDenseinfo().getUserSid(i);
                    // $NON-NLS-1$
                    info.setChangeset(String.valueOf(changeset));
                    info.setTimestamp(format.format(new Date(date_granularity * (timestamp))));
                    info.setUser(getStringById(user));
                    // $NON-NLS-1$
                    info.setUid(String.valueOf(uid));
                    // $NON-NLS-1$
                    info.setVersion(String.valueOf(n.getDenseinfo().getVersion(i)));
                    // $NON-NLS-1$
                    info.setVisible("true");
                }
                if (!tagsEmpty) {
                    while (n.getKeysVals(keyInd) != 0) {
                        String key = getStringById(n.getKeysVals(keyInd));
                        String val = getStringById(n.getKeysVals(keyInd + 1));
                        node.putTag(key, val);
                        keyInd += 2;
                    }
                    keyInd++;
                }
                registerEntity(EntityType.NODE, node, info);
            }
            updateProgress(n.getIdCount());
        }

        protected EntityInfo parseEntityInfo(Info i) {
            EntityInfo info = new EntityInfo();
            // $NON-NLS-1$
            info.setChangeset(String.valueOf(i.getChangeset()));
            info.setTimestamp(format.format(getDate(i)));
            info.setUser(getStringById(i.getUserSid()));
            // $NON-NLS-1$
            info.setUid(String.valueOf(i.getUid()));
            // $NON-NLS-1$
            info.setVersion(String.valueOf(i.getVersion()));
            // $NON-NLS-1$
            info.setVisible("true");
            return info;
        }

        @Override
        protected void parseNodes(List<crosby.binary.Osmformat.Node> n) {
            EntityInfo info = null;
            int nsize = n.size();
            for (int i = 0; i < nsize; i++) {
                crosby.binary.Osmformat.Node nod = n.get(i);
                Node e = new Node(parseLat(nod.getLat()), parseLon(nod.getLon()), nod.getId());
                for (int j = 0; j < nod.getKeysCount(); j++) {
                    String key = getStringById(nod.getKeys(j));
                    String val = getStringById(nod.getVals(j));
                    e.putTag(key, val);
                }
                if (entityInfo) {
                    info = parseEntityInfo(nod.getInfo());
                }
                registerEntity(EntityType.NODE, e, info);
            }
            updateProgress(nsize);
        }

        @Override
        protected void parseRelations(List<crosby.binary.Osmformat.Relation> r) {
            EntityInfo info = null;
            int rsize = r.size();
            for (int i = 0; i < rsize; i++) {
                crosby.binary.Osmformat.Relation rel = r.get(i);
                Relation e = new Relation(rel.getId());
                long id = 0;
                for (int j = 0; j < rel.getMemidsCount(); j++) {
                    id += rel.getMemids(j);
                    String role = getStringById(rel.getRolesSid(j));
                    MemberType t = rel.getTypes(j);
                    EntityType ts = EntityType.NODE;
                    switch(t) {
                        case NODE:
                            ts = EntityType.NODE;
                            break;
                        case WAY:
                            ts = EntityType.WAY;
                            break;
                        case RELATION:
                            ts = EntityType.RELATION;
                            break;
                    }
                    e.addMember(id, ts, role);
                }
                for (int j = 0; j < rel.getKeysCount(); j++) {
                    String key = getStringById(rel.getKeys(j));
                    String val = getStringById(rel.getVals(j));
                    e.putTag(key, val);
                }
                if (entityInfo) {
                    info = parseEntityInfo(rel.getInfo());
                }
                registerEntity(EntityType.RELATION, e, info);
            }
            updateProgress(rsize);
        }

        @Override
        protected void parseWays(List<crosby.binary.Osmformat.Way> w) {
            EntityInfo info = null;
            int wsize = w.size();
            for (int i = 0; i < wsize; i++) {
                crosby.binary.Osmformat.Way way = w.get(i);
                Way e = new Way(way.getId());
                long id = 0;
                for (int j = 0; j < way.getRefsCount(); j++) {
                    id += way.getRefs(j);
                    e.addNode(id);
                }
                for (int j = 0; j < way.getKeysCount(); j++) {
                    String key = getStringById(way.getKeys(j));
                    String val = getStringById(way.getVals(j));
                    e.putTag(key, val);
                }
                if (entityInfo) {
                    info = parseEntityInfo(way.getInfo());
                }
                registerEntity(EntityType.WAY, e, info);
            }
            updateProgress(wsize);
        }

        @Override
        public void complete() {
        }
    };
    this.progressEntity = 0;
    this.entities.clear();
    this.entityInfo.clear();
    if (progress != null) {
        progress.startWork(stream.available());
    }
    BlockInputStream bis = new BlockInputStream(stream, parser);
    bis.process();
    if (progress != null) {
        progress.finishTask();
    }
    completeReading();
}
Also used : Entity(net.osmand.osm.edit.Entity) HeaderBlock(crosby.binary.Osmformat.HeaderBlock) Node(net.osmand.osm.edit.Node) Way(net.osmand.osm.edit.Way) Relation(net.osmand.osm.edit.Relation) BlockInputStream(crosby.binary.file.BlockInputStream) List(java.util.List) BinaryParser(crosby.binary.BinaryParser) IOException(java.io.IOException) EntityInfo(net.osmand.osm.edit.EntityInfo) Info(crosby.binary.Osmformat.Info) Date(java.util.Date) EntityType(net.osmand.osm.edit.Entity.EntityType) EntityId(net.osmand.osm.edit.Entity.EntityId) MemberType(crosby.binary.Osmformat.Relation.MemberType) EntityInfo(net.osmand.osm.edit.EntityInfo) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DenseNodes(crosby.binary.Osmformat.DenseNodes) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with Relation

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

the class OsmStorageWriter method writeOSM.

public void writeOSM(OutputStream output, Map<EntityId, EntityInfo> entityInfo, Collection<Node> nodes, Collection<Way> ways, Collection<Relation> relations, boolean skipMissingMembers) throws FactoryConfigurationError, XMLStreamException {
    // transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    // String indent = "{http://xml.apache.org/xslt}indent-amount";
    // transformer.setOutputProperty(indent, "4");
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter streamWriter = xof.createXMLStreamWriter(new OutputStreamWriter(output));
    streamWriter.writeStartDocument();
    Set<EntityId> nd = new HashSet<Entity.EntityId>();
    writeStartElement(streamWriter, ELEM_OSM, "");
    streamWriter.writeAttribute(ATTR_VERSION, "0.6");
    for (Node n : nodes) {
        writeStartElement(streamWriter, ELEM_NODE, INDENT);
        streamWriter.writeAttribute(ATTR_LAT, String.valueOf(n.getLatitude()));
        streamWriter.writeAttribute(ATTR_LON, String.valueOf(n.getLongitude()));
        streamWriter.writeAttribute(ATTR_ID, String.valueOf(n.getId()));
        writeEntityAttributes(streamWriter, n, entityInfo.get(EntityId.valueOf(n)));
        writeTags(streamWriter, n);
        writeEndElement(streamWriter, INDENT);
        if (skipMissingMembers) {
            nd.add(EntityId.valueOf(n));
        }
    }
    for (Way w : ways) {
        writeStartElement(streamWriter, ELEM_WAY, INDENT);
        streamWriter.writeAttribute(ATTR_ID, String.valueOf(w.getId()));
        writeEntityAttributes(streamWriter, w, entityInfo.get(EntityId.valueOf(w)));
        TLongArrayList ids = w.getNodeIds();
        for (int i = 0; i < ids.size(); i++) {
            writeStartElement(streamWriter, ELEM_ND, INDENT2);
            streamWriter.writeAttribute(ATTR_REF, String.valueOf(ids.get(i)));
            writeEndElement(streamWriter, INDENT2);
        }
        writeTags(streamWriter, w);
        writeEndElement(streamWriter, INDENT);
        if (skipMissingMembers) {
            nd.add(EntityId.valueOf(w));
        }
    }
    for (Relation r : relations) {
        if (skipMissingMembers) {
            nd.add(EntityId.valueOf(r));
        }
        writeStartElement(streamWriter, ELEM_RELATION, INDENT);
        streamWriter.writeAttribute(ATTR_ID, String.valueOf(r.getId()));
        writeEntityAttributes(streamWriter, r, entityInfo.get(EntityId.valueOf(r)));
        for (RelationMember e : r.getMembers()) {
            if (skipMissingMembers && !nd.contains(e.getEntityId())) {
                continue;
            }
            writeStartElement(streamWriter, ELEM_MEMBER, INDENT2);
            streamWriter.writeAttribute(ATTR_REF, String.valueOf(e.getEntityId().getId()));
            String s = e.getRole();
            if (s == null) {
                s = "";
            }
            streamWriter.writeAttribute(ATTR_ROLE, s);
            streamWriter.writeAttribute(ATTR_TYPE, e.getEntityId().getType().toString().toLowerCase());
            writeEndElement(streamWriter, INDENT2);
        }
        writeTags(streamWriter, r);
        writeEndElement(streamWriter, INDENT);
    }
    // osm
    writeEndElement(streamWriter, "");
    streamWriter.writeEndDocument();
    streamWriter.flush();
}
Also used : Entity(net.osmand.osm.edit.Entity) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) TLongArrayList(gnu.trove.list.array.TLongArrayList) Node(net.osmand.osm.edit.Node) Way(net.osmand.osm.edit.Way) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with Relation

use of net.osmand.osm.edit.Relation 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)

Aggregations

Relation (net.osmand.osm.edit.Relation)30 Way (net.osmand.osm.edit.Way)17 RelationMember (net.osmand.osm.edit.Relation.RelationMember)16 Entity (net.osmand.osm.edit.Entity)15 Node (net.osmand.osm.edit.Node)14 EntityId (net.osmand.osm.edit.Entity.EntityId)11 ArrayList (java.util.ArrayList)8 LinkedHashMap (java.util.LinkedHashMap)7 TLongArrayList (gnu.trove.list.array.TLongArrayList)4 IOException (java.io.IOException)4 LinkedHashSet (java.util.LinkedHashSet)4 EntityInfo (net.osmand.osm.edit.EntityInfo)4 File (java.io.File)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 LatLon (net.osmand.data.LatLon)3 EntityType (net.osmand.osm.edit.Entity.EntityType)3 FileOutputStream (java.io.FileOutputStream)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2