Java:WFSDatastore返回要在主类中使用的功能

时间:2018-08-15 12:05:56

标签: java geotools

我在Java编程方面还很新,所以我非常感谢您的帮助。 我目前正在尝试在项目的另一个类中访问WFS数据存储的功能-以便进一步重新转换投影并通过处理显示这些功能。

使用return wfsDSF我无法以某种方式访问​​功能(lon,lat,date,time,rate ..)

在控制台中打印功能已对我有用。

此处是代码(网址和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

1 个答案:

答案 0 :(得分:0)

您正在朝着正确的方向前进,但是有一些更简单的方法可以帮助您完成尝试的工作。通常,我会将FeatureCollection返回到我的主程序以在那里进行处理。

类似这样:

private SimpleFeatureCollection getFeatureCollection(String typeName) throws IOException {
   return dataStore.getFeatureSource(typeName).getFeatures();
}

String typeName = "topp:states";
SimpleFeatureCollection features = me.getFeatureCollection(typeName);
try (SimpleFeatureIterator itr = features.features()) {
  while (itr.hasNext()) {
    SimpleFeature f = itr.next();
    //[.......]
  }
}

是常见的模式。注意,除非被迫总是使用SimpleFeatures而不是通用的。然后,可以使用适当的强制转换将属性作为Java对象来访问,而不必进行复杂的字符串操作。

    SimpleFeature f = itr.next();
    Geometry geom = (Geometry) f.getDefaultGeometry();
    double lat = geom.getCentroid().getY();
    double lon = geom.getCentroid().getX();
    String name = (String) f.getAttribute("STATE_NAME");
    String abbr = (String) f.getAttribute("STATE_ABBR");
    double people = (double) f.getAttribute("PERSONS");
    System.out.println(name + "\t(" + abbr + ")\t" + people + "\t(" + lat + "," + lon + ")");

很显然,您可以在此循环中随意执行所需的任何处理。对于几何图形,我知道自己有多边形,因此选择了centroid,如果您有点,则直接在该点上调用.getX().getY()

这是整个程序:

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.wfs.WFSDataStoreFactory;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

public class GetWFSAttributes {
  DataStore dataStore = null;

  public static void main(String[] args) throws IOException {
    String getCapabilities = "http://localhost:8080/geoserver/ows?service=wfs&version=1.1.0&request=GetCapabilities";
    GetWFSAttributes me = new GetWFSAttributes(getCapabilities);
    String[] names = me.getTypeNames();
    for (String name : names) {
      SimpleFeatureType schema = me.getSchema(name);
      System.out.println(name + ":" + schema);
    }

    String typeName = "topp:states";
    SimpleFeatureCollection features = me.getFeatureCollection(typeName);
    try (SimpleFeatureIterator itr = features.features()) {
      while (itr.hasNext()) {
        SimpleFeature f = itr.next();
        Geometry geom = (Geometry) f.getDefaultGeometry();
        double lat = geom.getCentroid().getY();
        double lon = geom.getCentroid().getX();
        String name = (String) f.getAttribute("STATE_NAME");
        String abbr = (String) f.getAttribute("STATE_ABBR");
        double people = (double) f.getAttribute("PERSONS");
        System.out.println(name + "\t(" + abbr + ")\t" + people + "\t(" + lat + "," + lon + ")");
      }
    }
  }

  private SimpleFeatureCollection getFeatureCollection(String typeName) throws IOException {
    return dataStore.getFeatureSource(typeName).getFeatures();
  }

  private String[] getTypeNames() throws IOException {
    return dataStore.getTypeNames();
  }

  private SimpleFeatureType getSchema(String name) throws IOException {
    return dataStore.getSchema(name);
  }

  public GetWFSAttributes(String capabilities) {
    aquireDataStoreWFS(capabilities);
  }

  public void aquireDataStoreWFS(String capabilities) {
    if (dataStore == null) {
      try {
        Map<String, Serializable> connectionParameters = new HashMap<>();
        connectionParameters.put(WFSDataStoreFactory.URL.key, capabilities);
        dataStore = DataStoreFinder.getDataStore(connectionParameters);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}