All Downloads are FREE. Search and download functionalities are using the official Maven repository.

geotrellis.feature.MultiPolygon.scala Maven / Gradle / Ivy

The newest version!
package geotrellis.feature

import geotrellis._

import com.vividsolutions.jts.{ geom => jts }

class MultiPolygon[D](override val geom:jts.MultiPolygon, data:D) extends GeometryCollection(geom, data) {

  def flatten:List[Polygon[D]] =
    (0 until geom.getNumGeometries).map(
      i => new JtsPolygon(geom.getGeometryN(i).asInstanceOf[jts.Polygon],data)).toList

}

/// MultiPolygon implementation
object MultiPolygon {

  val factory = Feature.factory

  /**
   * Create a an multipolygon feature.
   */
  def empty(): MultiPolygon[_] = 
    JtsMultiPolygon(factory.createMultiPolygon(Array[jts.Polygon]()), None)

  /**
   * Create a an multipolygon feature with data.
   *
   * @param data  Data of this feature.
   */
  def empty[D](data: D): MultiPolygon[D] = 
    JtsMultiPolygon(factory.createMultiPolygon(Array[jts.Polygon]()), data)

  /**
   * Create a multipolygon feature from a JTS MultiPolygon instance.
   *
   * @param g     JTS MultiPolygon instance
   * @param data  Data of this feature.
   */
  def apply[D](g: jts.MultiPolygon, data: D): MultiPolygon[D] = 
    JtsMultiPolygon(g, data)

  def apply[D](g: jts.Geometry, data: D): MultiPolygon[D] =
    JtsMultiPolygon(g.asInstanceOf[jts.MultiPolygon], data)

  /**
   * Create a MultiPolygon using four nested lists.
   *
   * The first list represents each polygon.
   *
   * The second list represents each ring of the polygon, the first being
   * the exterior ring.
   *
   * The third list is a list of coordinates in the ring.
   *
   * The fourth list represents a single coordinate, which is two double values.
   *
   * @param coords  Nested sequence of polygon coordinates
   * @param data    The data of this feature
   */
  def apply[D](coords: Seq[Seq[Seq[Seq[Double]]]], data: D)(implicit dummy: DI, dummy2: DI): MultiPolygon[D] = {
    val polygons = coords.map(
      polygonCoords => {
        Polygon.createJtsPolygonFromSeqs(polygonCoords.head, polygonCoords.tail)
      }).toArray
    MultiPolygon(factory.createMultiPolygon(polygons), data)
  }

}

case class JtsMultiPolygon[D](g: jts.MultiPolygon, d: D) extends MultiPolygon(g,d)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy