Search in sources :

Example 1 with HeaderBlock

use of crosby.binary.Osmformat.HeaderBlock 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)

Aggregations

BinaryParser (crosby.binary.BinaryParser)1 DenseNodes (crosby.binary.Osmformat.DenseNodes)1 HeaderBlock (crosby.binary.Osmformat.HeaderBlock)1 Info (crosby.binary.Osmformat.Info)1 MemberType (crosby.binary.Osmformat.Relation.MemberType)1 BlockInputStream (crosby.binary.file.BlockInputStream)1 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 Entity (net.osmand.osm.edit.Entity)1 EntityId (net.osmand.osm.edit.Entity.EntityId)1 EntityType (net.osmand.osm.edit.Entity.EntityType)1 EntityInfo (net.osmand.osm.edit.EntityInfo)1 Node (net.osmand.osm.edit.Node)1 Relation (net.osmand.osm.edit.Relation)1 Way (net.osmand.osm.edit.Way)1