Skip to content

Commit

Permalink
Allow a list of shapefiles on the commandline wrapper for the servlet.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattb committed Jan 2, 2013
1 parent 3afece7 commit 62fadb5
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
deploy.sh
target
data
.*.sw*
39 changes: 38 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hackdiary.geo</groupId>
<version>1.0-SNAPSHOT</version>
<version>1.1</version>
<artifactId>flickrgeocode</artifactId>
<dependencies>
<dependency>
Expand Down Expand Up @@ -62,6 +62,43 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.hackdiary.geo.FlickrGeocodeServlet</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<!-- Optional -->
<onejarVersion>0.97</onejarVersion>
<!-- Optional, default is false -->
<attachToBuild>true</attachToBuild>
<!-- Optional, default is "onejar" -->
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
</project>
8 changes: 4 additions & 4 deletions src/main/java/com/hackdiary/geo/CachingFlickrGeocode.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public String toString() {

public class CachingFlickrGeocode extends FlickrGeocode {
LoadingCache<LatLng, List<Map<String, Object>>> cache;
public CachingFlickrGeocode(URL url) throws IOException {
this(url, "maximumSize=10000");
public CachingFlickrGeocode(List<URL> urls) throws IOException {
this(urls, "maximumSize=10000");
}
public CachingFlickrGeocode(URL url, String cacheSpec) throws IOException {
super(url);
public CachingFlickrGeocode(List<URL> urls, String cacheSpec) throws IOException {
super(urls);
cache = CacheBuilder.newBuilder()
.from(cacheSpec)
.build(
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/hackdiary/geo/FlickrGeocode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ public class FlickrGeocode {
public STRtree tree = new STRtree();
GeometryFactory gf = new GeometryFactory();
public FlickrGeocode(URL url) throws IOException {
this(Collections.singletonList(url));
}
public FlickrGeocode(List<URL> urls) throws IOException {
org.geotools.util.logging.Logging.GEOTOOLS.setLoggerFactory(org.geotools.util.logging.Log4JLoggerFactory.getInstance());
org.apache.log4j.LogManager.getLogger("org.geotools").setLevel(org.apache.log4j.Level.OFF);
ShapefileDataStore data = new ShapefileDataStore(url);
int count = 0;
for(FeatureReader<SimpleFeatureType, SimpleFeature> reader = data.getFeatureReader() ; reader.hasNext(); count++) {
SimpleFeature f = reader.next();
tree.insert(JTS.toGeometry(f.getBounds()).getEnvelopeInternal(),f);
for(URL url : urls) {
ShapefileDataStore data = new ShapefileDataStore(url);
int count = 0;
for(FeatureReader<SimpleFeatureType, SimpleFeature> reader = data.getFeatureReader() ; reader.hasNext(); count++) {
SimpleFeature f = reader.next();
tree.insert(JTS.toGeometry(f.getBounds()).getEnvelopeInternal(),f);
}
}
}
public List<Map<String, Object>> geocode(double lat, double lng) {
Expand All @@ -39,12 +44,14 @@ public List<Map<String, Object>> geocode(double lat, double lng) {
MultiPolygon g = (MultiPolygon)feature.getDefaultGeometry();
try {
if(g.contains(p)) {
Map<String, Object> result = new HashMap();
Map<String, Object> result = new HashMap<String, Object>();
for(Property prop : feature.getProperties()) {
if(!prop.getName().toString().equals("the_geom")) {
result.put(prop.getName().toString(), prop.getValue());
}
}
result.put("midpoint_lat", feature.getBounds().getMedian(1));
result.put("midpoint_lng", feature.getBounds().getMedian(0));
results.add(result);
}
} catch(TopologyException e) {
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/hackdiary/geo/FlickrGeocodeServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;
import java.util.*;
import java.net.*;

import java.io.IOException;
import javax.servlet.ServletException;
Expand All @@ -13,12 +14,20 @@
public class FlickrGeocodeServlet extends HttpServlet {
FlickrGeocode geocode;
public FlickrGeocodeServlet() throws IOException {
geocode = new CachingFlickrGeocode(getClass().getResource("flickr_shapes_public_dataset_2.0/flickr_shapes_countries/OGRGeoJSON.shp"));
geocode = new CachingFlickrGeocode(Collections.singletonList(getClass().getResource("zillow/ZillowNeighborhoods-CA.shp")));
}
public FlickrGeocodeServlet(String[] files) throws IOException {
List<URL> urls = new ArrayList<URL>(files.length);
for(String file : files) {
urls.add(new File(file).toURI().toURL());
}
geocode = new CachingFlickrGeocode(urls);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
double lat = Double.parseDouble(req.getParameter("lat"));
double lng = Double.parseDouble(req.getParameter("lng"));
resp.setContentType("application/json");
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(resp.getWriter(), geocode.geocode(lat, lng));
}
Expand All @@ -28,7 +37,13 @@ public static void main(String[] args) throws Exception{
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new FlickrGeocodeServlet()),"/*");
FlickrGeocodeServlet servlet;
if(args.length == 0) {
servlet = new FlickrGeocodeServlet();
} else {
servlet = new FlickrGeocodeServlet(args);
}
context.addServlet(new ServletHolder(servlet),"/*");
server.start();
server.join();
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<!--<!DOCTYPE metadata SYSTEM "http://www.esri.com/metadata/esriprof80.dtd">-->
<metadata xml:lang="en"><Esri><MetaID>{63D678E6-82E9-4F47-A156-84E301C42FEF}</MetaID><CreaDate>20071221</CreaDate><CreaTime>17373300</CreaTime><SyncOnce>FALSE</SyncOnce><SyncDate>20071221</SyncDate><SyncTime>17373300</SyncTime><ModDate>20071221</ModDate><ModTime>17373300</ModTime></Esri><idinfo><native Sync="TRUE">Microsoft Windows XP Version 5.1 (Build 2600) Service Pack 2; ESRI ArcCatalog 9.2.0.1324</native><descript><langdata Sync="TRUE">en</langdata><abstract>Neighborhood boundaries for large cities in California. This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License. You are free to Share - to copy, distribute and transmit the work. You are free to adapt the work. You must attribute the work in the manner specified by the author or licensor. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. http://creativecommons.org/licenses/by-sa/3.0</abstract><purpose>Displaying neighborhoods. Organizing and aggregating geographic and point level data based on neighborhoods.</purpose></descript><citation><citeinfo><origin>Zillow.com compiled this data with the help of municipalities throughout the US.</origin><pubdate>20071001</pubdate><title Sync="FALSE">Neighborhood Boundaries in California</title><geoform Sync="TRUE">vector digital data</geoform><onlink Sync="FALSE">http://www.zillow.com/</onlink></citeinfo></citation><timeperd><current>Current as of 2007</current><timeinfo><sngdate><caldate>2007</caldate></sngdate></timeinfo></timeperd><status><progress>More neighborhoods will continue to be added to this data set.</progress><update>Roughly once per quarter.</update></status><spdom><bounding><westbc Sync="TRUE">-122.514493</westbc><eastbc Sync="TRUE">-116.908818</eastbc><northbc Sync="TRUE">38.685285</northbc><southbc Sync="TRUE">32.534855</southbc></bounding><lboundng><leftbc Sync="TRUE">-122.514493</leftbc><rightbc Sync="TRUE">-116.908818</rightbc><bottombc Sync="TRUE">32.534855</bottombc><topbc Sync="TRUE">38.685285</topbc></lboundng></spdom><keywords><theme><themekt>None</themekt><themekey>shape</themekey><themekey>neighborhood</themekey><themekey>polygon</themekey><themekey>boundaries</themekey></theme><place><placekt>None</placekt><placekey>United States</placekey><placekey>California</placekey><placekey>Neighborhoods</placekey></place></keywords><accconst><a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br />This <span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/" rel="dc:type">work</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0 License</a>. http://creativecommons.org/licenses/by-sa/3.0</accconst><useconst><a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br />This <span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/" rel="dc:type">work</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0 License</a>. http://creativecommons.org/licenses/by-sa/3.0</useconst><natvform Sync="TRUE">Shapefile</natvform></idinfo><dataIdInfo><envirDesc Sync="TRUE">Microsoft Windows XP Version 5.1 (Build 2600) Service Pack 2; ESRI ArcCatalog 9.2.0.1324</envirDesc><dataLang><languageCode Sync="TRUE" value="en"></languageCode></dataLang><idCitation><resTitle Sync="TRUE">ZillowNeighborhoods-CA</resTitle><presForm><PresFormCd Sync="TRUE" value="005"></PresFormCd></presForm></idCitation><spatRpType><SpatRepTypCd Sync="TRUE" value="001"></SpatRepTypCd></spatRpType><dataExt><geoEle><GeoBndBox esriExtentType="native"><westBL Sync="TRUE">-122.514493</westBL><eastBL Sync="TRUE">-116.908818</eastBL><northBL Sync="TRUE">38.685285</northBL><southBL Sync="TRUE">32.534855</southBL><exTypeCode Sync="TRUE">1</exTypeCode></GeoBndBox></geoEle></dataExt><geoBox esriExtentType="decdegrees"><westBL Sync="TRUE">-122.514493</westBL><eastBL Sync="TRUE">-116.908818</eastBL><northBL Sync="TRUE">38.685285</northBL><southBL Sync="TRUE">32.534855</southBL><exTypeCode Sync="TRUE">1</exTypeCode></geoBox></dataIdInfo><metainfo><langmeta Sync="TRUE">en</langmeta><metstdn Sync="TRUE">FGDC Content Standards for Digital Geospatial Metadata</metstdn><metstdv Sync="TRUE">FGDC-STD-001-1998</metstdv><mettc Sync="TRUE">local time</mettc><metextns><onlink Sync="TRUE">http://www.esri.com/metadata/esriprof80.html</onlink><metprof Sync="TRUE">ESRI Metadata Profile</metprof></metextns><metc><cntinfo><cntorgp><cntper>Tommy Unger</cntper><cntorg>Zillow.com</cntorg></cntorgp><cntaddr><addrtype>999 Third AVE, Suite 4600</addrtype><city>Seattle</city><state>WA</state><postal>98104</postal></cntaddr><cntvoice>206-470-7000</cntvoice></cntinfo></metc><metd Sync="TRUE">20071221</metd></metainfo><mdLang><languageCode Sync="TRUE" value="en"></languageCode></mdLang><mdStanName Sync="TRUE">ISO 19115 Geographic Information - Metadata</mdStanName><mdStanVer Sync="TRUE">DIS_ESRI1.0</mdStanVer><mdChar><CharSetCd Sync="TRUE" value="004"></CharSetCd></mdChar><mdHrLv><ScopeCd Sync="TRUE" value="005"></ScopeCd></mdHrLv><mdHrLvName Sync="TRUE">dataset</mdHrLvName><distinfo><resdesc Sync="TRUE">Downloadable Data</resdesc><stdorder><digform><digtinfo><transize Sync="TRUE">1.537</transize><dssize Sync="TRUE">1.537</dssize></digtinfo></digform></stdorder></distinfo><distInfo><distributor><distorTran><transSize Sync="TRUE">1.537</transSize></distorTran><distorFormat><formatName Sync="TRUE">Shapefile</formatName></distorFormat></distributor></distInfo><spdoinfo><direct Sync="TRUE">Vector</direct><ptvctinf><esriterm Name="ZillowNeighborhoods-CA"><efeatyp Sync="TRUE">Simple</efeatyp><efeageom Sync="TRUE">Polygon</efeageom><esritopo Sync="TRUE">FALSE</esritopo><efeacnt Sync="TRUE">948</efeacnt><spindex Sync="TRUE">FALSE</spindex><linrefer Sync="TRUE">FALSE</linrefer></esriterm><sdtsterm Name="ZillowNeighborhoods-CA"><sdtstype Sync="TRUE">G-polygon</sdtstype><ptvctcnt Sync="TRUE">948</ptvctcnt></sdtsterm></ptvctinf></spdoinfo><spref><horizsys><cordsysn><geogcsn Sync="TRUE">GCS_North_American_1983</geogcsn></cordsysn><geograph><geogunit Sync="TRUE">Decimal degrees</geogunit><latres Sync="TRUE">0.000000</latres><longres Sync="TRUE">0.000000</longres></geograph><geodetic><horizdn Sync="TRUE">North American Datum of 1983</horizdn><ellips Sync="TRUE">Geodetic Reference System 80</ellips><semiaxis Sync="TRUE">6378137.000000</semiaxis><denflat Sync="TRUE">298.257222</denflat></geodetic></horizsys></spref><refSysInfo><RefSystem><refSysID><identCode Sync="TRUE">GCS_North_American_1983</identCode></refSysID></RefSystem></refSysInfo><spatRepInfo><VectSpatRep><topLvl><TopoLevCd Sync="TRUE" value="001"></TopoLevCd></topLvl><geometObjs Name="ZillowNeighborhoods-CA"><geoObjTyp><GeoObjTypCd Sync="TRUE" value="001"></GeoObjTypCd></geoObjTyp><geoObjCnt Sync="TRUE">948</geoObjCnt></geometObjs></VectSpatRep></spatRepInfo><eainfo><detailed Name="ZillowNeighborhoods-CA"><enttyp><enttypl Sync="TRUE">ZillowNeighborhoods-CA</enttypl><enttypt Sync="TRUE">Feature Class</enttypt><enttypc Sync="TRUE">948</enttypc></enttyp><attr><attrlabl Sync="TRUE">FID</attrlabl><attalias Sync="TRUE">FID</attalias><attrtype Sync="TRUE">OID</attrtype><attwidth Sync="TRUE">4</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale><attrdef Sync="TRUE">Internal feature number.</attrdef><attrdefs Sync="TRUE">ESRI</attrdefs><attrdomv><udom Sync="TRUE">Sequential unique whole numbers that are automatically generated.</udom></attrdomv></attr><attr><attrlabl Sync="TRUE">Shape</attrlabl><attalias Sync="TRUE">Shape</attalias><attrtype Sync="TRUE">Geometry</attrtype><attwidth Sync="TRUE">0</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale><attrdef Sync="TRUE">Feature geometry.</attrdef><attrdefs Sync="TRUE">ESRI</attrdefs><attrdomv><udom Sync="TRUE">Coordinates defining the features.</udom></attrdomv></attr><attr><attrlabl Sync="TRUE">STATE</attrlabl><attalias Sync="TRUE">STATE</attalias><attrtype Sync="TRUE">String</attrtype><attwidth Sync="TRUE">2</attwidth></attr><attr><attrlabl Sync="TRUE">COUNTY</attrlabl><attalias Sync="TRUE">COUNTY</attalias><attrtype Sync="TRUE">String</attrtype><attwidth Sync="TRUE">43</attwidth></attr><attr><attrlabl Sync="TRUE">CITY</attrlabl><attalias Sync="TRUE">CITY</attalias><attrtype Sync="TRUE">String</attrtype><attwidth Sync="TRUE">64</attwidth></attr><attr><attrlabl Sync="TRUE">NAME</attrlabl><attalias Sync="TRUE">NAME</attalias><attrtype Sync="TRUE">String</attrtype><attwidth Sync="TRUE">64</attwidth></attr><attr><attrlabl Sync="TRUE">REGIONID</attrlabl><attalias Sync="TRUE">REGIONID</attalias><attrtype Sync="TRUE">Number</attrtype><attwidth Sync="TRUE">32</attwidth><atnumdec Sync="TRUE">10</atnumdec></attr></detailed></eainfo><mdDateSt Sync="TRUE">20071221</mdDateSt></metadata>
Binary file not shown.

0 comments on commit 62fadb5

Please sign in to comment.