private Rect calcBounds(rtree.Node n) {
   Rect r = null;
   Element[] e = n.getAllElements();
   for (int i = 0; i < n.getTotalElements(); i++) {
     Rect re = e[i].getRect();
     if (r == null) {
       try {
         r = new Rect(re.getMinX(), re.getMinY(), re.getMaxX(), re.getMaxY());
       } catch (IllegalValueException ex) {
       }
     } else {
       r.expandToInclude(re);
     }
   }
   return r;
 }
  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);
    }
  }