4

I am dealing with geospatial queries on a database. The query returns all data sets within an arbitrary bounding box, given a point associated with the data set is within this bounding box.

Since the bounding boxes provided have very high precision, I was not able to apply caching mechanisms based on the bounding boxes latitude and longitude values. In order to be able to cache queries, I thought it would be a smart idea to normalize bounding boxes before querying the database.

My initial idea was to simply truncate lat/lon decimal places, but this approach doesn't allow for configuration of the precision.

Later, I found JTS GeometrySnapper which allows to snap to arbitrary geometries.

I would love to create a geometry that allows me to snap to a grid comprising of lines every n meters both in latitude and longitude. How do I construct such a geometry?

This is my approach so far:

  private def sanitizeBoundingBox(boundaries: BoundingBox): Geometry = {
    val precisionModel: PrecisionModel = new PrecisionModel()
    val geometryFactory: GeometryFactory = new GeometryFactory(precisionModel, 4326)
    val box: Geometry = geometryFactory.toGeometry(new Envelope(boundaries.minLongitude, boundaries.maxLongitude, boundaries.minLatitude, boundaries.maxLatitude))
    val snapper = new GeometrySnapper(box)
    snapper.snapTo(???, ???)
  }
6

For this scenario, I would suggest using the JTS GeometryPrecisionReducer class rather than GeometrySnapper. GeometrySnapper is intended for snapping one geometry to another. If you wanted to use GeometrySnapper, you could generate a fishnet grid at your desired precision level to snap to. But GeometryPrecisionReducer provides the same result without needing to generate the grid.

Also, just an observation on the argument you're using for the PrecisionModel constructor (5). Just making sure you're aware that that is not equivalent to number of decimals, but rather scale. From the PrecisionModel class documentation:

  • For example, to specify 3 decimal places of precision, use a scale factor of 1000. To specify -3 decimal places of precision (i.e. rounding to the nearest 1000), use a scale factor of 0.001.
  • Makes much more sense. Thank you! And yes: I was very much mistaken about the PrecisionModel constructor (I did adapt the example to make it less confusing). – mritz_p Mar 7 '16 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.