Exemplo n.º 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();
  }
Exemplo n.º 2
0
 /**
  * Overrides to handle the case of encoding {@code java.util.Date} and its date/time/timestamp
  * descendants, as well as {@code java.util.Calendar} instances as ISO 8601 strings. In addition
  * handles rounding numbers to the specified number of decimal points.
  *
  * @see net.sf.json.util.JSONBuilder#value(java.lang.Object)
  */
 @Override
 public GeoJSONBuilder value(Object value) {
   if (value instanceof java.util.Date || value instanceof Calendar) {
     value = Converters.convert(value, String.class);
   }
   super.value(value);
   return this;
 }
Exemplo n.º 3
0
  public static void featureToJson(
      org.opengis.feature.Feature feature, JSONBuilder json, boolean returnGeometry) {
    GeometryAttribute geometry = feature.getDefaultGeometryProperty();
    json.object();
    if (returnGeometry) {
      json.key("geometry");
      GeometryEncoder.toJson((com.vividsolutions.jts.geom.Geometry) geometry.getValue(), json);
    }
    json.key("attributes");
    json.object();

    json.key("objectid").value(adaptId(feature.getIdentifier().getID()));

    for (Property prop : feature.getProperties()) {
      if (geometry == null || !prop.getName().equals(geometry.getName())) {
        final Object value;
        if (prop.getValue() instanceof java.util.Date) {
          value = ((java.util.Date) prop.getValue()).getTime();
        } else {
          value = prop.getValue();
        }
        json.key(prop.getName().getLocalPart()).value(value);
      }
    }

    json.endObject();
    json.endObject();
  }
Exemplo n.º 4
0
 @Test
 public void testIconRenderer() throws Exception {
   StyleFactory factory = CommonFactoryFinder.getStyleFactory();
   SLDParser parser = new SLDParser(factory, getClass().getResource("mark.sld"));
   org.geotools.styling.Style sld = parser.readXML()[0];
   JSONBuilder json = new JSONStringer();
   Renderer renderer = StyleEncoder.styleToRenderer((org.geotools.styling.Style) sld);
   assertNotNull(renderer);
   StyleEncoder.encodeRenderer(json, renderer);
   JSONObject object = JSONObject.fromObject(json.toString());
   JSONObject symbol = object.getJSONObject("symbol");
   String url = symbol.getString("url");
   String contentType = symbol.getString("contentType");
   int width = symbol.getInt("width");
   int height = symbol.getInt("height");
   assertTrue(url.endsWith("example.jpg"));
   assertEquals("image/jpeg", contentType);
   assertEquals(64, width);
   assertEquals(64, height);
 }
Exemplo n.º 5
0
  private static void descriptorToJson(PropertyDescriptor desc, JSONBuilder json) {
    String name = desc.getName().getLocalPart();
    FieldTypeEnum type = FieldTypeEnum.forClass(desc.getType().getBinding());
    String alias = name;
    // TODO: For text fields we are expected to include a "length" field.

    json.object()
        .key("name")
        .value(name)
        .key("type")
        .value(type.getFieldType())
        .key("alias")
        .value(alias)
        .endObject();
  }
Exemplo n.º 6
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();
  }
Exemplo n.º 7
0
 private void roundedValue(double value) {
   super.value(RoundingUtil.round(value, numDecimals));
 }