/gazetteer/src/main/java/org/ala/rest/Intersector.java
Java | 207 lines | 109 code | 33 blank | 65 comment | 6 complexity | 1127bfe094cb38b68865695c9f9c99b9 MD5 | raw file
1/* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5package org.ala.rest; 6 7import com.vividsolutions.jts.geom.Point; 8import java.io.File; 9import java.io.IOException; 10import java.io.Serializable; 11import java.util.ArrayList; 12import java.util.HashMap; 13import java.util.List; 14import java.util.Map; 15import java.util.concurrent.CountDownLatch; 16import java.util.concurrent.TimeUnit; 17import java.util.logging.Level; 18import java.util.logging.Logger; 19import javax.servlet.ServletContext; 20import org.geoserver.catalog.Catalog; 21import org.geoserver.catalog.LayerInfo; 22import org.geoserver.config.GeoServer; 23import org.geoserver.platform.GeoServerExtensions; 24import org.geotools.data.DataStore; 25import org.geotools.data.DefaultTransaction; 26import org.geotools.data.FeatureSource; 27import org.geotools.data.FeatureStore; 28import org.geotools.data.Transaction; 29import org.geotools.data.shapefile.ShapefileDataStore; 30import org.geotools.data.shapefile.ShapefileDataStoreFactory; 31import org.geotools.feature.FeatureCollection; 32import org.geotools.feature.simple.SimpleFeatureTypeBuilder; 33import org.geotools.referencing.crs.DefaultGeographicCRS; 34 35import org.geotools.util.logging.Logging; 36import org.opengis.feature.simple.SimpleFeatureType; 37import org.vfny.geoserver.util.DataStoreUtils; 38 39/** 40 * 41 * @author angus 42 */ 43public class Intersector { 44 45 GazetteerConfig gc = new GazetteerConfig(); 46 GeoServer gs = GeoServerExtensions.bean(GeoServer.class); 47 ServletContext sc = GeoServerExtensions.bean(ServletContext.class); 48 Catalog catalog = gs.getCatalog(); 49 private static final Logger logger = Logging.getLogger("org.ala.rest.GazetteerFeature"); 50 private List<FeatureSource> featureSources = new ArrayList(); 51 private List<String> layers; 52 private List<String> shapeFiles = new ArrayList(); 53 54 public Intersector(List<String> layers) { 55 this.layers = layers; 56 //Load all the layers to intersect with? 57// 58// try { 59// for (String layerName : layers) { 60// if (!gc.layerNameExists(layerName)) { 61// logger.log(Level.FINER, "layer {0} does not exist - trying aliases.", layerName); 62// layerName = gc.getNameFromAlias(layerName); 63// if (layerName.compareTo("") == 0) { 64// logger.finer("no aliases found for layer, giving up"); 65// } 66// } 67// ///else { 68// LayerInfo layerInfo = catalog.getLayerByName(layerName); 69// Map params = layerInfo.getResource().getStore().getConnectionParameters(); 70// 71// DataStore dataStore = DataStoreUtils.acquireDataStore(params, sc); 72// 73// FeatureSource layer = dataStore.getFeatureSource(layerName); 74// String shapeFileName = createShapeFile(layerName + ".shp",layer); 75// System.out.println("Loading layer: " + layer.getName()); 76// featureSources.add(layer); 77// shapeFiles.add(shapeFileName); 78// //} 79// 80// } 81// } catch (IOException e1) { 82// logger.log(Level.SEVERE, "IOException in Intersector: {0}", e1.getMessage()); 83// } catch (Exception e2) { 84// logger.log(Level.SEVERE, "Exception while creating shapefile {0}", e2.getMessage()); 85// } 86 } 87 88// public List<String[]> intersect(double[][] points) { 89// long [] timing = new long[6]; 90// timing[0] = System.currentTimeMillis(); 91// 92// List<String[]> allResults = new ArrayList(); 93// //TODO configurable thread count? 94// int threadCount = 6; 95// 96// for(String shapeFileName : shapeFiles) { 97// SimpleShapeFile ssf = new SimpleShapeFile(shapeFileName, threadCount); 98// String [] results = ssf.intersect(points); 99// 100// allResults.add(results); 101// } 102// return allResults; 103// } 104 public List<String[]> intersect(List<Point> points) { 105 106 List<String[]> resultSet = new ArrayList(); 107 //Check each point against each region in 108 System.out.println("Intersecting points ..."); 109 int threadCount = 1; 110 int step = points.size()/threadCount; 111 if (step == 0) { 112 step = 1; 113 threadCount = points.size(); 114 } 115 116 CountDownLatch latch = new CountDownLatch(threadCount); 117 List<IntersectThread> threads = new ArrayList(); 118 for (int i =0; i < threadCount; i++) { 119 String[] results = new String[step]; 120 IntersectThread it = new IntersectThread(points.subList(i*step, ((i+1)*step)),layers, results, latch); 121 threads.add(it); 122 it.start(); 123 } 124 try { 125 latch.await(); 126 } catch (InterruptedException E) { 127 } 128 129 for(IntersectThread it : threads) { 130 resultSet.add(it.results); 131 } 132 133 return resultSet; 134 135 } 136 137 private String createShapeFile(String fileName, FeatureSource layer) throws IOException, Exception { 138 139 FeatureCollection featureCollection = layer.getFeatures(); 140 141 File shapeFile = new File(fileName); 142 ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); 143 144 Map<String, Serializable> params = new HashMap<String, Serializable>(); 145 params.put("url", shapeFile.toURI().toURL()); 146 params.put("create spatial index", Boolean.TRUE); 147 148 149 ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); 150 newDataStore.createSchema((SimpleFeatureType)layer.getSchema()); 151 152 /* 153 * You can comment out this line if you are using the createFeatureType 154 * method (at end of class file) rather than DataUtilities.createType 155 */ 156 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); 157 158 /* 159 * Write the features to the shapefile 160 */ 161 Transaction transaction = new DefaultTransaction("create"); 162 163 String typeName = newDataStore.getTypeNames()[0]; 164 FeatureSource shapeSource = newDataStore.getFeatureSource(typeName); 165 166 if (shapeSource instanceof FeatureStore) { 167 FeatureStore shapeStore = (FeatureStore) shapeSource; 168 169 shapeStore.setTransaction(transaction); 170 try { 171 shapeStore.addFeatures(featureCollection); 172 transaction.commit(); 173 174 } catch (Exception e) { 175 176 logger.severe("Problem writing shapefile: " + e.getMessage()); 177 transaction.rollback(); 178 179 } finally { 180 transaction.close(); 181 } 182 //System.exit(0); // success! 183 } else { 184 throw new Exception(typeName + " does not support read/write access"); 185 186 } 187 188 return shapeFile.getAbsolutePath().replace(".shp", ""); 189 190 } 191 192 private static SimpleFeatureType createFeatureType() { 193 194 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); 195 builder.setName("Location"); 196 builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference system 197 198 // add attributes in order 199 builder.add("Location", Point.class); 200 builder.length(15).add("Name", String.class); // <- 15 chars width for name field 201 202 // build the type 203 final SimpleFeatureType LOCATION = builder.buildFeatureType(); 204 205 return LOCATION; 206 } 207}