Created
September 20, 2012 18:12
-
-
Save damionjunk/3757473 to your computer and use it in GitHub Desktop.
A more complete example of using GeoTools from clojure.
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
(ns geoloader.core | |
(:require [clojure.java.io :as io]) | |
(:import [com.vividsolutions.jts.geom Point Coordinate MultiPolygon] | |
[org.geotools.data.simple SimpleFeatureCollection SimpleFeatureIterator SimpleFeatureSource] | |
[org.geotools.data FileDataStore FileDataStoreFinder] | |
[org.geotools.geometry.jts JTSFactoryFinder] | |
[org.opengis.feature.simple SimpleFeature SimpleFeatureType] | |
[org.opengis.feature.type AttributeDescriptor AttributeType])) | |
(defn iteration-seq [iteration] | |
(iterator-seq | |
(reify java.util.Iterator | |
(hasNext [this] (.hasNext iteration)) | |
(next [this] (.next iteration)) | |
(remove [this] (.remove iteration))))) | |
(defn process-shape-file | |
[file] | |
(let [store (FileDataStoreFinder/getDataStore (io/file file)) | |
featuresource (.getFeatureSource store) | |
featureseq (iteration-seq (.. featuresource (getFeatures) (features)))] | |
(reduce | |
(fn [m feature] | |
(let [name (.getAttribute feature "NAME") | |
abbv (.getAttribute feature "ABBREV") | |
isoa2 (.getAttribute feature "ISO_A2") | |
isoa3 (.getAttribute feature "ISO_A3") | |
geom (.getAttribute feature "the_geom")] | |
(assoc m isoa2 | |
{:name name :abbrev abbv :isoa2 isoa2 :isoa3 isoa3 | |
:geom geom}) | |
)) {} featureseq))) | |
(defn locate-point | |
"Given a map of shapes see (process-shape-file), locate the entry that | |
contains the given lat/long, optionally filtered by keys." | |
([shapes long lat] (locate-point shapes lat long nil)) | |
([shapes long lat keys] | |
(let [locs (if keys (select-keys shapes keys) shapes) | |
point (.. JTSFactoryFinder (getGeometryFactory) | |
(createPoint (Coordinate. long lat))) | |
flocs (first | |
(filter (fn [x] | |
(let [geom (:geom (val x))] | |
(if (nil? geom) false (.contains geom point)))) | |
locs))] | |
(if flocs (val flocs))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment