Search in sources :

Example 21 with EntityId

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

the class AHSupermarketResolver method updateOSMFile.

// this file could be retrieved using xapi
// http://xapi.openstreetmap.org/api/0.6/*[shop=supermarket][bbox=2.5,50,7.8,53.5]
public void updateOSMFile(String pathToOsmFile, String pathToModifiedFile, boolean show) throws IOException, XMLStreamException, JSONException, XmlPullParserException {
    OsmBaseStorage storage = new OsmBaseStorage();
    final Map<String, EntityId> winkelNumbers = new LinkedHashMap<String, EntityId>();
    storage.getFilters().add(new IOsmStorageFilter() {

        @Override
        public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity entity) {
            if (entity.getTag("winkelnummer") != null && entity.getTag("name").contains("eijn")) {
                winkelNumbers.put(entity.getTag("winkelnummer"), entityId);
                return true;
            }
            // register all nodes in order to operate with ways
            return true;
        }
    });
    storage.parseOSM(new FileInputStream(pathToOsmFile), new ConsoleProgressImplementation(2), null, true);
    Map<String, Map<String, Object>> supermarkets = getSupermarkets();
    DataTileManager<Entity> deleted = new DataTileManager<Entity>();
    for (String s : winkelNumbers.keySet()) {
        if (!supermarkets.containsKey(s)) {
            System.err.println("Shop " + s + " id=" + winkelNumbers.get(s) + " doesn't present on the site.");
            EntityId e = winkelNumbers.get(s);
            Entity en = storage.getRegisteredEntities().get(e);
            deleted.registerObject(en.getLatLon().getLatitude(), en.getLatLon().getLongitude(), en);
        }
    }
    DataTileManager<Entity> notCorrelated = new DataTileManager<Entity>();
    DataTileManager<Entity> notShown = new DataTileManager<Entity>();
    for (String s : supermarkets.keySet()) {
        Map<String, Object> props = supermarkets.get(s);
        if (winkelNumbers.get(s) != null) {
            EntityId id = winkelNumbers.get(s);
            Entity e = storage.getRegisteredEntities().get(id);
            EntityInfo info = storage.getRegisteredEntityInfo().get(id);
            Map<String, String> newTags = new LinkedHashMap<String, String>();
            String p = String.valueOf(props.get("format"));
            // IMPORTANT : comment what information should be updated or check
            String name = "Albert Heijn";
            if (!p.equals("AH")) {
                name += " " + p;
            }
            newTags.put("name", name);
            newTags.put("phone", String.valueOf(props.get("phone")));
            newTags.put("addr:city", String.valueOf(props.get("city")));
            newTags.put("addr:street", String.valueOf(props.get("street")));
            newTags.put("addr:housenumber", String.valueOf(props.get("housenr")));
            newTags.put("addr:postcode", String.valueOf(props.get("zip")));
            JSONArray o = (JSONArray) props.get("hours");
            OpeningHoursParser.OpeningHours rules = new OpeningHoursParser.OpeningHours();
            BasicOpeningHourRule prev = null;
            for (int i = 0; i < 7; i++) {
                JSONObject obj = o.getJSONObject(i);
                if (!obj.isNull("C") && obj.getBoolean("C")) {
                } else {
                    String opened = String.valueOf(obj.get("F"));
                    String closed = String.valueOf(obj.get("U"));
                    int start = Integer.parseInt(opened.substring(0, 2)) * 60 + Integer.parseInt(opened.substring(2));
                    int end = Integer.parseInt(closed.substring(0, 2)) * 60 + Integer.parseInt(closed.substring(2));
                    if (prev != null && prev.getStartTime() == start && prev.getEndTime() == end) {
                        prev.getDays()[i] = true;
                    } else {
                        BasicOpeningHourRule rule = new OpeningHoursParser.BasicOpeningHourRule();
                        rule.getDays()[i] = true;
                        rule.addTimeRange(start, end);
                        prev = rule;
                        rules.addRule(rule);
                    }
                }
            }
            newTags.put("opening_hours", rules.toString());
            // Check distance to info
            LatLon real = new LatLon((Double) props.get("lat"), (Double) props.get("lng"));
            double dist = MapUtils.getDistance(e.getLatLon(), real);
            if (dist > 150) {
                // TODO move shop ?
                System.err.println("Winkel number = " + s + " is too far from site info - " + dist + " m !!! " + real);
                if (dist > 300) {
                    notCorrelated.registerObject(real.getLatitude(), real.getLongitude(), e);
                }
            }
            boolean changed = false;
            for (String k : newTags.keySet()) {
                String val = newTags.get(k);
                if (!Algorithms.objectEquals(val, e.getTag(k))) {
                    e.putTag(k, val);
                    changed = true;
                }
            }
            if (changed) {
                info.setAction("modify");
            }
        } else {
            // TODO?
            LatLon real = new LatLon((Double) props.get("lat"), (Double) props.get("lng"));
            System.err.println("Winkel number = " + s + " is not found in database !!! " + real);
            Node n = new Node(real.getLatitude(), real.getLongitude(), -1);
            n.putTag("winkelnummer", "REG : " + s);
            notShown.registerObject(real.getLatitude(), real.getLongitude(), n);
        }
    }
    OsmStorageWriter writer = new OsmStorageWriter();
    writer.saveStorage(new FileOutputStream(pathToModifiedFile), storage, null, true);
    if (show) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        final MapPanel panel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory());
        panel.setFocusable(true);
        MapPointsLayer toAdd = panel.getLayer(MapPointsLayer.class);
        toAdd.setPoints(notShown);
        toAdd.setPointSize(5);
        toAdd.setTagToShow("winkelnummer");
        MapPointsLayer red = new MapPointsLayer();
        red.setPoints(deleted);
        red.setColor(Color.red);
        red.setPointSize(5);
        panel.addLayer(red);
        MapPointsLayer blue = new MapPointsLayer();
        blue.setPoints(notCorrelated);
        blue.setColor(Color.blue);
        blue.setPointSize(4);
        panel.addLayer(blue);
        JFrame frame = new JFrame("Map view");
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                DataExtractionSettings settings = DataExtractionSettings.getSettings();
                settings.saveDefaultLocation(panel.getLatitude(), panel.getLongitude());
                settings.saveDefaultZoom(panel.getZoom());
                System.exit(0);
            }
        });
        Container content = frame.getContentPane();
        content.add(panel, BorderLayout.CENTER);
        JMenuBar bar = new JMenuBar();
        bar.add(MapPanel.getMenuToChooseSource(panel));
        frame.setJMenuBar(bar);
        frame.setSize(512, 512);
        frame.setVisible(true);
    }
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Entity(net.osmand.osm.edit.Entity) BasicOpeningHourRule(net.osmand.util.OpeningHoursParser.BasicOpeningHourRule) MapPanel(net.osmand.swing.MapPanel) Node(net.osmand.osm.edit.Node) WindowAdapter(java.awt.event.WindowAdapter) LinkedHashMap(java.util.LinkedHashMap) Container(java.awt.Container) DataTileManager(net.osmand.data.DataTileManager) JFrame(javax.swing.JFrame) IOsmStorageFilter(net.osmand.osm.io.IOsmStorageFilter) JSONArray(org.json.JSONArray) DataExtractionSettings(net.osmand.swing.DataExtractionSettings) ConsoleProgressImplementation(net.osmand.impl.ConsoleProgressImplementation) OpeningHoursParser(net.osmand.util.OpeningHoursParser) FileInputStream(java.io.FileInputStream) JSONException(org.json.JSONException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) EntityId(net.osmand.osm.edit.Entity.EntityId) LatLon(net.osmand.data.LatLon) MapPointsLayer(net.osmand.swing.MapPointsLayer) JSONObject(org.json.JSONObject) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) EntityInfo(net.osmand.osm.edit.EntityInfo) FileOutputStream(java.io.FileOutputStream) WindowEvent(java.awt.event.WindowEvent) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JMenuBar(javax.swing.JMenuBar)

Example 22 with EntityId

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

the class GenerateRegionTags method iterateOverEntities.

private static void iterateOverEntities(Map<EntityId, Entity> ids, OsmandRegions or) throws IOException {
    Map<EntityId, TreeSet<String>> mp = new LinkedHashMap<Entity.EntityId, TreeSet<String>>();
    LOG.info("About to process " + ids.size() + " entities");
    LOG.info("Processing nodes...");
    long i = 0;
    for (Entity e : ids.values()) {
        if (e instanceof Node) {
            i++;
            printProgress(i, ids.size());
            int y = MapUtils.get31TileNumberY(((Node) e).getLatitude());
            int x = MapUtils.get31TileNumberX(((Node) e).getLongitude());
            List<BinaryMapDataObject> l = or.query(x, y);
            EntityId id = EntityId.valueOf(e);
            TreeSet<String> lst = new TreeSet<String>();
            mp.put(id, lst);
            for (BinaryMapDataObject b : l) {
                if (or.contain(b, x, y)) {
                    String dw = or.getDownloadName(b);
                    if (!Algorithms.isEmpty(dw) && or.isDownloadOfType(b, OsmandRegions.MAP_TYPE)) {
                        lst.add(dw);
                    }
                }
            }
            if (!e.getTags().isEmpty()) {
                e.putTag(MapRenderingTypesEncoder.OSMAND_REGION_NAME_TAG, serialize(lst));
            }
        }
    }
    LOG.info("Processing ways...");
    for (Entity e : ids.values()) {
        if (e instanceof Way) {
            i++;
            printProgress(i, ids.size());
            Way w = (Way) e;
            TreeSet<String> lst = new TreeSet<String>();
            for (EntityId id : w.getEntityIds()) {
                TreeSet<String> ls = mp.get(id);
                if (ls != null) {
                    lst.addAll(ls);
                }
            }
            if (!e.getTags().isEmpty()) {
                e.putTag(MapRenderingTypesEncoder.OSMAND_REGION_NAME_TAG, serialize(lst));
            }
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) Node(net.osmand.osm.edit.Node) Way(net.osmand.osm.edit.Way) LinkedHashMap(java.util.LinkedHashMap) EntityId(net.osmand.osm.edit.Entity.EntityId) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TreeSet(java.util.TreeSet)

Example 23 with EntityId

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

the class AugmentedDiffsInspector method prepareRegions.

private void prepareRegions(Context ctx, Map<EntityId, Entity> ids, Map<String, Set<EntityId>> regionsMap, OsmandRegions osmandRegions) throws IOException {
    Map<EntityId, Set<String>> mp = new HashMap<Entity.EntityId, Set<String>>();
    for (Entity e : ids.values()) {
        if (e instanceof Node) {
            int y = MapUtils.get31TileNumberY(((Node) e).getLatitude());
            int x = MapUtils.get31TileNumberX(((Node) e).getLongitude());
            EntityId id = EntityId.valueOf(e);
            TreeSet<String> lst = new TreeSet<String>();
            mp.put(id, lst);
            if (osmandRegions == null) {
                addEntityToRegion(regionsMap, id, lst, DEFAULT_REGION);
            } else {
                List<BinaryMapDataObject> l = osmandRegions.query(x, y);
                for (BinaryMapDataObject b : l) {
                    if (osmandRegions.contain(b, x, y)) {
                        String dw = osmandRegions.getDownloadName(b);
                        if (!Algorithms.isEmpty(dw) && osmandRegions.isDownloadOfType(b, OsmandRegions.MAP_TYPE)) {
                            addEntityToRegion(regionsMap, id, lst, dw);
                        }
                    }
                }
            }
        }
    }
    // 2. add ways and complete ways with missing nodes
    for (Entity e : ids.values()) {
        if (e instanceof Way) {
            Way w = (Way) e;
            EntityId wid = EntityId.valueOf(w);
            TreeSet<String> lst = new TreeSet<String>();
            mp.put(wid, lst);
            for (EntityId it : w.getEntityIds()) {
                Set<String> countries = mp.get(it);
                for (String cnt : countries) {
                    regionsMap.get(cnt).add(wid);
                }
                lst.addAll(countries);
            }
            // complete ways with missing nodes
            for (EntityId it : w.getEntityIds()) {
                mp.get(it).addAll(lst);
                for (String s : lst) {
                    regionsMap.get(s).add(it);
                }
            }
        }
    }
    // 3. add relations (not complete with ways or nodes)
    for (Entity e : ids.values()) {
        if (e instanceof Relation) {
            Relation r = (Relation) e;
            EntityId rid = EntityId.valueOf(r);
            TreeSet<String> lst = new TreeSet<String>();
            mp.put(rid, lst);
            for (RelationMember it : r.getMembers()) {
                Set<String> countries = mp.get(it.getEntityId());
                for (String cnt : countries) {
                    regionsMap.get(cnt).add(rid);
                }
                lst.addAll(countries);
            }
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TreeSet(java.util.TreeSet)

Example 24 with EntityId

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

the class OpenstreetmapRemoteUtil method loadNode.

@Override
public Node loadNode(Amenity n) {
    if (n.getId() % 2 == 1) {
        // that's way id
        return null;
    }
    long nodeId = n.getId() >> 1;
    try {
        String res = sendRequest(getSiteApi() + "api/0.6/node/" + nodeId, "GET", null, ctx.getString(R.string.loading_poi_obj) + nodeId, // $NON-NLS-1$ //$NON-NLS-2$
        false);
        if (res != null) {
            OsmBaseStorage st = new OsmBaseStorage();
            st.setConvertTagsToLC(false);
            // $NON-NLS-1$
            st.parseOSM(new ByteArrayInputStream(res.getBytes("UTF-8")), null, null, true);
            EntityId id = new Entity.EntityId(EntityType.NODE, nodeId);
            Node entity = (Node) st.getRegisteredEntities().get(id);
            entityInfo = st.getRegisteredEntityInfo().get(id);
            entityInfoId = id;
            // check whether this is node (because id of node could be the same as relation)
            if (entity != null && MapUtils.getDistance(entity.getLatLon(), n.getLocation()) < 50) {
                PoiType poiType = n.getType().getPoiTypeByKeyName(n.getSubType());
                if (poiType.getOsmValue().equals(entity.getTag(poiType.getOsmTag()))) {
                    entity.removeTag(poiType.getOsmTag());
                    entity.putTagNoLC(EditPoiData.POI_TYPE_TAG, poiType.getTranslation());
                } else {
                // later we could try to determine tags
                }
                return entity;
            }
            return null;
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        log.error("Loading node failed " + nodeId, e);
        ctx.runInUIThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(ctx, ctx.getResources().getString(R.string.shared_string_io_error), Toast.LENGTH_LONG).show();
            }
        });
    }
    return null;
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) ByteArrayInputStream(java.io.ByteArrayInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) Node(net.osmand.osm.edit.Node) PoiType(net.osmand.osm.PoiType) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 25 with EntityId

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

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