Search in sources :

Example 6 with OsmStorageWriter

use of net.osmand.osm.io.OsmStorageWriter in project OsmAnd-tools by osmandapp.

the class FixLinkedCoastline 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());
    List<EntityId> toWrite = new ArrayList<EntityId>();
    for (EntityId e : entities.keySet()) {
        if (e.getType() == EntityType.WAY) {
            Entity oldWay = storage.getRegisteredEntities().remove(e);
            List<Way> result = processWay((Way) oldWay);
            alignAndAddtoStorage(storage, toWrite, result);
        }
    }
    System.out.println("ERROR Ways : ");
    int errors = 0;
    for (List<Way> w : endWays.values()) {
        Way way = w.get(0);
        Way lway = w.get(w.size() - 1);
        LatLon first = way.getNodes().get(0).getLatLon();
        LatLon last = lway.getNodes().get(lway.getNodes().size() - 1).getLatLon();
        double dist = MapUtils.getDistance(first, last);
        if (dist < 500000) {
            alignAndAddtoStorage(storage, toWrite, w);
        } else {
            errors++;
            String val = "First " + first + "Last " + last + " id " + way.getId() + " dist " + MapUtils.getDistance(first, last) + " m";
            System.out.println("Ways in chain - " + w.size() + " - " + val);
        }
    }
    System.out.println("Fixed errors : " + ERRORS + ", errors not fixed : " + errors);
    OsmStorageWriter writer = new OsmStorageWriter();
    /*Set<Long> ls = new HashSet<Long>();
        for(EntityId e : toWrite) {
            if(!ls.add(e.getId())) {
                System.out.println("!!" + e.getId());
            }
        }*/
    writer.saveStorage(new FileOutputStream(write), storage, toWrite, true);
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Entity(net.osmand.osm.edit.Entity) 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)

Example 7 with OsmStorageWriter

use of net.osmand.osm.io.OsmStorageWriter in project OsmAnd-tools by osmandapp.

the class OsmExtractionUI method saveCountry.

public void saveCountry(final File f, final OsmBaseStorage storage) {
    final OsmStorageWriter writer = new OsmStorageWriter();
    try {
        // $NON-NLS-1$
        final ProgressDialog dlg = new ProgressDialog(frame, Messages.getString("OsmExtractionUI.SAVING_OSM_FILE"));
        dlg.setRunnable(new Runnable() {

            @Override
            public void run() {
                try {
                    OutputStream output = new FileOutputStream(f);
                    try {
                        if (f.getName().endsWith(".bz2")) {
                            // $NON-NLS-1$
                            output.write('B');
                            output.write('Z');
                            output = new CBZip2OutputStream(output);
                        }
                        writer.saveStorage(output, storage, null, false);
                    } finally {
                        output.close();
                    }
                } catch (IOException e) {
                    throw new IllegalArgumentException(e);
                } catch (XMLStreamException e) {
                    throw new IllegalArgumentException(e);
                }
            }
        });
        dlg.run();
    } catch (InterruptedException e1) {
        // $NON-NLS-1$
        log.error("Interrupted", e1);
    } catch (InvocationTargetException e1) {
        // $NON-NLS-1$
        ExceptionHandler.handle("Log file is not found", e1.getCause());
    }
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) CBZip2OutputStream(org.apache.tools.bzip2.CBZip2OutputStream) XMLStreamException(javax.xml.stream.XMLStreamException) OutputStream(java.io.OutputStream) CBZip2OutputStream(org.apache.tools.bzip2.CBZip2OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with OsmStorageWriter

use of net.osmand.osm.io.OsmStorageWriter 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)

Aggregations

FileOutputStream (java.io.FileOutputStream)8 OsmStorageWriter (net.osmand.osm.io.OsmStorageWriter)8 OsmBaseStorage (net.osmand.osm.io.OsmBaseStorage)6 EntityId (net.osmand.osm.edit.Entity.EntityId)5 Way (net.osmand.osm.edit.Way)5 FileInputStream (java.io.FileInputStream)4 ConsoleProgressImplementation (net.osmand.impl.ConsoleProgressImplementation)4 Entity (net.osmand.osm.edit.Entity)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LatLon (net.osmand.data.LatLon)3 Node (net.osmand.osm.edit.Node)3 File (java.io.File)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 EntityInfo (net.osmand.osm.edit.EntityInfo)2 Relation (net.osmand.osm.edit.Relation)2