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

Avro Schema Creation for ProjectedExtent and TemporalProjectedExtent #1971

Merged
merged 9 commits into from
Jan 23, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ trait Implicits
with TileFeatureCodec
with VectorTileCodec
with ExtentCodec
with ProjectedExtentCodec
with TemporalProjectedExtentCodec
with KeyCodecs {
implicit def tileUnionCodec = new AvroUnionCodec[Tile](
byteArrayTileCodec,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package geotrellis.spark.io.avro.codecs

import geotrellis.proj4.CRS
import geotrellis.spark._
import geotrellis.spark.io.avro._
import geotrellis.spark.io.avro.codecs._
import geotrellis.spark.io.avro.codecs.Implicits._
import geotrellis.vector._

import org.apache.avro._
import org.apache.avro.generic._

// --- //

trait ProjectedExtentCodec {
implicit def projectedExtentCodec = new AvroRecordCodec[ProjectedExtent] {
def schema: Schema = SchemaBuilder
.record("ProjectedExtent").namespace("geotrellis.vector")
.fields()
.name("extent").`type`(extentCodec.schema).noDefault()
.name("epsg").`type`().intType().noDefault()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update to use the proj4 string.
Or, if possible, check if it's an EPSG code, and then fall back to CRS?

.endRecord()

def encode(projectedExtent: ProjectedExtent, rec: GenericRecord): Unit = {
rec.put("extent", extentCodec.encode(projectedExtent.extent))
rec.put("epsg", projectedExtent.crs.epsgCode.get)
}

def decode(rec: GenericRecord): ProjectedExtent = {
val epsg = rec[Int]("epsg")
val crs = CRS.fromEpsgCode(epsg)

val extent = extentCodec.decode(rec[GenericRecord]("extent"))

ProjectedExtent(extent, crs)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package geotrellis.spark.io.avro.codecs

import geotrellis.proj4.CRS
import geotrellis.spark._
import geotrellis.spark.io.avro._
import geotrellis.spark.io.avro.codecs._
import geotrellis.spark.io.avro.codecs.Implicits._
import geotrellis.vector._

import org.apache.avro._
import org.apache.avro.generic._


trait TemporalProjectedExtentCodec {
implicit def temporalProjectedExtentCodec = new AvroRecordCodec[TemporalProjectedExtent] {
def schema: Schema = SchemaBuilder
.record("TemporalProjectedExtent").namespace("geotrellis.spark")
.fields()
.name("extent").`type`(extentCodec.schema).noDefault()
.name("epsg").`type`().intType().noDefault()
.name("instant").`type`().longType().noDefault()
.endRecord()

def encode(temporalProjectedExtent: TemporalProjectedExtent, rec: GenericRecord): Unit = {
rec.put("extent", extentCodec.encode(temporalProjectedExtent.extent))
rec.put("epsg", temporalProjectedExtent.crs.epsgCode.get)
rec.put("instant", temporalProjectedExtent.instant)
}

def decode(rec: GenericRecord): TemporalProjectedExtent = {
val instant = rec[Long]("instant")
val epsg = rec[Int]("epsg")
val crs = CRS.fromEpsgCode(epsg)

val extent = extentCodec.decode(rec[GenericRecord]("extent"))

TemporalProjectedExtent(extent, crs, instant)
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2016 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.spark.io.avro

import geotrellis.spark.io.avro.codecs._
import org.scalatest._
import geotrellis.proj4._
import geotrellis.vector._
import geotrellis.spark._
import geotrellis.spark.io.avro.AvroTools._

class ExtentCodecsSpec extends FunSpec with Matchers with AvroTools {
describe("ExtentCodecs") {
it("encodes Extent"){
roundTrip(Extent(0, 1, 2, 3))
}
it("encodes ProjectedExtent") {
roundTrip(ProjectedExtent(Extent(0, 1, 2, 3), CRS.fromEpsgCode(4324)))
}
it("encodes TemporalProjectedExtent"){
roundTrip(TemporalProjectedExtent(Extent(0, 1, 2, 3), CRS.fromEpsgCode(4324), 1.toLong))
}
}
}