private void writeRouteStops(TransportRoute r, List<TransportStop> stops, boolean direction)
     throws SQLException {
   int i = 0;
   for (TransportStop s : stops) {
     if (!visitedStops.contains(s.getId())) {
       transStopsStat.setLong(1, s.getId());
       transStopsStat.setDouble(2, s.getLocation().getLatitude());
       transStopsStat.setDouble(3, s.getLocation().getLongitude());
       transStopsStat.setString(4, s.getName());
       transStopsStat.setString(5, s.getEnName());
       int x = (int) MapUtils.getTileNumberX(24, s.getLocation().getLongitude());
       int y = (int) MapUtils.getTileNumberY(24, s.getLocation().getLatitude());
       addBatch(transStopsStat);
       try {
         transportStopsTree.insert(new LeafElement(new Rect(x, y, x, y), s.getId()));
       } catch (RTreeInsertException e) {
         throw new IllegalArgumentException(e);
       } catch (IllegalValueException e) {
         throw new IllegalArgumentException(e);
       }
       visitedStops.add(s.getId());
     }
     transRouteStopsStat.setLong(1, r.getId());
     transRouteStopsStat.setLong(2, s.getId());
     transRouteStopsStat.setInt(3, direction ? 1 : 0);
     transRouteStopsStat.setInt(4, i++);
     addBatch(transRouteStopsStat);
   }
 }
 public void commitAndCloseFiles(
     String rtreeStopsFileName, String rtreeStopsPackFileName, boolean deleteDatabaseIndexes)
     throws IOException, SQLException {
   // delete transport rtree files
   if (transportStopsTree != null) {
     transportStopsTree.getFileHdr().getFile().close();
     File f = new File(rtreeStopsFileName);
     if (f.exists() && deleteDatabaseIndexes) {
       f.delete();
     }
     f = new File(rtreeStopsPackFileName);
     if (f.exists() && deleteDatabaseIndexes) {
       f.delete();
     }
   }
   closeAllPreparedStatements();
 }
 private void createObfFiles(File country, GroupFiles g) throws Exception {
   File obf = g.getObfFileName(country);
   if (!obf.exists() || g.getTimestamp() - obf.lastModified() > 1000) {
     if (obf.exists()) {
       log.info(
           "The file "
               + obf.getName()
               + " was updated for "
               + (g.getTimestamp() - obf.lastModified()) / 1000
               + " seconds");
     } else {
       log.info("The file " + obf.getName() + " doesn't exist");
     }
     RTree.clearCache();
     IndexCreator ic = new IndexCreator(country);
     ic.setIndexAddress(false);
     ic.setIndexPOI(true);
     ic.setIndexRouting(true);
     ic.setIndexMap(true);
     ic.setGenerateLowLevelIndexes(false);
     ic.setDialects(DBDialect.SQLITE_IN_MEMORY, DBDialect.SQLITE_IN_MEMORY);
     ic.setLastModifiedDate(g.getTimestamp());
     File tmpFile = new File(g.dayName + ".tmp.odb");
     tmpFile.delete();
     ic.setRegionName(Algorithms.capitalizeFirstLetterAndLowercase(g.dayName));
     ic.setNodesDBFile(tmpFile);
     log.info("Processing " + g.dayName + " " + g.osmGzFiles.size() + " files");
     ic.generateIndexes(
         g.getSortedFiles(),
         new ConsoleProgressImplementation(),
         null,
         MapZooms.parseZooms("13-14;15-"),
         new MapRenderingTypesEncoder(country.getName()),
         log,
         false);
     File targetFile = new File(country, ic.getMapFileName());
     targetFile.setLastModified(g.getTimestamp());
     FileInputStream fis = new FileInputStream(targetFile);
     GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(obf));
     Algorithms.streamCopy(fis, gzout);
     fis.close();
     gzout.close();
     obf.setLastModified(g.getTimestamp());
     targetFile.delete();
   }
 }
  public void writeBinaryTransportTree(
      rtree.Node parent,
      RTree r,
      BinaryMapIndexWriter writer,
      PreparedStatement selectTransportStop,
      PreparedStatement selectTransportRouteStop,
      Map<Long, Long> transportRoutes,
      Map<String, Integer> stringTable)
      throws IOException, RTreeException, SQLException {
    Element[] e = parent.getAllElements();
    List<Long> routes = null;
    for (int i = 0; i < parent.getTotalElements(); i++) {
      Rect re = e[i].getRect();
      if (e[i].getElementType() == rtree.Node.LEAF_NODE) {
        long id = ((LeafElement) e[i]).getPtr();
        selectTransportStop.setLong(1, id);
        selectTransportRouteStop.setLong(1, id);
        ResultSet rs = selectTransportStop.executeQuery();
        if (rs.next()) {
          int x24 = (int) MapUtils.getTileNumberX(24, rs.getDouble(3));
          int y24 = (int) MapUtils.getTileNumberY(24, rs.getDouble(2));
          String name = rs.getString(4);
          String nameEn = rs.getString(5);
          if (nameEn != null && nameEn.equals(Junidecode.unidecode(name))) {
            nameEn = null;
          }
          ResultSet rset = selectTransportRouteStop.executeQuery();
          if (routes == null) {
            routes = new ArrayList<Long>();
          } else {
            routes.clear();
          }
          while (rset.next()) {
            Long route = transportRoutes.get(rset.getLong(1));
            if (route == null) {
              log.error(
                  "Something goes wrong with transport route id = "
                      + rset.getLong(1)); // $NON-NLS-1$
            } else {
              routes.add(route);
            }
          }
          rset.close();
          writer.writeTransportStop(id, x24, y24, name, nameEn, stringTable, routes);
        } else {
          log.error("Something goes wrong with transport id = " + id); // $NON-NLS-1$
        }
      } else {
        long ptr = ((NonLeafElement) e[i]).getPtr();
        rtree.Node ns = r.getReadNode(ptr);

        writer.startTransportTreeElement(re.getMinX(), re.getMaxX(), re.getMinY(), re.getMaxY());
        writeBinaryTransportTree(
            ns,
            r,
            writer,
            selectTransportStop,
            selectTransportRouteStop,
            transportRoutes,
            stringTable);
        writer.endWriteTransportTreeElement();
      }
    }
  }
  public void writeBinaryTransportIndex(
      BinaryMapIndexWriter writer, String regionName, Connection mapConnection)
      throws IOException, SQLException {
    try {
      closePreparedStatements(transRouteStat, transRouteStopsStat, transStopsStat);
      mapConnection.commit();
      transportStopsTree.flush();

      visitedStops = null; // allow gc to collect it
      PreparedStatement selectTransportRouteData =
          mapConnection.prepareStatement(
              "SELECT id, dist, name, name_en, ref, operator, type FROM transport_route"); //$NON-NLS-1$
      PreparedStatement selectTransportData =
          mapConnection.prepareStatement(
              "SELECT S.stop, S.direction,"
                  + //$NON-NLS-1$
                  "  A.latitude,  A.longitude, A.name, A.name_en "
                  + //$NON-NLS-1$
                  "FROM transport_route_stop S INNER JOIN transport_stop A ON A.id = S.stop WHERE S.route = ? ORDER BY S.ord asc"); //$NON-NLS-1$

      writer.startWriteTransportIndex(regionName);

      writer.startWriteTransportRoutes();

      // expect that memory would be enough
      Map<String, Integer> stringTable = createStringTableForTransport();
      Map<Long, Long> transportRoutes = new LinkedHashMap<Long, Long>();

      ResultSet rs = selectTransportRouteData.executeQuery();
      List<TransportStop> directStops = new ArrayList<TransportStop>();
      List<TransportStop> reverseStops = new ArrayList<TransportStop>();
      while (rs.next()) {

        long idRoute = rs.getLong(1);
        int dist = rs.getInt(2);
        String routeName = rs.getString(3);
        String routeEnName = rs.getString(4);
        if (routeEnName != null && routeEnName.equals(Junidecode.unidecode(routeName))) {
          routeEnName = null;
        }
        String ref = rs.getString(5);
        String operator = rs.getString(6);
        String type = rs.getString(7);

        selectTransportData.setLong(1, idRoute);
        ResultSet rset = selectTransportData.executeQuery();
        reverseStops.clear();
        directStops.clear();
        while (rset.next()) {
          boolean dir = rset.getInt(2) != 0;
          long idStop = rset.getInt(1);
          String stopName = rset.getString(5);
          String stopEnName = rset.getString(6);
          if (stopEnName != null && stopEnName.equals(Junidecode.unidecode(stopName))) {
            stopEnName = null;
          }
          TransportStop st = new TransportStop();
          st.setId(idStop);
          st.setName(stopName);
          st.setLocation(rset.getDouble(3), rset.getDouble(4));
          if (stopEnName != null) {
            st.setEnName(stopEnName);
          }
          if (dir) {
            directStops.add(st);
          } else {
            reverseStops.add(st);
          }
        }
        writer.writeTransportRoute(
            idRoute,
            routeName,
            routeEnName,
            ref,
            operator,
            type,
            dist,
            directStops,
            reverseStops,
            stringTable,
            transportRoutes);
      }
      rs.close();
      selectTransportRouteData.close();
      selectTransportData.close();
      writer.endWriteTransportRoutes();

      PreparedStatement selectTransportStop =
          mapConnection.prepareStatement(
              "SELECT A.id,  A.latitude,  A.longitude, A.name, A.name_en FROM transport_stop A where A.id = ?"); //$NON-NLS-1$
      PreparedStatement selectTransportRouteStop =
          mapConnection.prepareStatement(
              "SELECT DISTINCT S.route FROM transport_route_stop S WHERE S.stop = ? "); //$NON-NLS-1$
      long rootIndex = transportStopsTree.getFileHdr().getRootIndex();
      rtree.Node root = transportStopsTree.getReadNode(rootIndex);
      Rect rootBounds = calcBounds(root);
      if (rootBounds != null) {
        writer.startTransportTreeElement(
            rootBounds.getMinX(), rootBounds.getMaxX(), rootBounds.getMinY(), rootBounds.getMaxY());
        writeBinaryTransportTree(
            root,
            transportStopsTree,
            writer,
            selectTransportStop,
            selectTransportRouteStop,
            transportRoutes,
            stringTable);
        writer.endWriteTransportTreeElement();
      }
      selectTransportStop.close();
      selectTransportRouteStop.close();

      writer.writeTransportStringTable(stringTable);

      writer.endWriteTransportIndex();
      writer.flush();
    } catch (RTreeException e) {
      throw new IllegalStateException(e);
    }
  }