Search in sources :

Example 1 with GeometrySerializer

use of com.bedatadriven.geojson.GeometrySerializer in project activityinfo by bedatadriven.

the class AdminLevelResource method getFeatures.

@GET
@Timed(name = "site.rest.admin.features")
@Path("/entities/features")
public Response getFeatures(@InjectParam EntityManager em) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(baos, Charsets.UTF_8);
    List<AdminEntity> entities = em.createQuery("select e  from AdminEntity e where e.deleted = false and e.level = :level").setParameter("level", level).getResultList();
    JsonFactory jfactory = new JsonFactory();
    JsonGenerator json = jfactory.createJsonGenerator(writer);
    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
    json.setPrettyPrinter(prettyPrinter);
    json.writeStartObject();
    json.writeStringField("type", "FeatureCollection");
    json.writeFieldName("features");
    json.writeStartArray();
    GeometrySerializer geometrySerializer = new GeometrySerializer();
    for (AdminEntity entity : entities) {
        if (entity.getGeometry() != null) {
            json.writeStartObject();
            json.writeStringField("type", "Feature");
            json.writeNumberField("id", entity.getId());
            json.writeObjectFieldStart("properties");
            json.writeStringField("name", entity.getName());
            if (entity.getParentId() != null) {
                json.writeNumberField("parentId", entity.getParentId());
            }
            json.writeEndObject();
            json.writeFieldName("geometry");
            geometrySerializer.writeGeometry(json, entity.getGeometry());
            json.writeEndObject();
        }
    }
    json.writeEndArray();
    json.writeEndObject();
    json.close();
    return Response.ok().entity(baos.toByteArray()).type(MediaType.APPLICATION_JSON).build();
}
Also used : DefaultPrettyPrinter(org.codehaus.jackson.util.DefaultPrettyPrinter) NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) GeometrySerializer(com.bedatadriven.geojson.GeometrySerializer) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Timed(org.activityinfo.server.util.monitoring.Timed)

Aggregations

GeometrySerializer (com.bedatadriven.geojson.GeometrySerializer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)1 NewAdminEntity (org.activityinfo.server.endpoint.rest.model.NewAdminEntity)1 Timed (org.activityinfo.server.util.monitoring.Timed)1 JsonFactory (org.codehaus.jackson.JsonFactory)1 JsonGenerator (org.codehaus.jackson.JsonGenerator)1 DefaultPrettyPrinter (org.codehaus.jackson.util.DefaultPrettyPrinter)1