Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a safe WKTParser.parse function #3286

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
}