|
| 1 | +package mil.nga.giat.geowave.examples.query; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Map.Entry; |
| 10 | + |
| 11 | +import mil.nga.giat.geowave.adapter.vector.FeatureDataAdapter; |
| 12 | +import mil.nga.giat.geowave.core.geotime.GeometryUtils; |
| 13 | +import mil.nga.giat.geowave.core.geotime.IndexType; |
| 14 | +import mil.nga.giat.geowave.core.geotime.store.query.SpatialQuery; |
| 15 | +import mil.nga.giat.geowave.core.store.CloseableIterator; |
| 16 | +import mil.nga.giat.geowave.core.store.DataStore; |
| 17 | +import mil.nga.giat.geowave.core.store.index.Index; |
| 18 | +import mil.nga.giat.geowave.datastore.accumulo.AccumuloDataStore; |
| 19 | +import mil.nga.giat.geowave.datastore.accumulo.BasicAccumuloOperations; |
| 20 | + |
| 21 | +import org.apache.accumulo.core.client.AccumuloException; |
| 22 | +import org.apache.accumulo.core.client.AccumuloSecurityException; |
| 23 | +import org.apache.accumulo.minicluster.MiniAccumuloCluster; |
| 24 | +import org.apache.accumulo.minicluster.MiniAccumuloConfig; |
| 25 | +import org.apache.commons.io.FileUtils; |
| 26 | +import org.geotools.feature.AttributeTypeBuilder; |
| 27 | +import org.geotools.feature.simple.SimpleFeatureBuilder; |
| 28 | +import org.geotools.feature.simple.SimpleFeatureTypeBuilder; |
| 29 | +import org.opengis.feature.simple.SimpleFeature; |
| 30 | +import org.opengis.feature.simple.SimpleFeatureType; |
| 31 | + |
| 32 | +import com.google.common.io.Files; |
| 33 | +import com.vividsolutions.jts.geom.Coordinate; |
| 34 | +import com.vividsolutions.jts.geom.Envelope; |
| 35 | +import com.vividsolutions.jts.geom.Geometry; |
| 36 | +import com.vividsolutions.jts.geom.Polygon; |
| 37 | + |
| 38 | +/** |
| 39 | + * This class is intended to provide a self-contained, easy-to-follow example of |
| 40 | + * a few GeoTools queries against GeoWave. For simplicity, a MiniAccumuloCluster |
| 41 | + * is spun up and a few points from the DC area are ingested (Washington |
| 42 | + * Monument, White House, FedEx Field). Two queries are executed against this |
| 43 | + * data set. |
| 44 | + */ |
| 45 | +public class GeotoolsQueryExample |
| 46 | +{ |
| 47 | + private static File tempAccumuloDir; |
| 48 | + private static MiniAccumuloCluster accumulo; |
| 49 | + private static DataStore dataStore; |
| 50 | + |
| 51 | + private static final Index index = IndexType.SPATIAL_VECTOR.createDefaultIndex(); |
| 52 | + |
| 53 | + // Points (to be ingested into GeoWave Data Store) |
| 54 | + private static final Coordinate washingtonMonument = new Coordinate( |
| 55 | + -77.0352, |
| 56 | + 38.8895); |
| 57 | + private static final Coordinate whiteHouse = new Coordinate( |
| 58 | + -77.0366, |
| 59 | + 38.8977); |
| 60 | + private static final Coordinate fedexField = new Coordinate( |
| 61 | + -76.8644, |
| 62 | + 38.9078); |
| 63 | + |
| 64 | + // cities used to construct Geometries for queries |
| 65 | + private static final Coordinate baltimore = new Coordinate( |
| 66 | + -76.6167, |
| 67 | + 39.2833); |
| 68 | + private static final Coordinate richmond = new Coordinate( |
| 69 | + -77.4667, |
| 70 | + 37.5333); |
| 71 | + private static final Coordinate harrisonburg = new Coordinate( |
| 72 | + -78.8689, |
| 73 | + 38.4496); |
| 74 | + |
| 75 | + private static final Map<String, Coordinate> cannedData = new HashMap<>(); |
| 76 | + |
| 77 | + static { |
| 78 | + cannedData.put( |
| 79 | + "Washington Monument", |
| 80 | + washingtonMonument); |
| 81 | + cannedData.put( |
| 82 | + "White House", |
| 83 | + whiteHouse); |
| 84 | + cannedData.put( |
| 85 | + "FedEx Field", |
| 86 | + fedexField); |
| 87 | + } |
| 88 | + |
| 89 | + public static void main( |
| 90 | + String[] args ) |
| 91 | + throws AccumuloException, |
| 92 | + AccumuloSecurityException, |
| 93 | + InterruptedException, |
| 94 | + IOException { |
| 95 | + |
| 96 | + // spin up a MiniAccumuloCluster and initialize the DataStore |
| 97 | + setup(); |
| 98 | + |
| 99 | + // ingest 3 points represented as SimpleFeatures: Washington Monument, |
| 100 | + // White House, FedEx Field |
| 101 | + ingestCannedData(); |
| 102 | + |
| 103 | + // execute a query for a bounding box |
| 104 | + executeBoundingBoxQuery(); |
| 105 | + |
| 106 | + // execute a query for a large polygon |
| 107 | + executePolygonQuery(); |
| 108 | + |
| 109 | + // stop MiniAccumuloCluster and delete temporary files |
| 110 | + cleanup(); |
| 111 | + } |
| 112 | + |
| 113 | + private static void setup() |
| 114 | + throws AccumuloException, |
| 115 | + AccumuloSecurityException, |
| 116 | + IOException, |
| 117 | + InterruptedException { |
| 118 | + |
| 119 | + final String ACCUMULO_USER = "root"; |
| 120 | + final String ACCUMULO_PASSWORD = "Ge0wave"; |
| 121 | + |
| 122 | + tempAccumuloDir = Files.createTempDir(); |
| 123 | + |
| 124 | + accumulo = new MiniAccumuloCluster( |
| 125 | + new MiniAccumuloConfig( |
| 126 | + tempAccumuloDir, |
| 127 | + ACCUMULO_PASSWORD)); |
| 128 | + |
| 129 | + accumulo.start(); |
| 130 | + |
| 131 | + dataStore = new AccumuloDataStore( |
| 132 | + new BasicAccumuloOperations( |
| 133 | + accumulo.getConnector( |
| 134 | + ACCUMULO_USER, |
| 135 | + ACCUMULO_PASSWORD))); |
| 136 | + } |
| 137 | + |
| 138 | + private static void ingestCannedData() { |
| 139 | + |
| 140 | + final List<SimpleFeature> points = new ArrayList<>(); |
| 141 | + |
| 142 | + System.out.println("Building SimpleFeatures from canned data set..."); |
| 143 | + |
| 144 | + for (Entry<String, Coordinate> entry : cannedData.entrySet()) { |
| 145 | + System.out.println("Added point: " + entry.getKey()); |
| 146 | + points.add(buildSimpleFeature( |
| 147 | + entry.getKey(), |
| 148 | + entry.getValue())); |
| 149 | + } |
| 150 | + |
| 151 | + System.out.println("Ingesting canned data..."); |
| 152 | + |
| 153 | + dataStore.ingest( |
| 154 | + new FeatureDataAdapter( |
| 155 | + getPointSimpleFeatureType()), |
| 156 | + index, |
| 157 | + points.iterator()); |
| 158 | + |
| 159 | + System.out.println("Ingest complete."); |
| 160 | + } |
| 161 | + |
| 162 | + private static void executeBoundingBoxQuery() |
| 163 | + throws IOException { |
| 164 | + |
| 165 | + System.out.println("Constructing bounding box for the area contained by [Baltimore, MD and Richmond, VA."); |
| 166 | + |
| 167 | + final Geometry boundingBox = GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope( |
| 168 | + baltimore, |
| 169 | + richmond)); |
| 170 | + |
| 171 | + System.out.println("Executing query, expecting to match ALL points..."); |
| 172 | + |
| 173 | + final CloseableIterator<SimpleFeature> iterator = dataStore.query( |
| 174 | + index, |
| 175 | + new SpatialQuery( |
| 176 | + boundingBox)); |
| 177 | + |
| 178 | + while (iterator.hasNext()) { |
| 179 | + System.out.println("Query match: " + iterator.next().getID()); |
| 180 | + } |
| 181 | + |
| 182 | + iterator.close(); |
| 183 | + } |
| 184 | + |
| 185 | + private static void executePolygonQuery() |
| 186 | + throws IOException { |
| 187 | + |
| 188 | + System.out.println("Constructing polygon for the area contained by [Baltimore, MD; Richmond, VA; Harrisonburg, VA]."); |
| 189 | + |
| 190 | + final Polygon polygon = GeometryUtils.GEOMETRY_FACTORY.createPolygon(new Coordinate[] { |
| 191 | + baltimore, |
| 192 | + richmond, |
| 193 | + harrisonburg, |
| 194 | + baltimore |
| 195 | + }); |
| 196 | + |
| 197 | + System.out.println("Executing query, expecting to match ALL points..."); |
| 198 | + |
| 199 | + final CloseableIterator<SimpleFeature> closableIterator = dataStore.query( |
| 200 | + index, |
| 201 | + new SpatialQuery( |
| 202 | + polygon)); |
| 203 | + |
| 204 | + while (closableIterator.hasNext()) { |
| 205 | + System.out.println("Query match: " + closableIterator.next().getID()); |
| 206 | + } |
| 207 | + |
| 208 | + closableIterator.close(); |
| 209 | + } |
| 210 | + |
| 211 | + private static void cleanup() |
| 212 | + throws IOException, |
| 213 | + InterruptedException { |
| 214 | + |
| 215 | + try { |
| 216 | + accumulo.stop(); |
| 217 | + } |
| 218 | + finally { |
| 219 | + FileUtils.deleteDirectory(tempAccumuloDir); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + private static SimpleFeatureType getPointSimpleFeatureType() { |
| 224 | + |
| 225 | + final String NAME = "PointSimpleFeatureType"; |
| 226 | + final SimpleFeatureTypeBuilder sftBuilder = new SimpleFeatureTypeBuilder(); |
| 227 | + final AttributeTypeBuilder atBuilder = new AttributeTypeBuilder(); |
| 228 | + sftBuilder.setName(NAME); |
| 229 | + sftBuilder.add(atBuilder.binding( |
| 230 | + String.class).nillable( |
| 231 | + false).buildDescriptor( |
| 232 | + "locationName")); |
| 233 | + sftBuilder.add(atBuilder.binding( |
| 234 | + Geometry.class).nillable( |
| 235 | + false).buildDescriptor( |
| 236 | + "geometry")); |
| 237 | + |
| 238 | + return sftBuilder.buildFeatureType(); |
| 239 | + } |
| 240 | + |
| 241 | + private static SimpleFeature buildSimpleFeature( |
| 242 | + String locationName, |
| 243 | + Coordinate coordinate ) { |
| 244 | + |
| 245 | + final SimpleFeatureBuilder builder = new SimpleFeatureBuilder( |
| 246 | + getPointSimpleFeatureType()); |
| 247 | + builder.set( |
| 248 | + "locationName", |
| 249 | + locationName); |
| 250 | + builder.set( |
| 251 | + "geometry", |
| 252 | + GeometryUtils.GEOMETRY_FACTORY.createPoint(coordinate)); |
| 253 | + |
| 254 | + return builder.buildFeature(locationName); |
| 255 | + } |
| 256 | + |
| 257 | +} |
0 commit comments