Saltar al contenido

Agregar procesamiento geoespacial avanzado a Hibernate

Ahora que las ubicaciones están disponibles en pantalla optimizada, deberá actualizar la aplicación para usar los nuevos datos.

Abre el archivo IncidentRepository.java desde el directorio src/main/java/com/sap/hana/hibernate/sample/repositories.

En cada pregunta, reemplace la columna i.location (el lugar en SRS 4326) con i.mapLocation (el lugar en SRS 7131).

Además, el sitio de entrada que todavía se muestra en SRS 4326 debe convertirse a SRS 7131 antes de que se realicen los cálculos geoespaciales. Con este fin, Spatial Hibernate proporciona una transform una función que se puede utilizar para cambiar la geometría de un SRS a otro. Para usar la función, agregue todas las apariciones de la misma :location le transform(:location, 7131).

Antes

query = this.em.createQuery(
    "select i from Incident i "
        + "where i.category in :category "
        + "  and i.date between :dateFrom and :dateTo "
        + "  and dwithin(i.location, :location, :distance) = true "
        + "order by i.date desc",
    Incident.class );

Después

query = this.em.createQuery(
    "select i from Incident i "
        + "where i.category in :category "
        + "  and i.date between :dateFrom and :dateTo "
        + "  and dwithin(i.mapLocation, transform(:location, 7131), :distance) = true "
        + "order by i.date desc",
    Incident.class );

Realice estas adaptaciones a cada una de las 4 preguntas del archivo.

public Page<Incident> findByLocationNear(Point<G2D> location, Distance distance, Date dateFrom, Date dateTo,
    List<String> category, Pageable pageable) {
  TypedQuery<Incident> query;
  if ( category == null || category.isEmpty() ) {
    query = this.em.createQuery(
        "select i from Incident i "
            + "where i.date between :dateFrom and :dateTo "
            + "  and dwithin(i.mapLocation, transform(:location, 7131), :distance) = true "
            + "order by i.date desc",
        Incident.class );
  }
  else {
    query = this.em.createQuery(
        "select i from Incident i "
            + "where i.category in :category "
            + "  and i.date between :dateFrom and :dateTo "
            + "  and dwithin(i.mapLocation, transform(:location, 7131), :distance) = true "
            + "order by i.date desc",
        Incident.class );
    query.setParameter( "category", category );
  }

  query.setParameter( "dateFrom", dateFrom );
  query.setParameter( "dateTo", dateTo );
  query.setParameter( "location", location );
  query.setParameter( "distance", distance );

  query.setFirstResult( (int) pageable.getOffset() );
  query.setMaxResults( pageable.getPageSize() );

  Query countQuery;
  if ( category == null || category.isEmpty() ) {
    countQuery = this.em.createQuery(
        "select count(i) from Incident i "
            + "where i.date between :dateFrom and :dateTo "
            + "  and dwithin(i.mapLocation, transform(:location, 7131), :distance) = true " );

  }
  else {
    countQuery = this.em.createQuery(
        "select count(i) from Incident i "
            + "where i.category in :category "
            + "  and i.date between :dateFrom and :dateTo "
            + "  and dwithin(i.mapLocation, transform(:location, 7131), :distance) = true " );
    countQuery.setParameter( "category", category );
  }

  countQuery.setParameter( "dateFrom", dateFrom );
  countQuery.setParameter( "dateTo", dateTo );
  countQuery.setParameter( "location", location );
  countQuery.setParameter( "distance", distance );

  long count = ( (Long) countQuery.getSingleResult() ).longValue();

  return new PageImpl<>( query.getResultList(), pageable, count );
}

Salva el IncidentRepository.java expediente.

Hecho

Inicie sesión para responder la pregunta