i am pretty new in Java Programming so i am thankfull for any help. I am currently trying to get the Features of a WFS Datastore to be accessed in the another class of the project - for further retransforming the projection and displaying these features with processing.
with return wfsDSF i somehow can't access the features (lon, lat, date, time, rate..)
Printing the features in console already works for me.
here the code (except for url and featuresource):
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.wfs.WFSDataStore;
import org.geotools.data.wfs.WFSDataStoreFactory;
import org.opengis.feature.Feature;
public class WFSConnector {
public static WFSDataStoreFactory wfsDSF () throws SQLException {
// define the getCapabilities request
String wfsGetCapabilitiesURL = "URL";
// create WFSDataStoreFactory object
WFSDataStoreFactory wfsDSF = new WFSDataStoreFactory();
// create HashMap and fill it with connection parameters
HashMap connectionParameters = new HashMap();
connectionParameters.put(WFSDataStoreFactory.URL.key, wfsGetCapabilitiesURL);
connectionParameters.put(WFSDataStoreFactory.TIMEOUT.key, 20000);
try {
WFSDataStore wfsDS = wfsDSF.createDataStore(connectionParameters);
SimpleFeatureSource sfSource = wfsDS.getFeatureSource("SELECTED-FEATURE");
SimpleFeatureCollection sfCollection = sfSource.getFeatures();
SimpleFeatureIterator sfIterator = sfCollection.features();
// check if the FeatureReader object holds another
while(sfIterator.hasNext() == true)
{
//iterate through the "rows" of the WFS response
Feature currentFeature = sfIterator.next();
String coordinates = currentFeature.getProperty("the_geom").getValue().toString();
String fphenomenon = currentFeature.getProperty("phenomenon").getValue().toString();
String ftriptime = currentFeature.getProperty("tripTime").getValue().toString();
String fheartrate = currentFeature.getProperty("heartrate").getValue().toString();
//get rid of the name "point" and the brackets of the geometry string
int x = coordinates.lastIndexOf('(');
int y = coordinates.lastIndexOf(')');
String coord = coordinates.substring(x+1, y);
//split the coordinates into 2 parts
String[] splitcoordinates = coord.split(" ");
String lon = splitcoordinates[0];
String lat = splitcoordinates[1];
//split phenomenon into date and time
String date = fphenomenon.substring(0,10);
String time = fphenomenon.substring(11,19);
{
System.out.println(lon + " : " + lat + " : " + date + " : " + time + " : " + ftriptime + " : " + fheartrate);
}
}
}
catch (IOException e2) {
e2.printStackTrace();
}
return wfsDSF;
} // main
} // class