Search in sources :

Example 1 with EntityType

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

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

the class AugmentedDiffsInspector method parseFile.

private Context parseFile(File file) throws XmlPullParserException, IOException {
    XmlPullParser parser = PlatformUtil.newXMLPullParser();
    InputStream fis = new FileInputStream(file);
    if (file.getName().endsWith(".gz")) {
        fis = new GZIPInputStream(fis);
    }
    parser.setInput(fis, "UTF-8");
    int next;
    int modify = Entity.MODIFY_UNKNOWN;
    Entity currentEntity = null;
    Way currentWay = null;
    Context ctx = new Context();
    boolean old = false;
    while ((next = parser.next()) != XmlPullParser.END_DOCUMENT) {
        if (next == XmlPullParser.END_TAG) {
            String name = parser.getName();
            if (name.equals("node") || name.equals("way") || name.equals("relation")) {
                if (currentEntity != null) {
                    if (old) {
                        updateTags(currentEntity, "name", "type", "area", "fixme");
                        ctx.oldIds.put(EntityId.valueOf(currentEntity), currentEntity);
                    } else if (modify != Entity.MODIFY_DELETED) {
                        ctx.newIds.put(EntityId.valueOf(currentEntity), currentEntity);
                    }
                }
            }
        } else if (next == XmlPullParser.START_TAG) {
            String name = parser.getName();
            if ("action".equals(name)) {
                String type = parser.getAttributeValue("", "type");
                if ("modify".equals(type)) {
                    modify = Entity.MODIFY_MODIFIED;
                } else if ("delete".equals(type)) {
                    modify = Entity.MODIFY_DELETED;
                } else if ("create".equals(type)) {
                    modify = Entity.MODIFY_CREATED;
                }
                old = false;
            } else if (name.equals("old")) {
                old = true;
            } else if (name.equals("new")) {
                old = false;
            } else if (name.equals("tag")) {
                currentEntity.putTag(parser.getAttributeValue("", "k"), parser.getAttributeValue("", "v"));
            } else if (name.equals("node")) {
                if (old || modify != Entity.MODIFY_DELETED) {
                    long id = Long.parseLong(parser.getAttributeValue("", "id"));
                    currentEntity = new Node(Double.parseDouble(parser.getAttributeValue("", "lat")), Double.parseDouble(parser.getAttributeValue("", "lon")), id);
                    parseVersion(parser, currentEntity);
                }
            } else if (name.equals("relation")) {
                long id = Long.parseLong(parser.getAttributeValue("", "id"));
                currentEntity = new Relation(id);
                parseVersion(parser, currentEntity);
            } else if (name.equals("way")) {
                long id = Long.parseLong(parser.getAttributeValue("", "id"));
                currentWay = new Way(id);
                currentEntity = currentWay;
                parseVersion(parser, currentEntity);
            } else if (name.equals("member")) {
                String tp = parser.getAttributeValue("", "type");
                long ref = Long.parseLong(parser.getAttributeValue("", "ref"));
                String role = parser.getAttributeValue("", "role");
                EntityType type = tp.equals("node") ? EntityType.NODE : (tp.equals("way") ? EntityType.WAY : EntityType.RELATION);
                EntityId nid = new EntityId(type, ref);
                Map<EntityId, Entity> o = old ? ctx.oldIds : ctx.newIds;
                boolean skip = false;
                currentWay = null;
                if (!o.containsKey(nid) || old) {
                    if (type == EntityType.NODE) {
                        Node nd = registerNewNode(parser, ctx, old, nid);
                        nid = EntityId.valueOf(nd);
                    } else if (type == EntityType.WAY) {
                        currentWay = new Way(ID_BASE--);
                        currentWay.putTag("oid", nid.getId().toString());
                        registerEntity(ctx, old, currentWay);
                        registerByOldId(ctx, old, currentWay, nid);
                        nid = EntityId.valueOf(currentWay);
                    } else if (type == EntityType.RELATION) {
                        // skip subrelations
                        // throw new UnsupportedOperationException();
                        skip = true;
                    }
                }
                if (!skip) {
                    ((Relation) currentEntity).addMember(nid.getId(), type, role);
                }
            } else if (name.equals("nd") && currentWay != null) {
                String rf = parser.getAttributeValue("", "ref");
                Node nd = null;
                EntityId nid = null;
                if (!Algorithms.isEmpty(rf) && !old) {
                    nid = new EntityId(EntityType.NODE, Long.parseLong(rf));
                    Map<EntityId, Entity> o = old ? ctx.oldIds : ctx.newIds;
                    nd = (Node) o.get(nid);
                }
                if (nd == null) {
                    nd = registerNewNode(parser, ctx, old, nid);
                }
                ((Way) currentWay).addNode(nd.getId());
            }
        }
    }
    return ctx;
}
Also used : Entity(net.osmand.osm.edit.Entity) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(net.osmand.osm.edit.Node) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileInputStream(java.io.FileInputStream) Way(net.osmand.osm.edit.Way) GZIPInputStream(java.util.zip.GZIPInputStream) EntityType(net.osmand.osm.edit.Entity.EntityType) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with EntityType

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

the class MapRenderingTypesEncoder method parseEntityConvertXML.

@Override
protected void parseEntityConvertXML(XmlPullParser parser) {
    String seq = parser.getAttributeValue("", "seq");
    if (Algorithms.isEmpty(seq)) {
        seq = "1:1";
    }
    String[] ls = seq.split(":");
    for (int ind = Integer.parseInt(ls[0]); ind <= Integer.parseInt(ls[1]); ind++) {
        Map<String, String> mp = new HashMap<String, String>();
        for (int i = 0; i < parser.getAttributeCount(); i++) {
            String at = parser.getAttributeName(i);
            mp.put(at, parser.getAttributeValue("", at).replace("*", ind + ""));
        }
        EntityConvert ec = new EntityConvert();
        // $NON-NLS-1$
        String tg = mp.get("if_region_name");
        if (tg != null) {
            ec.ifRegionName.addAll(Arrays.asList(tg.split("\\,")));
        }
        // $NON-NLS-1$
        tg = mp.get("if_not_region_name");
        if (tg != null) {
            ec.ifNotRegionName.addAll(Arrays.asList(tg.split("\\,")));
        }
        // $NON-NLS-1$
        ec.verbose = "true".equals(mp.get("verbose"));
        parseConvertCol(mp, ec.ifTags, "if_");
        parseConvertCol(mp, ec.ifStartsTags, "if_starts_with_");
        parseConvertCol(mp, ec.ifNotStartsTags, "if_not_starts_with_");
        parseConvertCol(mp, ec.ifEndsTags, "if_ends_with_");
        parseConvertCol(mp, ec.ifNotEndsTags, "if_not_ends_with_");
        parseConvertCol(mp, ec.ifContainsTags, "if_contains_");
        parseConvertCol(mp, ec.ifNotContainsTags, "if_not_contains_");
        parseConvertCol(mp, ec.ifNotTags, "if_not_");
        parseConvertCol(mp, ec.ifTagsNotLess, "if_not_less_");
        parseConvertCol(mp, ec.ifTagsLess, "if_less_");
        // $NON-NLS-1$
        ec.type = EntityConvertType.valueOf(mp.get("pattern").toUpperCase());
        ec.applyToType = EnumSet.allOf(EntityConvertApplyType.class);
        if ("no".equals(mp.get("routing")) || "false".equals(mp.get("routing"))) {
            ec.applyToType.remove(EntityConvertApplyType.ROUTING);
        }
        if ("no".equals(mp.get("map")) || "false".equals(mp.get("map"))) {
            ec.applyToType.remove(EntityConvertApplyType.MAP);
        }
        if ("no".equals(mp.get("poi")) || "false".equals(mp.get("poi"))) {
            ec.applyToType.remove(EntityConvertApplyType.POI);
        }
        parseConvertCol(mp, ec.toTags, "to_");
        // $NON-NLS-1$
        tg = mp.get("from_tag");
        // $NON-NLS-1$
        String value = mp.get("from_value");
        if (tg != null) {
            ec.fromTag = new TagValuePattern(tg, "".equals(value) ? null : value);
            if (!convertTags.containsKey(ec.fromTag.tag)) {
                convertTags.put(ec.fromTag.tag, new ArrayList<MapRenderingTypesEncoder.EntityConvert>());
            }
            convertTags.get(ec.fromTag.tag).add(ec);
        }
        // $NON-NLS-1$
        String appTo = mp.get("apply_to");
        if (appTo != null) {
            ec.applyTo = EnumSet.noneOf(EntityType.class);
            String[] tps = appTo.split(",");
            for (String t : tps) {
                EntityType et = EntityType.valueOf(t.toUpperCase());
                ec.applyTo.add(et);
            }
        }
    }
}
Also used : EntityType(net.osmand.osm.edit.Entity.EntityType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with EntityType

use of net.osmand.osm.edit.Entity.EntityType in project Osmand by osmandapp.

the class OsmBaseStorage method endElement.

public void endElement(XmlPullParser parser, String name) {
    EntityType type = null;
    if (ELEM_NODE.equals(name)) {
        type = EntityType.NODE;
    } else if (ELEM_WAY.equals(name)) {
        type = EntityType.WAY;
    } else if (ELEM_RELATION.equals(name)) {
        type = EntityType.RELATION;
    } else if (ELEM_MODIFY.equals(name)) {
        currentModify = 0;
    } else if (ELEM_CREATE.equals(name)) {
        currentModify = 0;
    } else if (ELEM_DELETE.equals(name)) {
        currentModify = 0;
    }
    if (type != null) {
        if (currentParsedEntity != null) {
            EntityId entityId = new EntityId(type, currentParsedEntity.getId());
            if (acceptEntityToLoad(entityId, currentParsedEntity)) {
                Entity oldEntity = entities.put(entityId, currentParsedEntity);
                if (parseEntityInfo && currentParsedEntityInfo != null) {
                    entityInfo.put(entityId, currentParsedEntityInfo);
                }
                if (!supressWarnings && oldEntity != null) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    throw new UnsupportedOperationException("Entity with id=" + oldEntity.getId() + " is duplicated in osm map");
                }
            } else {
            // System.gc();
            }
            currentParsedEntity = null;
        }
    }
}
Also used : EntityType(net.osmand.osm.edit.Entity.EntityType) EntityId(net.osmand.osm.edit.Entity.EntityId) Entity(net.osmand.osm.edit.Entity)

Example 5 with EntityType

use of net.osmand.osm.edit.Entity.EntityType in project Osmand by osmandapp.

the class OsmBaseStorage method startElement.

public void startElement(XmlPullParser parser, String name) {
    if (!parseStarted) {
        initRootElement(parser, name);
    }
    if (ELEM_MODIFY.equals(name)) {
        currentModify = Entity.MODIFY_MODIFIED;
    } else if (ELEM_CREATE.equals(name)) {
        currentModify = Entity.MODIFY_CREATED;
    } else if (ELEM_DELETE.equals(name)) {
        currentModify = Entity.MODIFY_DELETED;
    } else if (currentParsedEntity == null) {
        progressEntity++;
        if (progress != null && ((progressEntity % moduleProgress) == 0) && !progress.isIndeterminate() && streamForProgress != null) {
            try {
                progress.remaining(streamForProgress.available());
            } catch (IOException e) {
                progress.startWork(-1);
            }
        }
        if (ELEM_NODE.equals(name)) {
            currentParsedEntity = new Node(parseDouble(parser, ATTR_LAT, 0), parseDouble(parser, ATTR_LON, 0), parseId(parser, ATTR_ID, -1));
            currentParsedEntity.setVersion(parseVersion(parser));
        } else if (ELEM_WAY.equals(name)) {
            currentParsedEntity = new Way(parseId(parser, ATTR_ID, -1));
            currentParsedEntity.setVersion(parseVersion(parser));
        } else if (ELEM_RELATION.equals(name)) {
            currentParsedEntity = new Relation(parseId(parser, ATTR_ID, -1));
        } else {
        // this situation could be logged as unhandled
        }
        if (currentParsedEntity != null) {
            currentParsedEntity.setModify(currentModify);
            if (parseEntityInfo) {
                currentParsedEntityInfo = new EntityInfo();
                currentParsedEntityInfo.setChangeset(parser.getAttributeValue("", ATTR_CHANGESET));
                currentParsedEntityInfo.setTimestamp(parser.getAttributeValue("", ATTR_TIMESTAMP));
                currentParsedEntityInfo.setUser(parser.getAttributeValue("", ATTR_USER));
                currentParsedEntityInfo.setVersion(parser.getAttributeValue("", ATTR_VERSION));
                currentParsedEntityInfo.setVisible(parser.getAttributeValue("", ATTR_VISIBLE));
                currentParsedEntityInfo.setUid(parser.getAttributeValue("", ATTR_UID));
            }
        }
    } else {
        if (ELEM_TAG.equals(name)) {
            String key = parser.getAttributeValue("", ATTR_K);
            if (key != null) {
                if (convertTagsToLC) {
                    currentParsedEntity.putTag(key, parser.getAttributeValue("", ATTR_V));
                } else {
                    currentParsedEntity.putTagNoLC(key, parser.getAttributeValue("", ATTR_V));
                }
            }
        } else if (ELEM_ND.equals(name)) {
            Long id = parseId(parser, ATTR_REF, -1);
            if (id != -1 && currentParsedEntity instanceof Way) {
                ((Way) currentParsedEntity).addNode(id);
            }
        } else if (ELEM_MEMBER.equals(name)) {
            try {
                Long id = parseId(parser, ATTR_REF, -1);
                if (id != -1 && currentParsedEntity instanceof Relation) {
                    EntityType type = EntityType.valueOf(parser.getAttributeValue("", ATTR_TYPE).toUpperCase());
                    ((Relation) currentParsedEntity).addMember(id, type, parser.getAttributeValue("", ATTR_ROLE));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
        // this situation could be logged as unhandled
        }
    }
}
Also used : EntityType(net.osmand.osm.edit.Entity.EntityType) Relation(net.osmand.osm.edit.Relation) EntityInfo(net.osmand.osm.edit.EntityInfo) Node(net.osmand.osm.edit.Node) IOException(java.io.IOException) Way(net.osmand.osm.edit.Way) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SAXException(org.xml.sax.SAXException)

Aggregations

EntityType (net.osmand.osm.edit.Entity.EntityType)5 Entity (net.osmand.osm.edit.Entity)3 EntityId (net.osmand.osm.edit.Entity.EntityId)3 Node (net.osmand.osm.edit.Node)3 Relation (net.osmand.osm.edit.Relation)3 Way (net.osmand.osm.edit.Way)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 EntityInfo (net.osmand.osm.edit.EntityInfo)2 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 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1