Skip to content

Commit

Permalink
Add a safe WKTParser.parse function (#3286)
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin committed Aug 17, 2020
1 parent c4ab0d9 commit bea43a7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update GDAL up to 3.1 [#3279](https://github.com/locationtech/geotrellis/pull/3279)
- Fix GeoTiff writer does not currently support WebMercator projection with no EPSG code set [#3281](https://github.com/locationtech/geotrellis/issues/3281)
- Fix Tile combine should union cellTypes [#3284](https://github.com/locationtech/geotrellis/pull/3284)
- Fix CRS.fromWKT function throws [#3209](https://github.com/locationtech/geotrellis/issues/3209)

## [3.4.1] - 2020-07-16

Expand Down
2 changes: 1 addition & 1 deletion proj4/src/main/scala/geotrellis/proj4/CRS.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object CRS {
def fromWKT(wktString: String): Option[CRS] = {
val fromEpsgCode = WKT.getEpsgStringCode(wktString).map(fromName)
if(fromEpsgCode.isEmpty) {
WKTParser(wktString) match {
WKTParser.parse(wktString).toOption.flatMap {
case wkt: ProjCS =>
wkt.extension.flatMap {
case ExtensionProj4(proj4String) =>
Expand Down
27 changes: 14 additions & 13 deletions proj4/src/main/scala/geotrellis/proj4/io/wkt/WKT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ object WKT {
* @return
*/
def getEpsgCode(wktString: String): Option[Int] = {
val wktParsed = WKTParser(wktString)
val db = parsed.find { case (_, wkt) => wkt == wktParsed }.map(_._1)
if(db.nonEmpty) db
else
wktParsed match {
case wkt: ProjCS =>
wkt.extension.flatMap {
case ExtensionProj4(proj4String) =>
CRS.fromString(proj4String).epsgCode
case _ => None
}
case _ => None
}
WKTParser.parse(wktString).toOption.flatMap { wktParsed =>
val db = parsed.find { case (_, wkt) => wkt == wktParsed }.map(_._1)
if (db.nonEmpty) db
else
wktParsed match {
case wkt: ProjCS =>
wkt.extension.flatMap {
case ExtensionProj4(proj4String) =>
CRS.fromString(proj4String).epsgCode
case _ => None
}
case _ => None
}
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions proj4/src/main/scala/geotrellis/proj4/io/wkt/WKTParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package geotrellis.proj4.io.wkt

import scala.util.Try
import scala.util.parsing.combinator.RegexParsers

object WKTParser extends RegexParsers {
Expand Down Expand Up @@ -121,7 +122,7 @@ object WKTParser extends RegexParsers {

def wktCS: Parser[WktCS] = localcs | projcs | geogcs | geoccs | compdcs | vertcs

def apply(wktString: String) : WktCS = {
def apply(wktString: String): WktCS = {
val cleanWkt = wktString.replaceAll("\n", "")
parseAll(wktCS, cleanWkt) match {
case Success(wktObject, _) =>
Expand All @@ -134,4 +135,5 @@ object WKTParser extends RegexParsers {
}
}

}
def parse(wktString: String): Try[WktCS] = Try(apply(wktString))
}
4 changes: 4 additions & 0 deletions proj4/src/test/scala/geotrellis/proj4/CRSTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ class CRSTest extends AnyFunSpec with Inspectors {
assert(!str.contains("$") && !str.contains("@"))
}
}

it("CRS.fromWKT should not throw on an incorrect input") {
assert(CRS.fromWKT("") == Option.empty[CRS])
}
}

0 comments on commit bea43a7

Please sign in to comment.