Exemplo n.º 1
0
  /**
   * Create a new graph, with linestrings read from the database (optionally using only a buffer
   * around a given track for the network)
   *
   * @param track GPS track consisting of a list of data points
   * @param bufferSize size of the buffer to select around the track
   * @param dumpFile if not empty, the path of a shapefile to dump the network into.
   */
  public void addLineStringsFromDatabase(
      ArrayList<Point> track, float bufferSize, String dumpFile) {
    setLineMergeGraphH4cked(new LineMergeGraphH4cked());
    distancesCalculated = false;

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    // Query gpsResults = session.createQuery("from" );

    if (track == null) {
      Query result = session.createQuery("from OSMEdge");

      @SuppressWarnings("unchecked")
      Iterator<OSMEdge> iter = result.iterate();
      while (iter.hasNext()) {
        OSMEdge o = iter.next();
        LineString g = o.getGeometry();
        addLineString(g, o.getId(), o.getEnvtype(), o.getCyktype(), o.getGroenm());
        // let us add this thing to the spatial index
        addOSMEdgeToSpatialIndex(o);
      }
    } else {
      // let us join the nodes of the track to a linestring ....

      Iterator<Point> trackIter = track.iterator();
      Coordinate[] coords = new Coordinate[track.size()];
      int i = 0;
      while (trackIter.hasNext()) {
        Point p = trackIter.next();
        coords[i] = p.getCoordinate();
        i++;
      }
      if (i > 1) {
        LineString l = fact.createLineString(coords);

        Criteria testCriteria = session.createCriteria(OSMEdge.class);
        if (bufferSize > 0.) { // ... build a buffer ...
          Geometry buffer = l.buffer(bufferSize);
          //				testCriteria.add(SpatialRestrictions.within("geometry", buffer));
          testCriteria.add(SpatialRestrictions.intersects("geometry", buffer));
        }
        @SuppressWarnings("unchecked")
        List<OSMEdge> result = testCriteria.list();

        logger.info("Spatial query selected " + result.size() + " edges");

        Iterator<OSMEdge> iter = result.iterator();
        i = 0;
        while (iter.hasNext()) {
          i++;
          OSMEdge o = iter.next();
          LineString g = o.getGeometry();
          addLineString(g, o.getId(), o.getEnvtype(), o.getCyktype(), o.getGroenm());
          // let us add this thing to the spatial index
          addOSMEdgeToSpatialIndex(o);
        }

        // if required, dump the graph to a shapefile:
        if (dumpFile != "") {
          try {
            this.dumpBuffer(result, dumpFile);
            logger.info("buffer dumped");
          } catch (Exception e) {
            logger.error("error dumping buffer: " + e);
          }
        }
      } else {
        logger.error("error: track has <2 coordinates");
      }
    }

    session.disconnect();
  }