|
8 | 8 | import java.io.IOException; |
9 | 9 | import java.lang.invoke.MethodHandles; |
10 | 10 | import java.util.HashMap; |
| 11 | +import java.util.List; |
11 | 12 | import java.util.Map; |
12 | 13 | import org.geotools.api.data.DataStore; |
13 | 14 | import org.geotools.api.data.DataStoreFinder; |
14 | 15 | import org.geotools.api.data.Query; |
15 | 16 | import org.geotools.api.feature.Property; |
16 | 17 | import org.geotools.api.feature.simple.SimpleFeature; |
| 18 | +import org.geotools.api.feature.type.AttributeDescriptor; |
17 | 19 | import org.geotools.api.feature.type.GeometryDescriptor; |
| 20 | +import org.geotools.api.feature.type.Name; |
18 | 21 | import org.geotools.api.filter.Filter; |
19 | 22 | import org.geotools.data.simple.SimpleFeatureIterator; |
20 | 23 | import org.geotools.data.wfs.WFSDataStoreFactory; |
@@ -70,24 +73,30 @@ public Map<String, String> getOneFeature(String typeName, String cqlFilter) thro |
70 | 73 | GeometryDescriptor geomAttr = ds.getSchema(typeName).getGeometryDescriptor(); |
71 | 74 | logger.info("Geometry attribute for type {}: {}", typeName, geomAttr); |
72 | 75 |
|
| 76 | + // filter out the geometry attribute, unless it is a point, not needed for our demonstration and it could be |
| 77 | + // overly verbose |
| 78 | + if (geomAttr != null && !(geomAttr.getType().getBinding() == org.locationtech.jts.geom.Point.class)) { |
| 79 | + List<String> attributeList = ds.getSchema(typeName).getAttributeDescriptors().stream() |
| 80 | + .map(AttributeDescriptor::getName) |
| 81 | + .filter(name -> !name.equals(geomAttr.getName())) |
| 82 | + .map(Name::getLocalPart) |
| 83 | + .toList(); |
| 84 | + q.setPropertyNames(attributeList); |
| 85 | + } |
| 86 | + |
73 | 87 | try (SimpleFeatureIterator it = |
74 | 88 | ds.getFeatureSource(typeName).getFeatures(q).features()) { |
75 | 89 | if (it.hasNext()) { |
76 | 90 | SimpleFeature feature = it.next(); |
77 | | - Map<String, String> attributes = new HashMap<>(); |
| 91 | + Map<String, String> attributesMap = new HashMap<>(); |
78 | 92 | for (Property property : feature.getProperties()) { |
79 | | - if (property.getDescriptor() == geomAttr) { |
80 | | - // Skip/replace geometry attribute value, as it might be too verbose |
81 | | - attributes.put(property.getName().toString(), "GEOMETRY (/* omitted */)"); |
82 | | - } else { |
83 | | - attributes.put( |
84 | | - property.getName().toString(), |
85 | | - property.getValue() != null |
86 | | - ? property.getValue().toString() |
87 | | - : null); |
88 | | - } |
| 93 | + attributesMap.put( |
| 94 | + property.getName().toString(), |
| 95 | + property.getValue() != null |
| 96 | + ? property.getValue().toString() |
| 97 | + : null); |
89 | 98 | } |
90 | | - return attributes; |
| 99 | + return attributesMap; |
91 | 100 | } |
92 | 101 | logger.info("No feature found for type {} and filter {}", typeName, cqlFilter); |
93 | 102 | return null; |
|
0 commit comments