Search in sources :

Example 1 with EntityId

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

the class IndexRouteCreator method indexRelations.

public void indexRelations(Entity e, OsmDbAccessorContext ctx) throws SQLException {
    indexHighwayRestrictions(e, ctx);
    if (e instanceof Relation) {
        tagsTransformer.handleRelationPropogatedTags((Relation) e, renderingTypes, ctx, EntityConvertApplyType.ROUTING);
        Map<String, String> tags = renderingTypes.transformTags(e.getTags(), EntityType.RELATION, EntityConvertApplyType.ROUTING);
        if ("enforcement".equals(tags.get("type")) && "maxspeed".equals(tags.get("enforcement"))) {
            ctx.loadEntityRelation((Relation) e);
            Iterator<RelationMember> from = ((Relation) e).getMembers("from").iterator();
            // mark as speed cameras
            while (from.hasNext()) {
                Entity n = from.next().getEntity();
                if (n instanceof Node) {
                    tagsTransformer.registerPropogatedTag(new EntityId(EntityType.NODE, n.getId()), "highway", "speed_camera");
                }
            }
        }
    }
}
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)

Example 2 with EntityId

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

the class FixLinkedCoastline method alignAndAddtoStorage.

private static void alignAndAddtoStorage(OsmBaseStorage storage, List<EntityId> toWrite, List<Way> result) {
    // align start/end node and add to strage
    for (int i = 0; i < result.size(); i++) {
        Node nextStart;
        if (i < result.size() - 1) {
            nextStart = result.get(i + 1).getNodes().get(0);
        } else {
            nextStart = result.get(0).getNodes().get(0);
        }
        Way w = result.get(i);
        int sz = w.getNodes().size();
        w.removeNodeByIndex(sz - 1);
        w.addNode(nextStart);
        if ("land_coastline".equals(w.getTag(OSMTagKey.NATURAL))) {
            w.putTag(OSMTagKey.NATURAL.getValue(), "coastline");
        }
        EntityId eId = EntityId.valueOf(w);
        storage.getRegisteredEntities().put(eId, w);
        toWrite.add(eId);
    }
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) Node(net.osmand.osm.edit.Node) Way(net.osmand.osm.edit.Way)

Example 3 with EntityId

use of net.osmand.osm.edit.Entity.EntityId 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 4 with EntityId

use of net.osmand.osm.edit.Entity.EntityId 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 5 with EntityId

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

the class FixAdminLevel0 method process.

private static void process(File read, File write) throws IOException, XMLStreamException, XmlPullParserException {
    OsmBaseStorage storage = new OsmBaseStorage();
    storage.parseOSM(new FileInputStream(read), new ConsoleProgressImplementation());
    Map<EntityId, Entity> entities = new HashMap<EntityId, Entity>(storage.getRegisteredEntities());
    long id = -1;
    for (EntityId e : entities.keySet()) {
        Entity es = storage.getRegisteredEntities().get(e);
        if (e.getId() < id) {
            id = e.getId() - 1;
        }
        if (e.getType() == EntityType.WAY) {
            processWay((Way) es);
        }
    }
    for (String country : countryNames.keySet()) {
        List<Way> list = countryNames.get(country);
        for (Way w : list) {
            LatLon latLon = OsmMapUtils.getMathWeightCenterForNodes(w.getNodes());
            // LatLon latLon = w.getLatLon();
            Node node = new Node(latLon.getLatitude(), latLon.getLongitude(), id--);
            node.putTag("name", country);
            node.putTag("place", "country");
            storage.getRegisteredEntities().put(EntityId.valueOf(node), node);
        }
    }
    OsmStorageWriter writer = new OsmStorageWriter();
    writer.saveStorage(new FileOutputStream(write), storage, null, true);
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Entity(net.osmand.osm.edit.Entity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(net.osmand.osm.edit.Node) ConsoleProgressImplementation(net.osmand.impl.ConsoleProgressImplementation) FileInputStream(java.io.FileInputStream) Way(net.osmand.osm.edit.Way) EntityId(net.osmand.osm.edit.Entity.EntityId) LatLon(net.osmand.data.LatLon) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) FileOutputStream(java.io.FileOutputStream)

Aggregations

EntityId (net.osmand.osm.edit.Entity.EntityId)25 Entity (net.osmand.osm.edit.Entity)15 Node (net.osmand.osm.edit.Node)15 Way (net.osmand.osm.edit.Way)15 Relation (net.osmand.osm.edit.Relation)11 LinkedHashMap (java.util.LinkedHashMap)10 HashMap (java.util.HashMap)8 RelationMember (net.osmand.osm.edit.Relation.RelationMember)7 OsmBaseStorage (net.osmand.osm.io.OsmBaseStorage)7 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 OsmStorageWriter (net.osmand.osm.io.OsmStorageWriter)5 IOException (java.io.IOException)4 Map (java.util.Map)4 EntityInfo (net.osmand.osm.edit.EntityInfo)4 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)3 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 TLongArrayList (gnu.trove.list.array.TLongArrayList)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2