Skip to content

Commit

Permalink
Changed normalized flag name, optimized the code, removed unused code…
Browse files Browse the repository at this point in the history
…, added docstring and parameter into VectorTile constructor functions.
  • Loading branch information
pomadchin committed Apr 26, 2019
1 parent 2ed7bf3 commit 38b70c8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
30 changes: 22 additions & 8 deletions vectortile/src/main/scala/geotrellis/vectortile/Layer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,16 @@ import scala.collection.mutable.ListBuffer
).flatten
}

/** Encode this ProtobufLayer a mid-level Layer ready to be encoded as protobuf bytes. */
private[vectortile] def toProtobuf(normalize: Boolean = true): PBLayer = {
/**
* Encode this ProtobufLayer a mid-level Layer ready to be encoded as protobuf bytes.
* @param forceWindClockwise is a parameter to force orient all Polygons and MultiPolygons
* clockwise, since it's a MapBox spec requirement:
* Any polygon interior ring must be oriented with the winding order opposite that of their
* parent exterior ring and all interior rings must directly follow the exterior ring to which they belong.
* Exterior rings must be oriented clockwise and interior rings must be oriented counter-clockwise (when viewed in screen coordinates).
* See https://docs.mapbox.com/vector-tiles/specification/#winding-order for mor details.
**/
private[vectortile] def toProtobuf(forceWindClockwise: Boolean = true): PBLayer = {
val pgp = implicitly[ProtobufGeom[Point, MultiPoint]]
val pgl = implicitly[ProtobufGeom[Line, MultiLine]]
val pgy = implicitly[ProtobufGeom[Polygon, MultiPolygon]]
Expand All @@ -102,12 +110,18 @@ import scala.collection.mutable.ListBuffer
* points.map(f => unfeature(keys, values, f))
*/
val features = Seq(
points.map(f => unfeature(keyMap, valMap, POINT, pgp.toCommands(Left(f.geom.normalize(normalize)), tileExtent.northWest, resolution), f.data)),
multiPoints.map(f => unfeature(keyMap, valMap, POINT, pgp.toCommands(Right(f.geom.normalize(normalize)), tileExtent.northWest, resolution), f.data)),
lines.map(f => unfeature(keyMap, valMap, LINESTRING, pgl.toCommands(Left(f.geom.normalize(normalize)), tileExtent.northWest, resolution), f.data)),
multiLines.map(f => unfeature(keyMap, valMap, LINESTRING, pgl.toCommands(Right(f.geom.normalize(normalize)), tileExtent.northWest, resolution), f.data)),
polygons.map(f => unfeature(keyMap, valMap, POLYGON, pgy.toCommands(Left(f.geom.normalize(normalize)), tileExtent.northWest, resolution), f.data)),
multiPolygons.map(f => unfeature(keyMap, valMap, POLYGON, pgy.toCommands(Right(f.geom.normalize(normalize)), tileExtent.northWest, resolution), f.data))
points.map(f => unfeature(keyMap, valMap, POINT, pgp.toCommands(Left(f.geom), tileExtent.northWest, resolution), f.data)),
multiPoints.map(f => unfeature(keyMap, valMap, POINT, pgp.toCommands(Right(f.geom), tileExtent.northWest, resolution), f.data)),
lines.map(f => unfeature(keyMap, valMap, LINESTRING, pgl.toCommands(Left(f.geom), tileExtent.northWest, resolution), f.data)),
multiLines.map(f => unfeature(keyMap, valMap, LINESTRING, pgl.toCommands(Right(f.geom), tileExtent.northWest, resolution), f.data)),
polygons.map { f =>
val geom = if(forceWindClockwise) f.geom.normalized else f.geom
unfeature(keyMap, valMap, POLYGON, pgy.toCommands(Left(geom), tileExtent.northWest, resolution), f.data)
},
multiPolygons.map { f =>
val geom = if(forceWindClockwise) f.geom.normalized else f.geom
unfeature(keyMap, valMap, POLYGON, pgy.toCommands(Right(geom), tileExtent.northWest, resolution), f.data)
}
).flatten

PBLayer(version, name, features, keys, values.map(_.toProtobuf), Some(tileWidth))
Expand Down
16 changes: 11 additions & 5 deletions vectortile/src/main/scala/geotrellis/vectortile/VectorTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ import geotrellis.util.annotations.experimental
* @constructor This is not meant to be called directly - see this class's
* companion object for the available helper methods.
*/
@experimental case class VectorTile(layers: Map[String, Layer], tileExtent: Extent, normalize: Boolean = true) {
@experimental case class VectorTile(layers: Map[String, Layer], tileExtent: Extent, forceWindClockwise: Boolean = true) {
/** Encode this VectorTile back into a mid-level Protobuf object. */
private def toProtobuf: PBTile = PBTile(layers = layers.values.map(_.toProtobuf(normalize)).toSeq)
private def toProtobuf: PBTile = PBTile(layers = layers.values.map(_.toProtobuf(forceWindClockwise)).toSeq)

/** Encode this VectorTile back into its original form of Protobuf bytes. */
def toBytes: Array[Byte] = toProtobuf.toByteArray
Expand All @@ -74,7 +74,7 @@ ${layers.values.map(_.pretty).mkString}

@experimental object VectorTile {
/** Create a VectorTile from a low-level protobuf Tile type. */
private def fromPBTile(tile: PBTile, tileExtent: Extent): VectorTile = {
private def fromPBTile(tile: PBTile, tileExtent: Extent, forceWindClockwise: Boolean = true): VectorTile = {

val layers: Map[String, Layer] = tile.layers.map({ l =>
val pbl = LazyLayer(l, tileExtent)
Expand All @@ -89,8 +89,14 @@ ${layers.values.map(_.pretty).mkString}
*
* @param bytes Raw Protobuf bytes from a `.mvt` file or otherwise.
* @param tileExtent The [[Extent]] of this tile, '''not''' the global extent.
* @param forceWindClockwise is a parameter to force orient all Polygons and MultiPolygons
* clockwise, since it's a MapBox spec requirement:
* Any polygon interior ring must be oriented with the winding order opposite that of their
* parent exterior ring and all interior rings must directly follow the exterior ring to which they belong.
* Exterior rings must be oriented clockwise and interior rings must be oriented counter-clockwise (when viewed in screen coordinates).
* See https://docs.mapbox.com/vector-tiles/specification/#winding-order for mor details.
*/
def fromBytes(bytes: Array[Byte], tileExtent: Extent): VectorTile =
fromPBTile(PBTile.parseFrom(bytes), tileExtent)
def fromBytes(bytes: Array[Byte], tileExtent: Extent, forceWindClockwise: Boolean = true): VectorTile =
fromPBTile(PBTile.parseFrom(bytes), tileExtent, forceWindClockwise)

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,4 @@ package geotrellis
* @version 2.1
*/

import geotrellis.vector.Geometry

package object vectortile {
implicit class withGeometryMethods[G <: Geometry](val self: G) extends AnyVal {
private[vectortile] def normalize(enabled: Boolean = true): G = { if(enabled) { self.jtsGeom.normalize() }; self }
}
}
package object vectortile { }

0 comments on commit 38b70c8

Please sign in to comment.