Search in sources :

Example 6 with EntityId

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

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

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

the class AugmentedDiffsInspector method writeFile.

private File writeFile(File targetDir, String prefix, Map<EntityId, Entity> octx, Set<EntityId> oset, Map<EntityId, Entity> nctx, Set<EntityId> nset, long lastModified) throws XMLStreamException, IOException, FileNotFoundException {
    List<Node> nodes = new ArrayList<Node>();
    List<Way> ways = new ArrayList<Way>();
    List<Relation> relations = new ArrayList<Relation>();
    groupObjects(octx, oset, nodes, ways, relations);
    groupObjects(nctx, nset, nodes, ways, relations);
    File f = new File(targetDir, prefix + ".osm.gz");
    FileOutputStream fous = new FileOutputStream(f);
    GZIPOutputStream gz = new GZIPOutputStream(fous);
    new OsmStorageWriter().writeOSM(gz, new HashMap<Entity.EntityId, EntityInfo>(), nodes, ways, relations, true);
    gz.close();
    fous.close();
    f.setLastModified(lastModified);
    return f;
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Node(net.osmand.osm.edit.Node) ArrayList(java.util.ArrayList) Way(net.osmand.osm.edit.Way) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) GZIPOutputStream(java.util.zip.GZIPOutputStream) EntityInfo(net.osmand.osm.edit.EntityInfo) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 9 with EntityId

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

the class ObfDiffGenerator method comparePOI.

private void comparePOI(ObfFileInMemory fStart, ObfFileInMemory fEnd, boolean print, Set<EntityId> modifiedObjIds) {
    TLongObjectHashMap<Map<String, Amenity>> startPoiSource = fStart.getPoiObjects();
    TLongObjectHashMap<Map<String, Amenity>> endPoiSource = fEnd.getPoiObjects();
    if (endPoiSource == null) {
        return;
    }
    Map<String, Amenity> startPoi = buildPoiMap(startPoiSource);
    Map<String, Amenity> endPoi = buildPoiMap(endPoiSource);
    if (print) {
        System.out.println("Compare POI");
    }
    for (String idx : startPoi.keySet()) {
        Amenity objE = endPoi.get(idx);
        Amenity objS = startPoi.get(idx);
        EntityId aid = getAmenityId(objS);
        if (print) {
            if (objE == null) {
                System.out.println("POI " + idx + " is missing in (2): " + objS);
            } else {
                if (!objS.comparePoi(objE)) {
                    System.out.println("POI " + idx + " is not equal: " + objS + " != " + objE);
                }
                endPoi.remove(idx);
            }
        } else {
            if (objE == null) {
                if (modifiedObjIds == null || modifiedObjIds.contains(aid) || aid == null) {
                    objS.setAdditionalInfo(OSMAND_CHANGE_TAG, OSMAND_CHANGE_VALUE);
                    endPoi.put(idx, objS);
                    if (endPoiSource.get(objS.getId()) == null) {
                        endPoiSource.put(objS.getId(), new TreeMap<String, Amenity>());
                    }
                    endPoiSource.get(objS.getId()).put(objS.getType().getKeyName(), objS);
                }
            } else {
                if (objS.comparePoi(objE)) {
                    endPoi.remove(idx);
                    endPoiSource.get(objS.getId()).remove(objS.getType().getKeyName());
                }
            }
        }
    }
    if (print) {
        Iterator<Entry<String, Amenity>> it = endPoi.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Amenity> e = it.next();
            System.out.println("POI " + e.getKey() + " is missing in (1): " + e.getValue());
        }
    }
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) Amenity(net.osmand.data.Amenity) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Map(java.util.Map) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TreeMap(java.util.TreeMap)

Example 10 with EntityId

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

the class ObfDiffGenerator method compareMapData.

private void compareMapData(ObfFileInMemory fStart, ObfFileInMemory fEnd, boolean print, Set<EntityId> modifiedObjIds) {
    fStart.filterAllZoomsBelow(13);
    fEnd.filterAllZoomsBelow(13);
    MapIndex mi = fEnd.getMapIndex();
    int deleteId;
    Integer rl = mi.getRule(OSMAND_CHANGE_TAG, OSMAND_CHANGE_VALUE);
    if (rl != null) {
        deleteId = rl;
    } else {
        deleteId = mi.decodingRules.size() + 1;
        mi.initMapEncodingRule(0, deleteId, OSMAND_CHANGE_TAG, OSMAND_CHANGE_VALUE);
    }
    for (MapZoomPair mz : fStart.getZooms()) {
        TLongObjectHashMap<BinaryMapDataObject> startData = fStart.get(mz);
        TLongObjectHashMap<BinaryMapDataObject> endData = fEnd.get(mz);
        if (print) {
            System.out.println("Compare map " + mz);
        }
        if (endData == null) {
            continue;
        }
        for (Long idx : startData.keys()) {
            BinaryMapDataObject objE = endData.get(idx);
            BinaryMapDataObject objS = startData.get(idx);
            EntityId thisEntityId = getMapEntityId(objS.getId());
            if (print) {
                if (objE == null) {
                    System.out.println("Map " + idx + " is missing in (2): " + toString(objS));
                } else {
                    if (// !objS.getMapIndex().decodeType(objS.getTypes()[0]).tag.equals(OSMAND_CHANGE_TAG) &&
                    !objE.compareBinary(objS, COORDINATES_PRECISION_COMPARE)) {
                        System.out.println("Map " + idx + " is not equal: " + toString(objS) + " != " + toString(objE));
                    }
                    endData.remove(idx);
                }
            } else {
                if (objE == null) {
                    if (modifiedObjIds == null || modifiedObjIds.contains(thisEntityId) || thisEntityId == null) {
                        BinaryMapDataObject obj = new BinaryMapDataObject(idx, objS.getCoordinates(), null, objS.getObjectType(), objS.isArea(), new int[] { deleteId }, null);
                        endData.put(idx, obj);
                    }
                } else if (objE.compareBinary(objS, COORDINATES_PRECISION_COMPARE)) {
                    endData.remove(idx);
                }
            }
        }
        if (print) {
            for (BinaryMapDataObject e : endData.valueCollection()) {
                System.out.println("Map " + e.getId() + " is missing in (1): " + toString(e));
            }
        }
    }
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex)

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