尝试连接到WFS

时间:2017-06-01 05:10:14

标签: geotools

我是WFS和geotools的新手。我正在尝试连接到LRIS WFS。

代码如下:

String getCapabilities = "https://lris.scinfo.org.nz/services;key=ENTER-KEY/wfs/layer-66?service=WFS&request=GetCapabilities";

Map connectionParameters = new HashMap();
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities );

// Step 2 - connection      
DataStore data = DataStoreFinder.getDataStore( connectionParameters );

// Step 3 - discouvery
String typeNames[] = data.getTypeNames();

我在线上得到一个空指针异常:

String typeNames[] = data.getTypeNames();

感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

最佳做法是在使用之前检查DataStore是否已打开(即不是空)。

用上面的代码说我很容易得到数据,我假设你已经将自己的密钥添加到了URL中:

String getCapabilities = "https://lris.scinfo.org.nz/services;key=myKEY/wfs/layer-66?service=WFS&request=GetCapabilities";

    Map connectionParameters = new HashMap();
    connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities);
    connectionParameters.put("WFSDataStoreFactory:WFSDataStoreFactory:TIMEOUT", 10000000);
    // Step 2 - connection
    DataStore data = DataStoreFinder.getDataStore(connectionParameters);
    if(data==null) {
      System.err.println("failed to open datastore at "+getCapabilities);
    }
    // Step 3 - discouvery
    String types[] = data.getTypeNames();
    for (int i = 0; i < types.length; i++) {
      System.out.println(types[i]);

      String name = types[i];
      Query query = new Query(name);
      query.setCoordinateSystem(DefaultGeographicCRS.WGS84);
      SimpleFeatureSource source = data.getFeatureSource(name);
      SimpleFeatureType schema = source.getSchema();
      System.out.println(schema);
      SimpleFeatureCollection fc = source.getFeatures(query);
      int count = 0;
      while (fc.features().hasNext() && (count++ < 10)) {
        SimpleFeature sf = fc.features().next();
        System.out.println(sf);
      }
    }
  }

请注意,由于新西兰离英国很远,我确实需要调出超时参数。