Search in sources :

Example 1 with OsmandRegions

use of net.osmand.map.OsmandRegions in project OsmAnd-tools by osmandapp.

the class OsmExtractionUI method createUI.

public void createUI() {
    // $NON-NLS-1$
    frame = new JFrame(Messages.getString("OsmExtractionUI.OSMAND_MAP_CREATOR"));
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        // $NON-NLS-1$
        log.error("Can't set look and feel", e);
    }
    frame.addWindowListener(new ExitListener());
    Container content = frame.getContentPane();
    frame.setFocusable(true);
    mapPanel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory());
    mapPanel.setFocusable(true);
    mapPanel.addMapLocationListener(this);
    statusBarLabel = new JLabel();
    osmandRegions = new OsmandRegions();
    try {
        File regions = new File("regions.ocbf");
        if (!regions.exists()) {
            InputStream is = OsmandRegions.class.getResourceAsStream("regions.ocbf");
            FileOutputStream fous = new FileOutputStream(regions);
            Algorithms.streamCopy(is, fous);
            fous.close();
        }
        osmandRegions.prepareFile(regions.getAbsolutePath());
    } catch (IOException e2) {
        e2.printStackTrace();
        log.error(e2.getMessage(), e2);
    }
    content.add(statusBarLabel, BorderLayout.SOUTH);
    File workingDir = DataExtractionSettings.getSettings().getDefaultWorkingDir();
    // $NON-NLS-1$ //$NON-NLS-2$
    statusBarLabel.setText(workingDir == null ? Messages.getString("OsmExtractionUI.WORKING_DIR_UNSPECIFIED") : Messages.getString("OsmExtractionUI.WORKING_DIRECTORY") + workingDir.getAbsolutePath());
    String bdir = DataExtractionSettings.getSettings().getBinaryFilesDir();
    List<BinaryMapIndexReader> files = new ArrayList<>();
    if (!Algorithms.isEmpty(bdir)) {
        try {
            File bdirFile = new File(bdir);
            File cacheFile = new File(bdirFile, "indexes.cache");
            CachedOsmandIndexes cache = new CachedOsmandIndexes();
            if (cacheFile.exists()) {
                cache.readFromFile(cacheFile, 2);
            }
            if (bdirFile.exists() && bdirFile.listFiles() != null) {
                List<File> asList = Arrays.asList(Algorithms.getSortedFilesVersions(bdirFile));
                ArrayList<File> sortedFiles = new ArrayList<>(asList);
                // Collections.reverse(sortedFiles);
                for (File obf : sortedFiles) {
                    if (!obf.isDirectory() && obf.getName().endsWith(".obf")) {
                        try {
                            // BinaryMapIndexReader bmir = new BinaryMapIndexReader(new RandomAccessFile(obf, "r"),
                            // obf);
                            BinaryMapIndexReader bmir = cache.getReader(obf);
                            RandomAccessFile raf = new RandomAccessFile(obf, "r");
                            BinaryMapIndexReader bmir2 = new BinaryMapIndexReader(raf, bmir);
                            files.add(bmir2);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
            if (!files.isEmpty()) {
                cache.writeToFile(cacheFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    String loc = DataExtractionSettings.getSettings().getSearchLocale();
    if (loc.isEmpty()) {
        loc = null;
    }
    searchUICore = new SearchUICore(MapPoiTypes.getDefault(), loc, false);
    searchUICore.getSearchSettings().setOfflineIndexes(files);
    searchUICore.init();
    searchUICore.registerAPI(new SearchCoreFactory.SearchRegionByNameAPI());
    // treePlaces = new JTree();
    // treePlaces.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(Messages.getString("OsmExtractionUI.REGION")), false)); 	     //$NON-NLS-1$
    // JSplitPane panelForTreeAndMap = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(treePlaces), mapPanel);
    // panelForTreeAndMap.setResizeWeight(0.2);
    // content.add(panelForTreeAndMap, BorderLayout.CENTER);
    content.add(mapPanel, BorderLayout.CENTER);
    JPanel bl = new JPanel();
    bl.setLayout(new BoxLayout(bl, BoxLayout.PAGE_AXIS));
    JPanel buttonsBar = createButtonsBar();
    bl.add(buttonsBar);
    final JTextField statusField = new JTextField();
    mapPanel.setStatusField(statusField);
    bl.add(statusField);
    updateStatusField(statusField);
    content.add(bl, BorderLayout.NORTH);
    JMenuBar bar = new JMenuBar();
    fillMenuWithActions(bar);
    frame.setJMenuBar(bar);
}
Also used : JPanel(javax.swing.JPanel) SearchCoreFactory(net.osmand.search.core.SearchCoreFactory) BoxLayout(javax.swing.BoxLayout) ArrayList(java.util.ArrayList) JTextField(javax.swing.JTextField) CachedOsmandIndexes(net.osmand.binary.CachedOsmandIndexes) Container(java.awt.Container) OsmandRegions(net.osmand.map.OsmandRegions) JFrame(javax.swing.JFrame) InputStream(java.io.InputStream) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) JLabel(javax.swing.JLabel) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SAXException(org.xml.sax.SAXException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) SearchUICore(net.osmand.search.SearchUICore) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) JMenuBar(javax.swing.JMenuBar)

Example 2 with OsmandRegions

use of net.osmand.map.OsmandRegions in project OsmAnd-tools by osmandapp.

the class FixBasemapRoads method prepareRegions.

private OsmandRegions prepareRegions() throws IOException {
    OsmandRegions or = new OsmandRegions();
    File regions = new File("OsmAndMapCreator/regions.ocbf");
    if (!regions.exists()) {
        regions = new File("regions.ocbf");
    }
    if (!regions.exists()) {
        regions = new File("../../resources/countries-info/regions.ocbf");
    }
    or.prepareFile(regions.getAbsolutePath());
    or.cacheAllCountries();
    return or;
}
Also used : OsmandRegions(net.osmand.map.OsmandRegions) File(java.io.File)

Example 3 with OsmandRegions

use of net.osmand.map.OsmandRegions in project OsmAnd-tools by osmandapp.

the class WikipediaByCountryDivider method generateCountrySqlite.

protected static void generateCountrySqlite(String folder, boolean skip) throws SQLException, IOException, InterruptedException, XmlPullParserException {
    Connection conn = (Connection) DBDialect.SQLITE.getDatabaseConnection(folder + "wiki.sqlite", log);
    OsmandRegions regs = new OsmandRegions();
    regs.prepareFile(new File("resources/countries-info/regions.ocbf").getAbsolutePath());
    Map<String, LinkedList<BinaryMapDataObject>> mapObjects = regs.cacheAllCountries();
    File rgns = new File(folder, "regions");
    rgns.mkdirs();
    Map<String, String> preferredRegionLanguages = new LinkedHashMap<>();
    for (String key : mapObjects.keySet()) {
        if (key == null) {
            continue;
        }
        WorldRegion wr = regs.getRegionDataByDownloadName(key);
        if (wr == null) {
            System.out.println("Missing language for world region '" + key + "'!");
        } else {
            String regionLang = wr.getParams().getRegionLang();
            preferredRegionLanguages.put(key.toLowerCase(), regionLang);
        }
    }
    ResultSet rs = conn.createStatement().executeQuery("SELECT DISTINCT regionName  FROM wiki_region");
    while (rs.next()) {
        String lcRegionName = rs.getString(1);
        if (lcRegionName == null) {
            continue;
        }
        String regionName = Algorithms.capitalizeFirstLetterAndLowercase(lcRegionName);
        String preferredLang = preferredRegionLanguages.get(lcRegionName);
        if (preferredLang == null) {
            preferredLang = "";
        }
        LinkedList<BinaryMapDataObject> list = mapObjects.get(lcRegionName.toLowerCase());
        boolean hasWiki = false;
        if (list != null) {
            for (BinaryMapDataObject o : list) {
                Integer rl = o.getMapIndex().getRule("region_wiki", "yes");
                if (o.containsAdditionalType(rl)) {
                    hasWiki = true;
                    break;
                }
            }
        }
        if (!hasWiki) {
            System.out.println("Skip " + lcRegionName.toLowerCase() + " doesn't generate wiki");
            continue;
        }
        File fl = new File(rgns, regionName + ".sqlite");
        File osmBz2 = new File(rgns, regionName + "_" + IndexConstants.BINARY_MAP_VERSION + ".wiki.osm.bz2");
        File obfFile = new File(rgns, regionName + "_" + IndexConstants.BINARY_MAP_VERSION + ".wiki.obf");
        if (obfFile.exists() && skip) {
            continue;
        }
        fl.delete();
        osmBz2.delete();
        obfFile.delete();
        System.out.println("Generate " + fl.getName());
        Connection loc = (Connection) DBDialect.SQLITE.getDatabaseConnection(fl.getAbsolutePath(), log);
        loc.createStatement().execute("CREATE TABLE wiki_content(id long, lat double, lon double, lang text, wikiId long, title text, zipContent blob)");
        PreparedStatement insertWikiContent = loc.prepareStatement("INSERT INTO wiki_content VALUES(?, ?, ?, ?, ?, ?, ?)");
        ResultSet rps = conn.createStatement().executeQuery("SELECT WC.id, WC.lat, WC.lon, WC.lang, WC.wikiId, WC.title, WC.zipContent " + " FROM wiki_content WC INNER JOIN wiki_region WR " + " ON WC.id = WR.id AND WR.regionName = '" + rs.getString(1) + "' ORDER BY WC.id");
        FileOutputStream out = new FileOutputStream(osmBz2);
        out.write('B');
        out.write('Z');
        CBZip2OutputStream bzipStream = new CBZip2OutputStream(out);
        XmlSerializer serializer = new org.kxml2.io.KXmlSerializer();
        serializer.setOutput(bzipStream, "UTF-8");
        serializer.startDocument("UTF-8", true);
        serializer.startTag(null, "osm");
        serializer.attribute(null, "version", "0.6");
        serializer.attribute(null, "generator", "OsmAnd");
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        // indentation as 3 spaces
        // serializer.setProperty(
        // "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "   ");
        // // also set the line separator
        // serializer.setProperty(
        // "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
        int cnt = 1;
        long prevOsmId = -1;
        StringBuilder content = new StringBuilder();
        String nameUnique = null;
        boolean preferredAdded = false;
        boolean nameAdded = false;
        while (rps.next()) {
            long osmId = -rps.getLong(1);
            double lat = rps.getDouble(2);
            double lon = rps.getDouble(3);
            long wikiId = rps.getLong(5);
            String wikiLang = rps.getString(4);
            String title = rps.getString(6);
            byte[] bytes = rps.getBytes(7);
            GZIPInputStream gzin = new GZIPInputStream(new ByteArrayInputStream(bytes));
            BufferedReader br = new BufferedReader(new InputStreamReader(gzin));
            content.setLength(0);
            String s;
            while ((s = br.readLine()) != null) {
                content.append(s);
            }
            String contentStr = content.toString();
            contentStr = contentStr.replace((char) 9, ' ');
            contentStr = contentStr.replace((char) 0, ' ');
            contentStr = contentStr.replace((char) 22, ' ');
            contentStr = contentStr.replace((char) 27, ' ');
            insertWikiContent.setLong(1, osmId);
            insertWikiContent.setDouble(2, lat);
            insertWikiContent.setDouble(3, lon);
            insertWikiContent.setString(4, wikiLang);
            insertWikiContent.setLong(5, wikiId);
            insertWikiContent.setString(6, title);
            insertWikiContent.setBytes(7, bytes);
            insertWikiContent.addBatch();
            if (cnt++ % BATCH_SIZE == 0) {
                insertWikiContent.executeBatch();
            }
            if (osmId != prevOsmId) {
                if (prevOsmId != -1) {
                    closeOsmWikiNode(serializer, nameUnique, nameAdded);
                }
                prevOsmId = osmId;
                nameAdded = false;
                nameUnique = null;
                preferredAdded = false;
                serializer.startTag(null, "node");
                serializer.attribute(null, "visible", "true");
                serializer.attribute(null, "id", (osmId) + "");
                serializer.attribute(null, "lat", lat + "");
                serializer.attribute(null, "lon", lon + "");
            }
            if (wikiLang.equals("en")) {
                nameAdded = true;
                addTag(serializer, "name", title);
                addTag(serializer, "wiki_id", wikiId + "");
                addTag(serializer, "content", contentStr);
                addTag(serializer, "wiki_lang:en", "yes");
            } else {
                addTag(serializer, "name:" + wikiLang, title);
                addTag(serializer, "wiki_id:" + wikiLang, wikiId + "");
                addTag(serializer, "wiki_lang:" + wikiLang, "yes");
                if (!preferredAdded) {
                    nameUnique = title;
                    preferredAdded = preferredLang.contains(wikiLang);
                }
                addTag(serializer, "content:" + wikiLang, contentStr);
            }
        }
        if (prevOsmId != -1) {
            closeOsmWikiNode(serializer, nameUnique, nameAdded);
        }
        insertWikiContent.executeBatch();
        loc.close();
        serializer.endDocument();
        serializer.flush();
        bzipStream.close();
        System.out.println("Processed " + cnt + " pois");
        generateObf(osmBz2, obfFile);
    }
    conn.close();
}
Also used : LinkedHashMap(java.util.LinkedHashMap) GZIPInputStream(java.util.zip.GZIPInputStream) OsmandRegions(net.osmand.map.OsmandRegions) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) CBZip2OutputStream(org.apache.tools.bzip2.CBZip2OutputStream) ResultSet(java.sql.ResultSet) WorldRegion(net.osmand.map.WorldRegion) InputStreamReader(java.io.InputStreamReader) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LinkedList(java.util.LinkedList) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 4 with OsmandRegions

use of net.osmand.map.OsmandRegions in project OsmAnd-tools by osmandapp.

the class WikipediaByCountryDivider method updateCountries.

protected static void updateCountries(String folder, String regionsFile) throws IOException, SQLException {
    OsmandRegions regions = new OsmandRegions();
    regions.prepareFile(regionsFile);
    regions.cacheAllCountries();
    final GlobalWikiStructure wikiStructure = new GlobalWikiStructure(folder + "wiki.sqlite", regions, false);
    wikiStructure.updateRegions();
    wikiStructure.closeConnnection();
    System.out.println("Generation finished");
}
Also used : OsmandRegions(net.osmand.map.OsmandRegions)

Example 5 with OsmandRegions

use of net.osmand.map.OsmandRegions in project OsmAnd-tools by osmandapp.

the class CalculateCountryForChangesets method initCountriesTable.

private static OsmandRegions initCountriesTable(Connection conn, boolean empty, Map<WorldRegion, Integer> map) throws IOException, SQLException {
    OsmandRegions or = new OsmandRegions();
    File regions = new File("OsmAndMapCreator/regions.ocbf");
    if (!regions.exists()) {
        regions = new File("regions.ocbf");
    }
    or.prepareFile(regions.getAbsolutePath());
    or.cacheAllCountries();
    WorldRegion worldRegion = or.getWorldRegion();
    if (empty) {
        int id = 0;
        PreparedStatement ps = conn.prepareStatement("INSERT INTO countries(id, parentid, name, fullname, downloadname, clat, clon, map)" + " VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
        LinkedList<WorldRegion> queue = new LinkedList<WorldRegion>();
        queue.add(worldRegion);
        while (!queue.isEmpty()) {
            WorldRegion wr = queue.pollFirst();
            id++;
            map.put(wr, id);
            ps.setInt(1, id);
            WorldRegion parent = wr.getSuperregion();
            if (parent != null) {
                ps.setInt(2, map.get(parent));
            } else {
                ps.setInt(2, 0);
            }
            ps.setString(3, wr.getLocaleName());
            ps.setString(4, wr.getRegionId());
            ps.setString(5, wr.getRegionDownloadName());
            if (wr.getRegionCenter() != null) {
                ps.setDouble(6, wr.getRegionCenter().getLatitude());
                ps.setDouble(7, wr.getRegionCenter().getLongitude());
            } else {
                ps.setDouble(6, 0);
                ps.setDouble(7, 0);
            }
            ps.setInt(8, wr.isRegionMapDownload() ? 1 : 0);
            ps.addBatch();
            List<WorldRegion> lst = wr.getSubregions();
            if (lst != null) {
                queue.addAll(lst);
            }
        }
        ps.executeBatch();
        ps.close();
    }
    map.clear();
    ResultSet rs = conn.createStatement().executeQuery("select id, fullname from countries");
    while (rs.next()) {
        int id = rs.getInt(1);
        WorldRegion rd;
        if (rs.getString(2).equals("world")) {
            rd = worldRegion;
        } else {
            rd = or.getRegionData(rs.getString(2));
        }
        if (rd == null) {
            throw new UnsupportedOperationException(rs.getString(2) + " not found");
        }
        map.put(rd, id);
    }
    return or;
}
Also used : OsmandRegions(net.osmand.map.OsmandRegions) WorldRegion(net.osmand.map.WorldRegion) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) File(java.io.File) LinkedList(java.util.LinkedList)

Aggregations

OsmandRegions (net.osmand.map.OsmandRegions)20 File (java.io.File)10 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)8 WorldRegion (net.osmand.map.WorldRegion)8 LinkedList (java.util.LinkedList)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)5 IndexItem (net.osmand.plus.download.IndexItem)4 FileOutputStream (java.io.FileOutputStream)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)3 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 TagValuePair (net.osmand.binary.BinaryMapIndexReader.TagValuePair)2 AssetIndexItem (net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem)2