
geotrellis.vector.io.WKB.WKB.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geotrellis-vector_2.11 Show documentation
Show all versions of geotrellis-vector_2.11 Show documentation
GeoTrellis is an open source geographic data processing engine for high performance applications.
/*
* Copyright (c) 2014 Azavea.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geotrellis.vector.io.wkb
import com.vividsolutions.jts.io.WKBReader
import com.vividsolutions.jts.{geom => jts}
import geotrellis.vector._
/** A thread-safe wrapper for the [https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary WKB]
* Writer and Reader
*/
object WKB {
private val readerBox = new ThreadLocal[WKBReader]
private val writerBox = new ThreadLocal[WKBWriter]
/** Convert Well Known Binary to Geometry */
def read(value: Array[Byte]): Geometry = {
if (readerBox.get == null) readerBox.set(new WKBReader(GeomFactory.factory))
Geometry(readerBox.get.read(value))
}
/** Convert Well Known Binary to Geometry */
def read(hex: String): Geometry = {
if (readerBox.get == null) readerBox.set(new WKBReader(GeomFactory.factory))
Geometry(readerBox.get.read(WKBReader.hexToBytes(hex)))
}
/** Convert Geometry to Well Known Binary */
def write(geom: Geometry, srid: Int = 0): Array[Byte] = {
if (writerBox.get == null) writerBox.set(new WKBWriter(2))
if (srid == 0)
writerBox.get.write(geom)
else
writerBox.get.write(geom, Some(srid))
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy