Search in sources :

Example 1 with IOsmStorageFilter

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

the class OsmExtractionUI method fillMenuWithActions.

public void fillMenuWithActions(final JMenuBar bar) {
    // $NON-NLS-1$
    JMenu menu = new JMenu(Messages.getString("OsmExtractionUI.MENU_FILE"));
    bar.add(menu);
    // $NON-NLS-1$
    JMenuItem loadFile = new JMenuItem(Messages.getString("OsmExtractionUI.MENU_SELECT_FILE"));
    menu.add(loadFile);
    // $NON-NLS-1$
    JMenuItem loadSpecifiedAreaFile = new JMenuItem(Messages.getString("OsmExtractionUI.MENU_SELECT_OSM_FILE_AREA"));
    menu.add(loadSpecifiedAreaFile);
    // $NON-NLS-1$
    JMenuItem specifyWorkingDir = new JMenuItem(Messages.getString("OsmExtractionUI.SPECIFY_WORKING_DIR"));
    menu.add(specifyWorkingDir);
    menu.addSeparator();
    // $NON-NLS-1$
    JMenuItem exitMenu = new JMenuItem(Messages.getString("OsmExtractionUI.MENU_EXIT"));
    menu.add(exitMenu);
    // JMenu tileSource = new JMenu(Messages.getString("OsmExtractionUI.MENU_MAPS")); //$NON-NLS-1$MapPanel.getMenuToChooseSource(mapPanel);
    // $NON-NLS-1$
    final JMenuItem sqliteDB = new JMenuItem(Messages.getString("OsmExtractionUI.MENU_CREATE_SQLITE"));
    // tileSource.addSeparator();
    // tileSource.add(sqliteDB);
    JMenu tileSource = MapPanel.getMenuToChooseSource(mapPanel);
    tileSource.addSeparator();
    tileSource.add(sqliteDB);
    bar.add(tileSource);
    // $NON-NLS-1$
    menu = new JMenu(Messages.getString("OsmExtractionUI.MENU_WINDOW"));
    bar.add(menu);
    // $NON-NLS-1$
    JMenuItem settings = new JMenuItem(Messages.getString("OsmExtractionUI.MENU_SETTINGS"));
    menu.add(settings);
    menu.addSeparator();
    // $NON-NLS-1$
    JMenuItem openLogFile = new JMenuItem(Messages.getString("OsmExtractionUI.MENU_OPEN_LOG"));
    menu.add(openLogFile);
    // $NON-NLS-1$
    menu = new JMenu(Messages.getString("OsmExtractionUI.MENU_ABOUT"));
    bar.add(menu);
    // $NON-NLS-1$
    JMenuItem aboutApplication = new JMenuItem(Messages.getString("OsmExtractionUI.MENU_ABOUT_2"));
    menu.add(aboutApplication);
    aboutApplication.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(frame, MapCreatorVersion.APP_MAP_CREATOR_FULL_NAME);
        }
    });
    openLogFile.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            File file = new File(OsmExtractionUI.LOG_PATH);
            if (file != null && file.exists()) {
                if (System.getProperty("os.name").startsWith("Windows")) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    try {
                        // $NON-NLS-1$
                        Runtime.getRuntime().exec(new String[] { "notepad.exe", file.getAbsolutePath() });
                    } catch (IOException es) {
                        // $NON-NLS-1$
                        ExceptionHandler.handle(Messages.getString("OsmExtractionUI.UNABLE_OPEN_FILE"), es);
                    }
                } else {
                    // $NON-NLS-1$
                    JOptionPane.showMessageDialog(frame, Messages.getString("OsmExtractionUI.OPEN_LOG_FILE_MANUALLY") + LOG_PATH);
                }
            } else {
                // $NON-NLS-1$
                ExceptionHandler.handle(Messages.getString("OsmExtractionUI.LOG_FILE_NOT_FOUND"));
            }
        }
    });
    sqliteDB.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // $NON-NLS-1$
            final String regionName = OsmExtractionUI.this.regionName == null ? Messages.getString("OsmExtractionUI.REGION") : OsmExtractionUI.this.regionName;
            final ITileSource map = mapPanel.getMap();
            if (map != null) {
                try {
                    // $NON-NLS-1$
                    final ProgressDialog dlg = new ProgressDialog(frame, Messages.getString("OsmExtractionUI.CREATING_INDEX"));
                    dlg.setRunnable(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                SQLiteBigPlanetIndex.createSQLiteDatabase(DataExtractionSettings.getSettings().getTilesDirectory(), regionName, map);
                            } catch (SQLException e1) {
                                throw new IllegalArgumentException(e1);
                            } catch (IOException e1) {
                                throw new IllegalArgumentException(e1);
                            }
                        }
                    });
                    dlg.run();
                } catch (InterruptedException e1) {
                    // $NON-NLS-1$
                    log.error("Interrupted", e1);
                } catch (InvocationTargetException e1) {
                    // $NON-NLS-1$
                    ExceptionHandler.handle("Can't create big planet sqlite index", e1.getCause());
                }
            }
        }
    });
    exitMenu.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            exit();
        }
    });
    settings.addActionListener(new ActionListener() {

        private void applySettings() {
            mapPanel.applySettings();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            OsmExtractionPreferencesDialog dlg = new OsmExtractionPreferencesDialog(frame);
            dlg.showDialog();
            applySettings();
        }
    });
    specifyWorkingDir.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = new JFileChooser();
            // $NON-NLS-1$
            fc.setDialogTitle(Messages.getString("OsmExtractionUI.CHOOSE_WORKING_DIR"));
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            File workingDir = DataExtractionSettings.getSettings().getDefaultWorkingDir();
            if (workingDir != null) {
                fc.setCurrentDirectory(workingDir);
            }
            if (fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null && fc.getSelectedFile().isDirectory()) {
                DataExtractionSettings.getSettings().saveDefaultWorkingDir(fc.getSelectedFile());
                mapPanel.setTilesLocation(DataExtractionSettings.getSettings().getTilesDirectory());
                // $NON-NLS-1$
                statusBarLabel.setText(Messages.getString("OsmExtractionUI.WORKING_DIR") + fc.getSelectedFile().getAbsolutePath());
                JMenu tileSource = MapPanel.getMenuToChooseSource(mapPanel);
                tileSource.add(sqliteDB);
                bar.remove(1);
                bar.add(tileSource, 1);
            }
        }
    });
    loadSpecifiedAreaFile.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = getOsmFileChooser();
            int answer = fc.showOpenDialog(frame);
            if (answer == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null) {
                final JDialog dlg = new JDialog(frame, true);
                // $NON-NLS-1$
                dlg.setTitle(Messages.getString("OsmExtractionUI.SELECT_AREA_TO_FILTER"));
                MapPanel panel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory());
                panel.setLatLon(mapPanel.getLatitude(), mapPanel.getLongitude());
                panel.setZoom(mapPanel.getZoom());
                final StringBuilder res = new StringBuilder();
                panel.getLayer(MapInformationLayer.class).setAreaActionHandler(new // $NON-NLS-1$
                AbstractAction(// $NON-NLS-1$
                Messages.getString("OsmExtractionUI.SELECT_AREA")) {

                    private static final long serialVersionUID = -3452957517341961969L;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        res.append(true);
                        dlg.setVisible(false);
                    }
                });
                dlg.add(panel);
                JMenuBar bar = new JMenuBar();
                bar.add(MapPanel.getMenuToChooseSource(panel));
                dlg.setJMenuBar(bar);
                dlg.setSize(512, 512);
                double x = frame.getBounds().getCenterX();
                double y = frame.getBounds().getCenterY();
                dlg.setLocation((int) x - dlg.getWidth() / 2, (int) y - dlg.getHeight() / 2);
                dlg.setVisible(true);
                if (res.length() > 0 && panel.getSelectionArea().isVisible()) {
                    MapSelectionArea area = panel.getSelectionArea();
                    IOsmStorageFilter filter = new OsmBoundsFilter(area.getLat1(), area.getLon1(), area.getLat2(), area.getLon2());
                    loadCountry(fc.getSelectedFile(), filter);
                }
            }
        }
    });
    loadFile.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = getOsmFileChooser();
            int answer = fc.showOpenDialog(frame);
            if (answer == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null) {
                loadCountry(fc.getSelectedFile(), null);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) ActionEvent(java.awt.event.ActionEvent) OsmBoundsFilter(net.osmand.osm.io.OsmBoundsFilter) MapSelectionArea(net.osmand.swing.MapPanel.MapSelectionArea) JMenuItem(javax.swing.JMenuItem) AbstractAction(javax.swing.AbstractAction) IOsmStorageFilter(net.osmand.osm.io.IOsmStorageFilter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ActionListener(java.awt.event.ActionListener) JFileChooser(javax.swing.JFileChooser) ITileSource(net.osmand.map.ITileSource) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) JMenu(javax.swing.JMenu) JDialog(javax.swing.JDialog) JMenuBar(javax.swing.JMenuBar)

Example 2 with IOsmStorageFilter

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

the class IndexCreator method extractOsmToNodesDB.

private OsmDbCreator extractOsmToNodesDB(OsmDbAccessor accessor, File readFile, IProgress progress, IOsmStorageFilter addFilter, int additionId, int shiftId, boolean ovewriteIds, boolean generateNewIds, boolean createTables, OsmDbCreator previous) throws IOException, SQLException, XmlPullParserException {
    boolean pbfFile = false;
    InputStream stream = new BufferedInputStream(new FileInputStream(readFile), 8192 * 4);
    InputStream streamFile = stream;
    long st = System.currentTimeMillis();
    if (readFile.getName().endsWith(".bz2")) {
        // $NON-NLS-1$
        if (stream.read() != 'B' || stream.read() != 'Z') {
        // throw new RuntimeException("The source stream must start with the characters BZ if it is to be read as a BZip2 stream."); //$NON-NLS-1$
        } else {
            stream = new CBZip2InputStream(stream);
        }
    } else if (readFile.getName().endsWith(".gz")) {
        // $NON-NLS-1$
        stream = new GZIPInputStream(stream);
    } else if (readFile.getName().endsWith(".pbf")) {
        // $NON-NLS-1$
        pbfFile = true;
    }
    OsmBaseStorage storage = pbfFile ? new OsmBaseStoragePbf() : new OsmBaseStorage();
    storage.setSupressWarnings(DataExtractionSettings.getSettings().isSupressWarningsForDuplicatedId());
    if (addFilter != null) {
        storage.getFilters().add(addFilter);
    }
    storage.getFilters().add(new IOsmStorageFilter() {

        @Override
        public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity entity) {
            if (indexAddressCreator != null) {
                indexAddressCreator.registerCityIfNeeded(entity);
            }
            // accept to allow db creator parse it
            return true;
        }
    });
    // 1. Loading osm file
    OsmDbCreator dbCreator = new OsmDbCreator(additionId, shiftId, ovewriteIds, generateNewIds);
    if (previous != null) {
        dbCreator.setNodeIds(previous.getNodeIds());
        dbCreator.setWayIds(previous.getWayIds());
        dbCreator.setRelationIds(previous.getRelationIds());
    }
    dbCreator.setBackwardCompatibleIds(backwardCompatibleIds);
    try {
        // $NON-NLS-1$
        setGeneralProgress(progress, "[15 / 100]");
        // $NON-NLS-1$
        progress.startTask(Messages.getString("IndexCreator.LOADING_FILE") + readFile.getAbsolutePath(), -1);
        // 1 init database to store temporary data
        dbCreator.initDatabase(osmDBdialect, accessor.getDbConn(), createTables);
        storage.getFilters().add(dbCreator);
        if (pbfFile) {
            ((OsmBaseStoragePbf) storage).parseOSMPbf(stream, progress, false);
        } else {
            storage.parseOSM(stream, progress, streamFile, false);
        }
        dbCreator.finishLoading();
        osmDBdialect.commitDatabase(accessor.getDbConn());
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$
            log.info("File parsed : " + (System.currentTimeMillis() - st));
        }
        progress.finishTask();
        return dbCreator;
    } finally {
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$
            log.info("File indexed : " + (System.currentTimeMillis() - st));
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OsmBaseStoragePbf(net.osmand.osm.io.OsmBaseStoragePbf) IOsmStorageFilter(net.osmand.osm.io.IOsmStorageFilter) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) EntityId(net.osmand.osm.edit.Entity.EntityId) BufferedInputStream(java.io.BufferedInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage)

Example 3 with IOsmStorageFilter

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

IOsmStorageFilter (net.osmand.osm.io.IOsmStorageFilter)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 JMenuBar (javax.swing.JMenuBar)2 Entity (net.osmand.osm.edit.Entity)2 EntityId (net.osmand.osm.edit.Entity.EntityId)2 OsmBaseStorage (net.osmand.osm.io.OsmBaseStorage)2 Container (java.awt.Container)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1