-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PNG / JPG / GeoTiff hadoop write methods
Signed-off-by: Grigory Pomadchin <[email protected]>
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
spark/src/main/scala/geotrellis/spark/raster/HadoopWriteMethods.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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.raster | ||
|
||
import geotrellis.util.MethodExtensions | ||
|
||
import org.apache.hadoop.conf.Configuration | ||
import org.apache.hadoop.fs.{FileSystem, Path} | ||
import org.apache.hadoop.io.compress.CompressionCodecFactory | ||
import org.apache.spark.SparkContext | ||
|
||
import java.io.DataOutputStream | ||
|
||
trait HadoopWriteMethods[T] extends MethodExtensions[T] { | ||
def write(path: Path)(implicit sc: SparkContext): Unit = write(path, gzip = false) | ||
def write(path: Path, gzip: Boolean)(implicit sc: SparkContext): Unit = write(path, gzip, sc.hadoopConfiguration) | ||
def write(path: Path, conf: Configuration): Unit = write(path, gzip = false, conf) | ||
def write(path: Path, gzip: Boolean, conf: Configuration): Unit | ||
} | ||
|
||
object HadoopWriteMethods { | ||
def write(path: Path, gzip: Boolean, conf: Configuration)(dosWrite: DataOutputStream => Unit): Unit = { | ||
val fs = FileSystem.get(conf) | ||
|
||
val os = | ||
if (!gzip) { | ||
fs.create(path) | ||
} else { | ||
val factory = new CompressionCodecFactory(conf) | ||
val outputUri = new Path(s"${path.toUri.toString}.gz") | ||
|
||
val codec = factory.getCodec(outputUri) | ||
|
||
if (codec == null) { | ||
println(s"No codec found for $outputUri, writing without compression.") | ||
fs.create(path) | ||
} else { | ||
codec.createOutputStream(fs.create(outputUri)) | ||
} | ||
} | ||
try { | ||
val dos = new DataOutputStream(os) | ||
try { | ||
dosWrite(dos) | ||
} finally { | ||
dos.close | ||
} | ||
} finally { | ||
os.close | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
spark/src/main/scala/geotrellis/spark/raster/Implicits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package geotrellis.spark.raster | ||
|
||
import geotrellis.raster.CellGrid | ||
import geotrellis.raster.io.geotiff.GeoTiff | ||
import geotrellis.raster.io.geotiff.writer.GeoTiffWriter | ||
import geotrellis.raster.render.{Jpg, Png} | ||
|
||
import org.apache.hadoop.conf.Configuration | ||
import org.apache.hadoop.fs.Path | ||
|
||
object Implicits extends Implicits | ||
|
||
trait Implicits { | ||
implicit class withJpgHadoopWriteMethods(val self: Jpg) extends HadoopWriteMethods[Jpg] { | ||
def write(path: Path, gzip: Boolean, conf: Configuration): Unit = | ||
HadoopWriteMethods.write(path, gzip, conf) { _.write(self.bytes) } | ||
} | ||
|
||
implicit class withPngHadoopWriteMethods(val self: Png) extends HadoopWriteMethods[Png] { | ||
def write(path: Path, gzip: Boolean, conf: Configuration): Unit = | ||
HadoopWriteMethods.write(path, gzip, conf) { _.write(self.bytes) } | ||
} | ||
|
||
implicit class withGeoTiffHadoopWriteMethods[T <: CellGrid](val self: GeoTiff[T]) extends HadoopWriteMethods[GeoTiff[T]] { | ||
def write(path: Path, gzip: Boolean, conf: Configuration): Unit = | ||
HadoopWriteMethods.write(path, gzip, conf) { new GeoTiffWriter(self, _).write() } | ||
} | ||
} |