UserLayerWFSHelper extends UserLayerService { @SuppressWarnings("unchecked") public SimpleFeatureCollection postProcess(SimpleFeatureCollection sfc) throws Exception { if (sfc.isEmpty()) { return sfc; } List<SimpleFeature> fc = new ArrayList<>(); SimpleFeatureType schema; String geomAttrName = sfc.getSchema().getGeometryDescriptor().getLocalName(); try (SimpleFeatureIterator it = sfc.features()) { SimpleFeature firstFeatureForSchemaGeneration = it.next(); String property_json_for_first = (String) firstFeatureForSchemaGeneration.getAttribute(USERLAYER_ATTR_PROPERTY_JSON); JSONObject properties_for_first = new JSONObject(property_json_for_first); Set<String> featureAttributeNames = JSONHelper.getObjectAsMap(properties_for_first).keySet(); schema = createType(sfc.getSchema(), properties_for_first); SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema); builder.set(geomAttrName, firstFeatureForSchemaGeneration.getDefaultGeometry()); for (String attrName : featureAttributeNames) { builder.set(attrName, properties_for_first.get(attrName)); } fc.add(builder.buildFeature(firstFeatureForSchemaGeneration.getID())); while (it.hasNext()) { SimpleFeature feature = it.next(); builder.set(geomAttrName, feature.getDefaultGeometry()); String property_json = (String) feature.getAttribute(USERLAYER_ATTR_PROPERTY_JSON); JSONObject properties = new JSONObject(property_json); for (String attrName : featureAttributeNames) { builder.set(attrName, properties.opt(attrName)); } fc.add(builder.buildFeature(feature.getID())); } } return new GeoJSONFeatureCollection(fc, schema); } UserLayerWFSHelper(); void init(); int getBaselayerId(); boolean isUserlayerLayer(OskariLayer layer); boolean isUserContentLayer(String layerId); int parseId(String layerId); Filter getWFSFilter(String layerId,ReferencedEnvelope bbox); @SuppressWarnings("unchecked") SimpleFeatureCollection postProcess(SimpleFeatureCollection sfc); boolean hasViewPermission(String id, User user); static final String PROP_USERLAYER_BASELAYER_ID; static final String PREFIX_USERLAYER; }
@Test public void testRetype() throws Exception { Map<String, Object> geojson = readResource("geojson.json"); SimpleFeatureCollection original = createCollection(geojson); SimpleFeature f = null; SimpleFeatureCollection retyped = new UserLayerWFSHelper().postProcess(original); try (SimpleFeatureIterator it = retyped.features()) { while (it.hasNext()) { SimpleFeature feature = it.next(); if ("vuser_layer_data.2970316".equals(feature.getID())) { f = feature; break; } } } assertEquals(5, f.getAttributeCount()); Property defaultGeometry = f.getDefaultGeometryProperty(); Property interpoloi = f.getProperty("INTERPOLOI"); Property lahdeainei = f.getProperty("LAHDEAINEI"); Property id = f.getProperty("ID"); Property laji = f.getProperty("LAJI"); assertEquals(original.getSchema().getGeometryDescriptor().getName().getLocalPart(), defaultGeometry.getName().getLocalPart()); assertEquals(MultiLineString.class, defaultGeometry.getType().getBinding()); assertEquals(MultiLineString.class, defaultGeometry.getValue().getClass()); assertEquals(Integer.class, interpoloi.getValue().getClass()); assertEquals(1, interpoloi.getValue()); assertEquals(String.class, lahdeainei.getValue().getClass()); assertEquals("0", lahdeainei.getValue()); assertEquals(Double.class, id.getValue().getClass()); assertEquals(1.48627129E8, ((Double) id.getValue()).doubleValue(), 1e-8); assertEquals(String.class, laji.getValue().getClass()); assertEquals("696", laji.getValue()); }
@Test public void testTooManyAttributesOnFeatures() throws Exception { Map<String, Object> geojson = readResource("geojson.json"); dropAttrFromFirstFeature(geojson); SimpleFeatureCollection original = createCollection(geojson); SimpleFeatureCollection retyped = new UserLayerWFSHelper().postProcess(original); try (SimpleFeatureIterator it = retyped.features()) { while (it.hasNext()) { SimpleFeature feature = it.next(); assertEquals(4, feature.getAttributeCount()); } } }
@Test public void testMissingAttributesOnFeatures() throws Exception { Map<String, Object> geojson = readResource("geojson.json"); addAttrToFirstFeature(geojson); SimpleFeatureCollection original = createCollection(geojson); SimpleFeatureCollection retyped = new UserLayerWFSHelper().postProcess(original); try (SimpleFeatureIterator it = retyped.features()) { while (it.hasNext()) { SimpleFeature feature = it.next(); assertEquals(6, feature.getAttributeCount()); } } }