コード例 #1
0
  public static <T extends FeatureType, F extends org.opengis.feature.Feature> void featuresToJson(
      FeatureCollection<T, F> collection, JSONBuilder json, boolean returnGeometry)
      throws IOException {
    FeatureIterator<F> iterator = collection.features();

    T schema = collection.getSchema();
    json.object().key("objectIdFieldName").value("objectid").key("globalIdFieldName").value("");

    if (returnGeometry) {
      GeometryDescriptor geometryDescriptor = schema.getGeometryDescriptor();
      if (geometryDescriptor == null)
        throw new RuntimeException(
            "No geometry descriptor for type " + schema + "; " + schema.getDescriptors());
      GeometryType geometryType = geometryDescriptor.getType();
      if (geometryType == null) throw new RuntimeException("No geometry type for type " + schema);
      Class<?> binding = geometryType.getBinding();
      if (binding == null) throw new RuntimeException("No binding for geometry type " + schema);
      GeometryTypeEnum geometryTypeEnum = GeometryTypeEnum.forJTSClass(binding);
      json.key("geometryType").value(geometryTypeEnum.getGeometryType());
    }

    if (schema.getCoordinateReferenceSystem() != null) {
      try {
        SpatialReference sr = SpatialReferences.fromCRS(schema.getCoordinateReferenceSystem());
        json.key("spatialReference");
        SpatialReferenceEncoder.toJson(sr, json);
      } catch (FactoryException e) {
        throw new RuntimeException(e);
      }
    }

    json.key("fields").array();
    for (PropertyDescriptor desc : schema.getDescriptors()) {
      if (schema.getGeometryDescriptor() != null
          && !desc.getName().equals(schema.getGeometryDescriptor().getName())) {
        descriptorToJson(desc, json);
      }
    }
    json.endArray();

    try {
      json.key("features");
      json.array();
      while (iterator.hasNext()) {
        F feature = iterator.next();
        featureToJson(feature, json, returnGeometry);
      }
      json.endArray();
    } finally {
      iterator.close();
    }
    json.endObject();
  }
コード例 #2
0
  public static <T extends FeatureType, F extends Feature> void featureIdSetToJson(
      FeatureCollection<T, F> features, JSONBuilder json) {
    json.object();
    json.key("objectIdFieldName");
    json.value("objectid"); // TODO: Advertise "real" identifier property

    FeatureIterator<F> iterator = features.features();
    try {
      json.key("objectIds");
      json.array();
      while (iterator.hasNext()) {
        F feature = iterator.next();
        json.value(adaptId(feature.getIdentifier().getID()));
      }
      json.endArray();
    } finally {
      iterator.close();
    }

    json.endObject();
  }