001: package org.geotools.demo.example;
002:
003: import java.util.Collections;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009:
010: import org.geotools.data.DataStore;
011: import org.geotools.data.DataStoreFinder;
012: import org.geotools.data.DefaultQuery;
013: import org.geotools.data.DefaultTransaction;
014: import org.geotools.data.FeatureSource;
015: import org.geotools.data.FeatureStore;
016: import org.geotools.data.Query;
017: import org.geotools.data.Transaction;
018: import org.geotools.factory.CommonFactoryFinder;
019: import org.geotools.factory.GeoTools;
020: import org.geotools.feature.Feature;
021: import org.geotools.feature.FeatureCollection;
022: import org.geotools.feature.FeatureType;
023: import org.geotools.geometry.jts.JTS;
024: import org.opengis.filter.Filter;
025: import org.opengis.filter.FilterFactory;
026: import org.opengis.filter.FilterFactory2;
027: import org.opengis.filter.spatial.Intersects;
028:
029: import com.vividsolutions.jts.geom.Envelope;
030:
031: public class WFSExample {
032: /**
033: * Before running this application please install and start geoserver on your local machine.
034: * @param args
035: */
036: public static void main(String[] args) {
037: String getCapabilities = "http://localhost:8080/geoserver/wfs?service=WFS&request=GetCapabilities";
038: if (args.length != 0) {
039: getCapabilities = args[0];
040: }
041: try {
042: supressInfo();
043: dataAccess(getCapabilities);
044: } catch (Exception e) {
045: e.printStackTrace();
046: }
047: }
048:
049: public static void supressInfo() {
050: org.geotools.util.logging.Logging.getLogger("org.geotools.gml")
051: .setLevel(Level.SEVERE);
052: org.geotools.util.logging.Logging.getLogger(
053: "net.refractions.xml").setLevel(Level.SEVERE);
054: }
055:
056: public static void dataAccess(String getCapabilities)
057: throws Exception {
058: // Step 1 - connection parameters
059: //
060: Map connectionParameters = new HashMap();
061: connectionParameters.put(
062: "WFSDataStoreFactory:GET_CAPABILITIES_URL",
063: getCapabilities);
064:
065: // Step 2 - connection
066: DataStore data = DataStoreFinder
067: .getDataStore(connectionParameters);
068:
069: // Step 3 - discouvery
070: String typeNames[] = data.getTypeNames();
071: String typeName = typeNames[0];
072: FeatureType schema = data.getSchema(typeName);
073: System.out.println("Schema Attributes:"
074: + schema.getAttributeCount());
075:
076: // Step 4 - target
077: FeatureSource source = data.getFeatureSource(typeName);
078: System.out.println("Metadata Bounds:" + source.getBounds());
079:
080: // Step 5 - query
081: String geomName = schema.getDefaultGeometry().getName();
082: Envelope bbox = new Envelope(-100.0, -70, 25, 40);
083:
084: FilterFactory2 ff = CommonFactoryFinder
085: .getFilterFactory2(GeoTools.getDefaultHints());
086: Object polygon = JTS.toGeometry(bbox);
087: Intersects filter = ff.intersects(ff.property(geomName), ff
088: .literal(polygon));
089:
090: Query query = new DefaultQuery(typeName, filter,
091: new String[] { geomName });
092: FeatureCollection features = source.getFeatures(query);
093:
094: Envelope bounds = new Envelope();
095: Iterator iterator = features.iterator();
096: try {
097: while (iterator.hasNext()) {
098: Feature feature = (Feature) iterator.next();
099:
100: bounds.expandToInclude(feature.getBounds());
101: }
102: System.out.println("Calculated Bounds:" + bounds);
103: } finally {
104: features.close(iterator);
105: }
106: }
107:
108: public static void dataUpdate(String getCapabilities)
109: throws Exception {
110: // Step 1 - connection parameters
111: //
112: Map connectionParameters = new HashMap();
113: connectionParameters.put(
114: "WFSDataStoreFactory:GET_CAPABILITIES_URL",
115: getCapabilities);
116:
117: // Step 2 - connection
118: DataStore data = DataStoreFinder
119: .getDataStore(connectionParameters);
120:
121: // Step 3 - discouvery
122: String typeNames[] = data.getTypeNames();
123: String typeName = typeNames[0];
124: FeatureType schema = data.getSchema(typeName);
125: System.out.println("Schema Attributes:"
126: + schema.getAttributeCount());
127:
128: // Step 4 - target
129: FeatureSource source = data.getFeatureSource(typeName);
130: System.out.println("Metadata Bounds:" + source.getBounds());
131:
132: // Step 5 - query
133: FilterFactory ff = CommonFactoryFinder
134: .getFilterFactory(GeoTools.getDefaultHints());
135:
136: DefaultQuery query = new DefaultQuery(typeName, Filter.INCLUDE);
137: query.setMaxFeatures(2);
138: FeatureCollection features = source.getFeatures(query);
139:
140: String fid = null;
141: Iterator iterator = features.iterator();
142: try {
143: while (iterator.hasNext()) {
144: Feature feature = (Feature) iterator.next();
145: fid = feature.getID();
146: }
147: } finally {
148: features.close(iterator);
149: }
150: // step 6 modify
151: Transaction t = new DefaultTransaction();
152:
153: FeatureStore store = (FeatureStore) source;
154: store.setTransaction(t);
155: Filter filter = ff.id(Collections.singleton(ff.featureId(fid)));
156: try {
157: store.removeFeatures(filter);
158: } finally {
159: t.rollback();
160: }
161: }
162:
163: }
|