Last active
January 23, 2017 18:19
-
-
Save thampiman/a815c02d9e26a52ab6b6e8f4c5a21637 to your computer and use it in GitHub Desktop.
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
import spray.json._ | |
import com.esri.core.geometry.Point | |
import com.esri.core.geometry.OperatorContains | |
import com.esri.core.geometry.Geometry.GeometryAccelerationDegree | |
import com.esri.core.geometry.SpatialReference | |
import com.cloudera.science.geojson._ | |
import com.cloudera.science.geojson.GeoJsonProtocol._ | |
class ReverseGeocoder() extends java.io.Serializable { | |
// First load the GeoJSON data as a String | |
val geoJSONData = scala.io.Source.fromFile("<geojson_file_name>").mkString | |
// Parse and load the features in the GeoJSON | |
val features = geoJSONData.parseJson.convertTo[FeatureCollection] | |
/* Map each feature to a geometry | |
(similar to creating a geometry column in PostGIS) | |
*/ | |
features.map(a => OperatorContains.local().accelerateGeometry(a.geometry.geometry, SpatialReference.create(4326), GeometryAccelerationDegree.enumMedium)) | |
/* Function to reverse geocode | |
(similar to using ST_CONTAINS in PostGIS) | |
*/ | |
def reverseGeocode(longitude: Double, latitude: Double): Option[String] = { | |
val feature: Option[Feature] = features.find(f => {f.geometry.contains(new Point(longitude, latitude))}) | |
return feature.map(f => { f("region_name").convertTo[String] }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment