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

Added WKT Parser to improve WKT.getEpsgCode #1931

Merged
merged 2 commits into from
Dec 21, 2016
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 proj4/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name := "geotrellis-proj4"

libraryDependencies ++= Seq(
openCSV,
parserCombinators,
scalatest % "test",
scalacheck % "test")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1754,8 +1754,6 @@
26972=PROJCS["NAD83 / Illinois West", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4269"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", -90.16666666666667], PARAMETER["latitude_of_origin", 36.666666666666664], PARAMETER["scale_factor", 0.999941177], PARAMETER["false_easting", 700000.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","26972"]]
26971=PROJCS["NAD83 / Illinois East", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4269"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", -88.33333333333333], PARAMETER["latitude_of_origin", 36.666666666666664], PARAMETER["scale_factor", 0.999975], PARAMETER["false_easting", 300000.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","26971"]]
26970=PROJCS["NAD83 / Idaho West", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4269"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", -115.75], PARAMETER["latitude_of_origin", 41.666666666666664], PARAMETER["scale_factor", 0.999933333], PARAMETER["false_easting", 800000.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","26970"]]
TEST2=GEOGCS["WGS 84", DATUM["World Geodetic System 1984", SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Longitude", EAST], AXIS["Latitude", NORTH], AUTHORITY["EPSG","TEST2"]]
TEST1=GEOGCS["ED50", DATUM["European Datum 1950", SPHEROID["International 1924", 6378388.0, 297.0, AUTHORITY["EPSG","7022"]], TOWGS84[-116.641, -56.931, -110.559, 0.893, 0.921, -0.917, -3.52]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Longitude", EAST], AXIS["Latitude", NORTH], AUTHORITY["EPSG","TEST1"]]
26969=PROJCS["NAD83 / Idaho Central", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4269"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", -114.0], PARAMETER["latitude_of_origin", 41.666666666666664], PARAMETER["scale_factor", 0.999947368], PARAMETER["false_easting", 500000.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","26969"]]
26968=PROJCS["NAD83 / Idaho East", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4269"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", -112.16666666666667], PARAMETER["latitude_of_origin", 41.666666666666664], PARAMETER["scale_factor", 0.999947368], PARAMETER["false_easting", 200000.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","26968"]]
26967=PROJCS["NAD83 / Georgia West", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4269"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", -84.16666666666667], PARAMETER["latitude_of_origin", 30.0], PARAMETER["scale_factor", 0.9999], PARAMETER["false_easting", 700000.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","26967"]]
Expand Down Expand Up @@ -5588,8 +5586,6 @@ TEST1=GEOGCS["ED50", DATUM["European Datum 1950", SPHEROID["International 1924",
3601=PROJCS["NAD83(NSRS2007) / Missouri Central", GEOGCS["NAD83(NSRS2007)", DATUM["NAD83 (National Spatial Reference System 2007)", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6759"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4759"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", -92.5], PARAMETER["latitude_of_origin", 35.833333333333336], PARAMETER["scale_factor", 0.999933333], PARAMETER["false_easting", 500000.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","3601"]]
3600=PROJCS["NAD83(NSRS2007) / Mississippi West (ftUS)", GEOGCS["NAD83(NSRS2007)", DATUM["NAD83 (National Spatial Reference System 2007)", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6759"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4759"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", -90.33333333333333], PARAMETER["latitude_of_origin", 29.5], PARAMETER["scale_factor", 0.99995], PARAMETER["false_easting", 2296583.333], PARAMETER["false_northing", 0.0], UNIT["foot_survey_us", 0.30480060960121924], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","3600"]]
62836405=GEOGCS["GDA94 (deg)", DATUM["Geocentric Datum of Australia 1994", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6283"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","62836405"]]
#Extra Definitions Supplied from Community
#Tue Jan 13 07:44:38 MST 2015
23433=PROJCS["Garoua / UTM zone 33N",GEOGCS["Garoua",DATUM["Garoua",SPHEROID["Clarke 1880 (IGN)",6378249.2,293.4660212936269,AUTHORITY["EPSG","7011"]],AUTHORITY["EPSG","6234"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9108"]],AUTHORITY["EPSG","4234"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",15],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","23433"]]
22832=PROJCS["Douala / UTM zone 32N",GEOGCS["Douala",DATUM["Douala",SPHEROID["Clarke 1880 (IGN)",6378249.2,293.4660212936269,AUTHORITY["EPSG","7011"]],AUTHORITY["EPSG","6228"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9108"]],AUTHORITY["EPSG","4228"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",9],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","22832"]]
2982=PROJCS["IGN72 Grand Terre / UTM zone 58S",GEOGCS["IGN72 Grand Terre",DATUM["IGN72_Grande_Terre",SPHEROID["International 1924",6378388,297,AUTHORITY["EPSG","7022"]],AUTHORITY["EPSG","6634"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9108"]],AUTHORITY["EPSG","4634"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",165],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","2982"]]
Expand All @@ -5605,7 +5601,7 @@ TEST1=GEOGCS["ED50", DATUM["European Datum 1950", SPHEROID["International 1924",
42304=PROJCS["NAD83 / NRCan LCC Canada",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS_1980",6378137,298.257222101],TOWGS84[0,0,0]],PRIMEM["Greenwich",0],UNIT["Decimal_Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["central_meridian",-95.0],PARAMETER["latitude_of_origin",49.0],PARAMETER["standard_parallel_1",49.0],PARAMETER["standard_parallel_2",77.0],PARAMETER["false_easting",0.0],PARAMETER["false_northing",0.0],UNIT["Meter",1],AUTHORITY["EPSG","42304"]]
42303=PROJCS["NAD83 / Albers NorthAm",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS_1980",6378137,298.257222101],TOWGS84[0,0,0]],PRIMEM["Greenwich",0],UNIT["Decimal_Degree",0.0174532925199433]],PROJECTION["Albers_conic_equal_area"],PARAMETER["central_meridian",-96.0],PARAMETER["latitude_of_origin",23],PARAMETER["standard_parallel_1",29.5],PARAMETER["standard_parallel_2",45.5],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1],AUTHORITY["EPSG","42303"]]
42302=PROJCS["JapanOrtho.09 09",GEOGCS["Lon/Lat.Tokyo Datum",DATUM["Tokyo Datum",SPHEROID["anon",6377397.155,299.15281310608]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["Central_Meridian",139.833333333333],PARAMETER["False_Easting",0],PARAMETER["False_Northing",0],PARAMETER["Latitude_of_Origin",36],PARAMETER["Scale_Factor",0.9999],UNIT["Meter",1],AUTHORITY["EPSG","42302"]]
42301=PROJCS["NAD27 / Polar Stereographic / CM\=-98",GEOGCS["NAD27",DATUM["North_American_Datum_1927",SPHEROID["Clarke 1866",6378206.4,294.978698213901],TOWGS84[-9,151,185]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Stereographic"],PARAMETER["latitude_of_origin",90],PARAMETER["central_meridian",-98.0],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1],AUTHORITY["EPSG","42301"]]
42301=PROJCS["NAD27 / Polar Stereographic / CM=-98",GEOGCS["NAD27",DATUM["North_American_Datum_1927",SPHEROID["Clarke 1866",6378206.4,294.978698213901],TOWGS84[-9,151,185]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Stereographic"],PARAMETER["latitude_of_origin",90],PARAMETER["central_meridian",-98.0],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1],AUTHORITY["EPSG","42301"]]
22700=PROJCS["Deir ez Zor / Levant Zone",GEOGCS["Deir ez Zor",DATUM["Deir_ez_Zor",SPHEROID["Clarke 1880 (IGN)",6378249.2,293.4660212936269,AUTHORITY["EPSG","7011"]],AUTHORITY["EPSG","6227"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4227"]],PROJECTION["Lambert_Conformal_Conic_1SP"],PARAMETER["latitude_of_origin",34.65],PARAMETER["central_meridian",37.35],PARAMETER["scale_factor",0.9996256],PARAMETER["false_easting",300000],PARAMETER["false_northing",300000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","22700"]]
104305=GEOGCS["GCS_Voirol_Unifie_1960_Degree",DATUM["D_Voirol_Unifie_1960",SPHEROID["Clarke_1880_RGS",6378249.145,293.465]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295],AUTHORITY["ESRI","104305"]]
104304=GEOGCS["GCS_Voirol_1875_Degree",DATUM["D_Voirol_1875",SPHEROID["Clarke_1880_IGN",6378249.2,293.46602]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295],AUTHORITY["ESRI","104304"]]
Expand Down
77 changes: 38 additions & 39 deletions proj4/src/main/scala/geotrellis/proj4/io/wkt/WKT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,59 @@ import org.osgeo.proj4j.NotFoundException

import scala.io.Source

/**
* @author Manuri Perera
*/
object WKT {
private lazy val epsgcodetowktmap = new Memoize[String, String](retrieveWKTStringFromFile)
private lazy val wkttoepsgcodemap = new Memoize[String, String](retrieveEpsgCodeFromFile)
private val wktResourcePath = "/geotrellis/proj4/wkt/epsg.properties"
lazy val parsed: Map[Int, WktCS] = records.mapValues(WKTParser.apply)
lazy val projections: Set[WktCS] = parsed.values.toSet
lazy val records: Map[Int, String] = parseWktEpsgResource

def parseWktEpsgResource(): Map[Int, String] = {
//read input from epsg.properties file
WKT.withWktFile { lines =>
println(lines)
val iter =
for (line <- lines) yield {
//split the line for parsing the wkt, aka remove code infront
val firstEquals = line.indexOf("=")
val code = line.substring(0, firstEquals).toInt
val wktString = line.substring(firstEquals + 1)
code -> wktString
}
iter.toMap
}
}

def contains(input: WktCS): Boolean = {
projections contains input
}

def getEpsgCodeOption(input: String): Option[Int] = {
val wktParsed = WKTParser(input)
parsed.find{
case (epsgCode, wkt) => wkt == wktParsed
}.map(_._1)
}

def getEpsgStringOption(input: Int): Option[String] = {
records.get(input).map(_.toString)
}


/**
* Returns the WKT representation given an EPSG code in the format EPSG:[number]
* @param code
* @return
*/
def fromEpsgCode(code: Int): String = epsgcodetowktmap(code.toString)
def fromEpsgCode(code: Int): String = getEpsgStringOption(code).get

/**
* Returns the numeric code of a WKT string given the authority
* @param wktString
* @return
*/
def getEpsgCode(wktString: String) = wkttoepsgcodemap(wktString)
def getEpsgCode(wktString: String): String = s"EPSG:${getEpsgCodeOption(wktString).get}"

private def withWktFile[T](f: Iterator[String] => T) = {
def withWktFile[T](f: Iterator[String] => T) = {
val stream = getClass.getResourceAsStream(wktResourcePath)
try {
f(Source.fromInputStream(stream).getLines())
Expand All @@ -52,35 +82,4 @@ object WKT {
}
}

/**
* Returns the WKT string for the passed EPSG code
* @param code
* @return
*/
private def retrieveWKTStringFromFile(code: String): String =
withWktFile { lines =>
lines.find(_.split("=")(0) == code) match {
case Some(string) =>
val array = string.split("=")
array(1)
case None =>
throw new NotFoundException(s"Unable to find WKT representation of EPSG:$code")
}
}

/**
* Returns the EPSG code for the passed WKT string
* @param wktString
* @return
*/
private def retrieveEpsgCodeFromFile(wktString: String): String =
withWktFile { lines =>
lines.find(_.split("=")(1) == wktString) match {
case Some(string) =>
val array = string.split("=")
s"EPSG:${array(0)}"
case None =>
throw new NotFoundException(s"Unable to find the EPSG code of $wktString")
}
}
}
Loading