Ejemplo n.º 1
0
  private static void writeFeatureType(ILayer layer, BufferedWriter out) throws IOException {
    WFSDataStore wfs = (WFSDataStore) layer.getResource(WFSDataStore.class, null);
    SimpleFeatureType type = layer.getSchema();
    String typeName = type.getName().getLocalPart();

    int hidden = layer.isVisible() ? 1 : 0;
    ServiceInfo serviceInfo = wfs.getInfo();
    String title = serviceInfo.getTitle();
    String version = "1.0.0"; // only known entry at this time //$NON-NLS-1$
    URI source = serviceInfo.getSource();
    String get = source == null ? "" : source.toString();

    ResourceInfo resourceInfo = wfs.getFeatureSource(typeName).getInfo();
    append(4, out, "<SimpleFeatureType hidden=\"" + hidden + "\">"); // $NON-NLS-1$ //$NON-NLS-2$
    append(
        6,
        out,
        "<Server service=\"OGC:WFS\" title=\""
            + title
            + "\" version=\""
            + version
            + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    append(
        8,
        out,
        "<OnlineResource method=\"GET\" xlink:href=\""
            + get
            + "\" xlink:type=\"simple\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
    append(6, out, "</Server>"); // $NON-NLS-1$
    append(6, out, "<Name>" + typeName + "</Name>"); // $NON-NLS-1$ //$NON-NLS-2$
    append(6, out, "<Title>" + resourceInfo.getTitle() + "</Title>"); // $NON-NLS-1$ //$NON-NLS-2$
    // if( !Double.isNaN( layer.getMinScaleDenominator() ))
    //        append( 6, out,
    // "<sld:MinScaleDenominator>"+layer.getMinScaleDenominator()+"</sld:MinScaleDenominator>");
    // if( !Double.isNaN( layer.getMaxScaleDenominator() ))
    //        append( 6, out,
    // "<sld:MinScaleDenominator>"+layer.getMaxScaleDenominator()+"</sld:MinScaleDenominator>");
    String SRS = "EPSG:4326";
    CoordinateReferenceSystem crs = resourceInfo.getCRS();
    // TODO: anyone knows how to get the urn for the crs object?
    append(6, out, "<SRS>" + SRS + "</SRS>"); // $NON-NLS-1$ //$NON-NLS-2$
    append(4, out, "</SimpleFeatureType>"); // $NON-NLS-1$
  }
  public String wfsRequest() {
    // String getCapabilities =
    // "http://"+host+":"+port+"/geoserver/"+workspace+"/wfs?REQUEST=GetCapabilities&version=1.0.0";
    String getCapabilities =
        "http://"
            + host
            + ":"
            + port
            + "/geoserver/"
            + workspace
            + "/ows?REQUEST=GetCapabilities&version=1.1.1&service=WFS";
    Map connectionParameters = new HashMap();
    connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities);
    connectionParameters.put("WFSDataStoreFactory:USERNAME", "admin");
    connectionParameters.put("WFSDataStoreFactory:PASSWORD", "geoserver");
    connectionParameters.put("WFSDataStoreFactory:WFS_STRATEGY", "geoserver");
    connectionParameters.put("WFSDataStoreFactory:MAXFEATURES", 1000);
    WFSDataStoreFactory dsf = new WFSDataStoreFactory();
    try {
      // WFS_1_0_0_DataStore wfs = dsf.createDataStore(connectionParameters);
      WFSDataStore dataStore = dsf.createDataStore(connectionParameters);

      String[] typeNames = dataStore.getTypeNames();
      System.out.println(ArrayUtils.toString(typeNames));

      SimpleFeatureSource source = dataStore.getFeatureSource(workspace + ":" + layer);
      SimpleFeatureCollection fc = source.getFeatures();
      StringWriter writer = new StringWriter();
      while (fc.features().hasNext()) {
        SimpleFeature sf = fc.features().next();
        System.out.println(sf.getAttribute("myname"));
        FeatureJSON fjson = new FeatureJSON();
        fjson.writeFeature(sf, writer);
      }
      String geojson = writer.toString();
      return geojson;
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return null;
  }