private int getDescriptorIndex(String key, ImmutableList<PropertyDescriptor> properties) { for (int i = 0; i < properties.size(); i++) { PropertyDescriptor prop = properties.get(i); if (prop.getName().toString().equals(key)) { return i; } } return -1; }
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(); }
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(); }
public static String getGridAttributeName(MapLayer layer) { String attrName = null; Collection<PropertyDescriptor> descriptors = layer.getFeatureSource().getSchema().getDescriptors(); for (PropertyDescriptor desc : descriptors) { Class<?> binding = desc.getType().getBinding(); if (BASE_GRID_CLASS.isAssignableFrom(binding) || BASE_READER_CLASS.isAssignableFrom(binding)) { attrName = desc.getName().getLocalPart(); break; } } return attrName; }
/* This method is default for testing purposes */ Query createQuery(ExtractorLayerRequest request, FeatureType schema) throws IOException, TransformException, FactoryException { switch (request._owsType) { case WFS: // bbox may not be in the same projection as the data so it sometimes necessary to reproject // the request BBOX ReferencedEnvelope bbox = request._bbox; if (schema.getCoordinateReferenceSystem() != null) { bbox = request._bbox.transform(schema.getCoordinateReferenceSystem(), true, 10); } FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints()); String propertyName = schema.getGeometryDescriptor().getLocalName(); PropertyName geomProperty = filterFactory.property(propertyName); Geometry bboxGeom = new GeometryFactory().toGeometry(bbox); String epsgCode = "EPSG:" + CRS.lookupEpsgCode(bbox.getCoordinateReferenceSystem(), false); bboxGeom.setUserData(epsgCode); Literal geometry = filterFactory.literal(bboxGeom); Intersects filter = filterFactory.intersects(geomProperty, geometry); List<String> properties = new ArrayList<String>(); for (PropertyDescriptor desc : schema.getDescriptors()) { if (desc instanceof GeometryDescriptor && desc != schema.getGeometryDescriptor()) { // shapefiles can only have one geometry so skip any // geometry descriptor that is not the default continue; } else { properties.add(desc.getName().getLocalPart()); } } String[] propArray = properties.toArray(new String[properties.size()]); Query query = new Query(request.getWFSName(), filter, propArray); query.setCoordinateSystemReproject(request._projection); return query; default: return null; } }
protected void doSearch() { Filter filter = featureLayer.filter(); String s = searchText.getText().getText(); if (!StringUtils.isBlank(s)) { if (!s.contains("*") && !s.contains("?")) { s = s + "*"; } for (PropertyDescriptor prop : fs.getSchema().getDescriptors()) { if (String.class.isAssignableFrom(prop.getType().getBinding())) { PropertyIsLike isLike = ff.like(ff.property(prop.getName()), s, "*", "?", "\\"); filter = filter == Filter.INCLUDE ? isLike : ff.or(filter, isLike); } } } viewer.setSelection(StructuredSelection.EMPTY); log.info("FILTER: " + filter); // FeatureCollection filtered = features.subCollection( filter ); // log.info( "RESULT: " + filtered.size() ); contentProvider.filter(filter); }
private void printAttributeDescriptor(Writer w, PropertyDescriptor attrib) throws IOException { print(w, attrib.getName().toString()); print(w, "\t"); print(w, FieldType.forBinding(attrib.getType().getBinding()).name()); print(w, "\t"); print(w, Integer.toString(attrib.getMinOccurs())); print(w, "\t"); print(w, Integer.toString(attrib.getMaxOccurs())); print(w, "\t"); print(w, Boolean.toString(attrib.isNillable())); PropertyType attrType = attrib.getType(); if (attrType instanceof GeometryType) { GeometryType gt = (GeometryType) attrType; CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem(); String crsText = CrsTextSerializer.serialize(crs); print(w, "\t"); println(w, crsText); } else { println(w, ""); } }
@Override public void postCreateTable(String schemaName, SimpleFeatureType featureType, Connection cx) throws SQLException { Statement st = cx.createStatement(); String tableName = featureType.getTypeName(); try { // post process the feature type and set up constraints based on geometry type for (PropertyDescriptor ad : featureType.getDescriptors()) { if (ad instanceof GeometryDescriptor) { GeometryDescriptor gd = (GeometryDescriptor) ad; Class binding = ad.getType().getBinding(); String propertyName = ad.getName().getLocalPart(); // create a spatial index int epsg = -1; try { CoordinateReferenceSystem crs = gd.getCoordinateReferenceSystem(); if (crs == null) { continue; } epsg = CRS.lookupEpsgCode(crs, true); } catch (FactoryException e) { LOGGER.log(Level.FINER, "Unable to look epsg code", e); } StringBuffer sql = new StringBuffer(); sql.append("CALL AddGeometryColumn("); if (schemaName != null) { sql.append("'").append(schemaName).append("'"); } else { sql.append("NULL"); } sql.append(", '").append(tableName).append("'"); sql.append(", '").append(gd.getLocalName()).append("'"); sql.append(", ").append(epsg); sql.append(", '").append(Geometries.getForBinding(binding).getName()).append("'"); sql.append(", ").append(2); // TODO: dimension sql.append(")"); LOGGER.fine(sql.toString()); st.execute(sql.toString()); if (epsg != -1) { sql = new StringBuffer(); sql.append("CALL CreateSpatialIndex("); if (schemaName == null) { sql.append("NULL"); } else { sql.append("'").append(schemaName).append("'"); } sql.append(",'").append(tableName).append("'"); sql.append(",'").append(propertyName).append("'"); sql.append(",'").append(epsg).append("')"); LOGGER.fine(sql.toString()); st.execute(sql.toString()); } } } } finally { dataStore.closeSafe(st); } }
private void writeCSV(GeoGIT geogit, Writer out, Iterator<RevCommit> log) throws Exception { String response = "ChangeType,FeatureId,CommitId,Parent CommitIds,Author Name,Author Email,Author Commit Time,Committer Name,Committer Email,Committer Commit Time,Commit Message"; out.write(response); response = ""; String path = paths.get(0); // This is the feature type object Optional<NodeRef> ref = geogit .command(FindTreeChild.class) .setChildPath(path) .setParent(geogit.getRepository().workingTree().getTree()) .call(); Optional<RevObject> type = Optional.absent(); if (ref.isPresent()) { type = geogit .command(RevObjectParse.class) .setRefSpec(ref.get().getMetadataId().toString()) .call(); } else { throw new CommandSpecException("Couldn't resolve the given path."); } if (type.isPresent() && type.get() instanceof RevFeatureType) { RevFeatureType featureType = (RevFeatureType) type.get(); Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors(); int attributeLength = attribs.size(); for (PropertyDescriptor attrib : attribs) { response += "," + escapeCsv(attrib.getName().toString()); } response += '\n'; out.write(response); response = ""; RevCommit commit = null; while (log.hasNext()) { commit = log.next(); String parentId = commit.getParentIds().size() >= 1 ? commit.getParentIds().get(0).toString() : ObjectId.NULL.toString(); Iterator<DiffEntry> diff = geogit .command(DiffOp.class) .setOldVersion(parentId) .setNewVersion(commit.getId().toString()) .setFilter(path) .call(); while (diff.hasNext()) { DiffEntry entry = diff.next(); response += entry.changeType().toString() + ","; String fid = ""; if (entry.newPath() != null) { if (entry.oldPath() != null) { fid = entry.oldPath() + " -> " + entry.newPath(); } else { fid = entry.newPath(); } } else if (entry.oldPath() != null) { fid = entry.oldPath(); } response += fid + ","; response += commit.getId().toString() + ","; response += parentId; if (commit.getParentIds().size() > 1) { for (int index = 1; index < commit.getParentIds().size(); index++) { response += " " + commit.getParentIds().get(index).toString(); } } response += ","; if (commit.getAuthor().getName().isPresent()) { response += escapeCsv(commit.getAuthor().getName().get()); } response += ","; if (commit.getAuthor().getEmail().isPresent()) { response += escapeCsv(commit.getAuthor().getEmail().get()); } response += "," + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z") .format(new Date(commit.getAuthor().getTimestamp())) + ","; if (commit.getCommitter().getName().isPresent()) { response += escapeCsv(commit.getCommitter().getName().get()); } response += ","; if (commit.getCommitter().getEmail().isPresent()) { response += escapeCsv(commit.getCommitter().getEmail().get()); } response += "," + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z") .format(new Date(commit.getCommitter().getTimestamp())) + ","; String message = escapeCsv(commit.getMessage()); response += message; if (entry.newObjectId() == ObjectId.NULL) { // Feature was removed so we need to fill out blank attribute values for (int index = 0; index < attributeLength; index++) { response += ","; } } else { // Feature was added or modified so we need to write out the // attribute // values from the feature Optional<RevObject> feature = geogit.command(RevObjectParse.class).setObjectId(entry.newObjectId()).call(); RevFeature revFeature = (RevFeature) feature.get(); List<Optional<Object>> values = revFeature.getValues(); for (int index = 0; index < values.size(); index++) { Optional<Object> value = values.get(index); PropertyDescriptor attrib = (PropertyDescriptor) attribs.toArray()[index]; String stringValue = ""; if (value.isPresent()) { FieldType attributeType = FieldType.forBinding(attrib.getType().getBinding()); switch (attributeType) { case DATE: stringValue = new SimpleDateFormat("MM/dd/yyyy z").format((java.sql.Date) value.get()); break; case DATETIME: stringValue = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format((Date) value.get()); break; case TIME: stringValue = new SimpleDateFormat("HH:mm:ss z").format((Time) value.get()); break; case TIMESTAMP: stringValue = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z") .format((Timestamp) value.get()); break; default: stringValue = escapeCsv(value.get().toString()); } response += "," + stringValue; } else { response += ","; } } } response += '\n'; out.write(response); response = ""; } } } else { // Couldn't resolve FeatureType throw new CommandSpecException("Couldn't resolve the given path to a feature type."); } }
/** * Executes the diff operation. * * @return an iterator to a set of differences between the two trees * @see DiffEntry */ @Override protected AutoCloseableIterator<ChangeContainer> _call() { AutoCloseableIterator<DiffEntry> nodeIterator = command(DiffOp.class) .setFilter(OSMUtils.NODE_TYPE_NAME) .setNewVersion(newRefSpec) .setOldVersion(oldRefSpec) .setReportTrees(false) .call(); AutoCloseableIterator<DiffEntry> wayIterator = command(DiffOp.class) .setFilter(OSMUtils.WAY_TYPE_NAME) .setNewVersion(newRefSpec) .setOldVersion(oldRefSpec) .setReportTrees(false) .call(); AutoCloseableIterator<DiffEntry> iterator = AutoCloseableIterator.concat(nodeIterator, wayIterator); final EntityConverter converter = new EntityConverter(); final Function<DiffEntry, ChangeContainer> function = (diff) -> { NodeRef ref = diff.changeType().equals(ChangeType.REMOVED) ? diff.getOldObject() : diff.getNewObject(); RevFeature revFeature = command(RevObjectParse.class) .setObjectId(ref.getObjectId()) .call(RevFeature.class) .get(); RevFeatureType revFeatureType = command(RevObjectParse.class) .setObjectId(ref.getMetadataId()) .call(RevFeatureType.class) .get(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type()); ImmutableList<PropertyDescriptor> descriptors = revFeatureType.descriptors(); for (int i = 0; i < descriptors.size(); i++) { PropertyDescriptor descriptor = descriptors.get(i); Optional<Object> value = revFeature.get(i); featureBuilder.set(descriptor.getName(), value.orNull()); } SimpleFeature feature = featureBuilder.buildFeature(ref.name()); Entity entity = converter.toEntity(feature, id); EntityContainer container; if (entity instanceof Node) { container = new NodeContainer((Node) entity); } else { container = new WayContainer((Way) entity); } ChangeAction action = diff.changeType().equals(ChangeType.ADDED) ? ChangeAction.Create : diff.changeType().equals(ChangeType.MODIFIED) ? ChangeAction.Modify : ChangeAction.Delete; return new ChangeContainer(container, action); }; return AutoCloseableIterator.transform(iterator, function); }
/** * Write SQL string to create tables in the test database based on the property files. * * @param propertyFiles Property files from app-schema-test suite. * @param parser The parser (WKT or an SC4O one for 3D tests) * @throws IllegalAttributeException * @throws NoSuchElementException * @throws IOException */ private void createTables(Map<String, File> propertyFiles, String parser) throws IllegalAttributeException, NoSuchElementException, IOException { StringBuffer buf = new StringBuffer(); StringBuffer spatialIndex = new StringBuffer(); // drop table procedure I copied from Victor's Oracle_Data_ref_set.sql buf.append("CREATE OR REPLACE PROCEDURE DROP_TABLE_OR_VIEW(TabName in Varchar2) IS ") .append("temp number:=0;") .append(" tes VARCHAR2 (200) := TabName;") .append(" drp_stmt VARCHAR2 (200):=null;") .append("BEGIN select count(*) into temp from user_tables where TABLE_NAME = tes;") .append("if temp = 1 then drp_stmt := 'Drop Table '||tes;") .append("EXECUTE IMMEDIATE drp_stmt;") // drop views too .append("else select count(*) into temp from user_views where VIEW_NAME = tes;") .append("if temp = 1 then drp_stmt := 'Drop VIEW '||tes;") .append("EXECUTE IMMEDIATE drp_stmt;end if;end if;") .append("EXCEPTION WHEN OTHERS THEN ") .append( "raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);") .append("END DROP_TABLE_OR_VIEW;\n"); for (String fileName : propertyFiles.keySet()) { File file = new File(propertyFiles.get(fileName), fileName); try (PropertyFeatureReader reader = new PropertyFeatureReader("test", file)) { SimpleFeatureType schema = reader.getFeatureType(); String tableName = schema.getName().getLocalPart().toUpperCase(); // drop table if exists buf.append("CALL DROP_TABLE_OR_VIEW('").append(tableName).append("')\n"); // create the table buf.append("CREATE TABLE ").append(tableName).append("("); // + pkey int size = schema.getAttributeCount() + 1; String[] fieldNames = new String[size]; List<String> createParams = new ArrayList<String>(); int j = 0; String type; String field; int spatialIndexCounter = 0; for (PropertyDescriptor desc : schema.getDescriptors()) { field = desc.getName().toString().toUpperCase(); fieldNames[j] = field; if (desc instanceof GeometryDescriptor) { type = "SDO_GEOMETRY"; // Update spatial index int srid = getSrid(((GeometryType) desc.getType())); spatialIndex .append("DELETE FROM user_sdo_geom_metadata WHERE table_name = '") .append(tableName) .append("'\n"); spatialIndex .append("Insert into user_sdo_geom_metadata ") .append("(TABLE_NAME,COLUMN_NAME,DIMINFO,SRID)") .append("values ('") .append(tableName) .append("','") .append(field) .append("',MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X',140.962,144.909,0.00001),") .append("MDSYS.SDO_DIM_ELEMENT('Y',-38.858,-33.98,0.00001)") .append( // support 3d index ((GeometryDescriptor) desc).getCoordinateReferenceSystem() != null && ((GeometryDescriptor) desc) .getCoordinateReferenceSystem() .getCoordinateSystem() .getDimension() == 3 ? ", MDSYS.SDO_DIM_ELEMENT('Z',-100000, 100000, 1) )," : "),") .append(srid) .append(")\n"); // ensure it's <= 30 characters to avoid Oracle exception String indexName = (tableName.length() <= 26 ? tableName : tableName.substring(0, 26)) + "_IDX"; if (spatialIndexCounter > 0) { // to avoid duplicate index name when there are > 1 geometry in the same table indexName += spatialIndexCounter; } spatialIndex .append("CREATE INDEX \"") .append(indexName) .append("\" ON \"") .append(tableName) .append("\"(\"") .append(field) .append("\") ") .append("INDEXTYPE IS \"MDSYS\".\"SPATIAL_INDEX\"\n"); spatialIndexCounter++; } else { type = Classes.getShortName(desc.getType().getBinding()); if (type.equalsIgnoreCase("String")) { type = "NVARCHAR2(250)"; } else if (type.equalsIgnoreCase("Double")) { type = "NUMBER"; } // etc. assign as required } createParams.add(field + " " + type); j++; } // Add numeric PK for sorting fieldNames[j] = "PKEY"; createParams.add("PKEY VARCHAR2(30)"); buf.append(StringUtils.join(createParams.iterator(), ", ")); buf.append(")\n"); buf.append( "ALTER TABLE " + tableName + " ADD CONSTRAINT " + tableName + " PRIMARY KEY (PKEY)\n"); // then insert rows SimpleFeature feature; FeatureId id; while (reader.hasNext()) { buf.append("INSERT INTO ").append(tableName).append("("); feature = reader.next(); buf.append(StringUtils.join(fieldNames, ", ")); buf.append(") "); buf.append("VALUES ("); Collection<Property> properties = feature.getProperties(); String[] values = new String[size]; int valueIndex = 0; for (Property prop : properties) { Object value = prop.getValue(); if (value instanceof Geometry) { // use wkt writer to convert geometry to string, so third dimension can be supported // if present. Geometry geom = (Geometry) value; value = new WKTWriter(geom.getCoordinate().z == Double.NaN ? 2 : 3).write(geom); } if (value == null || value.toString().equalsIgnoreCase("null")) { values[valueIndex] = "null"; } else if (prop.getType() instanceof GeometryType) { int srid = getSrid(((GeometryType) prop.getType())); StringBuffer geomValue = new StringBuffer(parser + "('"); geomValue.append(value).append("'"); if (srid > -1) { // attach srid geomValue.append(", ").append(srid); } geomValue.append(")"); values[valueIndex] = geomValue.toString(); } else if (prop.getType().getBinding().getSimpleName().equalsIgnoreCase("DATE")) { values[valueIndex] = "TO_DATE('" + value + "', 'yyyy-MM-dd')"; } else { values[valueIndex] = "'" + value + "'"; } valueIndex++; } id = feature.getIdentifier(); // insert primary key values[valueIndex] = "'" + id.toString() + "'"; buf.append(StringUtils.join(values, ",")); buf.append(")\n"); } } buf.append(spatialIndex.toString()); spatialIndex.delete(0, spatialIndex.length()); if (buf.length() > 0) { this.sql = buf.toString(); } } }
public void post(Representation entity) { InputStream input = null; try { input = getRequest().getEntity().getStream(); final GeoGIG ggit = getGeogig(getRequest()).get(); final Reader body = new InputStreamReader(input); final JsonParser parser = new JsonParser(); final JsonElement conflictJson = parser.parse(body); if (conflictJson.isJsonObject()) { final JsonObject conflict = conflictJson.getAsJsonObject(); String featureId = null; RevFeature ourFeature = null; RevFeatureType ourFeatureType = null; RevFeature theirFeature = null; RevFeatureType theirFeatureType = null; JsonObject merges = null; if (conflict.has("path") && conflict.get("path").isJsonPrimitive()) { featureId = conflict.get("path").getAsJsonPrimitive().getAsString(); } Preconditions.checkState(featureId != null); if (conflict.has("ours") && conflict.get("ours").isJsonPrimitive()) { String ourCommit = conflict.get("ours").getAsJsonPrimitive().getAsString(); Optional<NodeRef> ourNode = parseID(ObjectId.valueOf(ourCommit), featureId, ggit); if (ourNode.isPresent()) { Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().objectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); ourFeature = (RevFeature) object.get(); object = ggit.command(RevObjectParse.class) .setObjectId(ourNode.get().getMetadataId()) .call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType); ourFeatureType = (RevFeatureType) object.get(); } } if (conflict.has("theirs") && conflict.get("theirs").isJsonPrimitive()) { String theirCommit = conflict.get("theirs").getAsJsonPrimitive().getAsString(); Optional<NodeRef> theirNode = parseID(ObjectId.valueOf(theirCommit), featureId, ggit); if (theirNode.isPresent()) { Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().objectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); theirFeature = (RevFeature) object.get(); object = ggit.command(RevObjectParse.class) .setObjectId(theirNode.get().getMetadataId()) .call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType); theirFeatureType = (RevFeatureType) object.get(); } } if (conflict.has("merges") && conflict.get("merges").isJsonObject()) { merges = conflict.get("merges").getAsJsonObject(); } Preconditions.checkState(merges != null); Preconditions.checkState(ourFeatureType != null || theirFeatureType != null); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder( (SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type() : theirFeatureType.type())); ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType : ourFeatureType).sortedDescriptors(); for (Entry<String, JsonElement> entry : merges.entrySet()) { int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors); if (descriptorIndex != -1 && entry.getValue().isJsonObject()) { PropertyDescriptor descriptor = descriptors.get(descriptorIndex); JsonObject attributeObject = entry.getValue().getAsJsonObject(); if (attributeObject.has("ours") && attributeObject.get("ours").isJsonPrimitive() && attributeObject.get("ours").getAsBoolean()) { featureBuilder.set( descriptor.getName(), ourFeature == null ? null : ourFeature.getValues().get(descriptorIndex).orNull()); } else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) { featureBuilder.set( descriptor.getName(), theirFeature == null ? null : theirFeature.getValues().get(descriptorIndex).orNull()); } else if (attributeObject.has("value") && attributeObject.get("value").isJsonPrimitive()) { JsonPrimitive primitive = attributeObject.get("value").getAsJsonPrimitive(); if (primitive.isString()) { try { Object object = valueFromString( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsString()); featureBuilder.set(descriptor.getName(), object); } catch (Exception e) { throw new Exception( "Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString()); } } else if (primitive.isNumber()) { try { Object value = valueFromNumber( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsNumber()); featureBuilder.set(descriptor.getName(), value); } catch (Exception e) { throw new Exception( "Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString()); } } else if (primitive.isBoolean()) { try { Object value = valueFromBoolean( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsBoolean()); featureBuilder.set(descriptor.getName(), value); } catch (Exception e) { throw new Exception( "Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString()); } } else if (primitive.isJsonNull()) { featureBuilder.set(descriptor.getName(), null); } else { throw new Exception( "Unsupported JSON type for attribute value (" + entry.getKey() + ")"); } } } } SimpleFeature feature = featureBuilder.buildFeature(NodeRef.nodeFromPath(featureId)); RevFeature revFeature = RevFeatureBuilder.build(feature); ggit.getRepository().stagingDatabase().put(revFeature); getResponse() .setEntity( new StringRepresentation(revFeature.getId().toString(), MediaType.TEXT_PLAIN)); } } catch (Exception e) { throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e); } finally { if (input != null) Closeables.closeQuietly(input); } }
/** * Write the feature into the stream. * * @param feature The feature * @param root * @throws XMLStreamException */ public Element writeFeature(final Feature feature, final Document rootDocument, boolean fragment) throws ParserConfigurationException { final Document document; if (rootDocument == null) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // then we have to create document-loader: factory.setNamespaceAware(false); DocumentBuilder loader = factory.newDocumentBuilder(); // creating a new DOM-document... document = loader.newDocument(); } else { document = rootDocument; } // the root element of the xml document (type of the feature) final FeatureType type = feature.getType(); final Name typeName = type.getName(); final String namespace = typeName.getNamespaceURI(); final String localPart = typeName.getLocalPart(); final Element rootElement; final Prefix prefix; if (namespace != null) { prefix = getPrefix(namespace); rootElement = document.createElementNS(namespace, localPart); rootElement.setPrefix(prefix.prefix); } else { rootElement = document.createElement(localPart); prefix = null; } // if main document set the xmlns if (!fragment) { rootElement.setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns:gml", "http://www.opengis.net/gml"); } final Attr idAttr = document.createAttributeNS(Namespaces.GML, "id"); idAttr.setValue(feature.getIdentifier().getID()); idAttr.setPrefix("gml"); rootElement.setAttributeNodeNS(idAttr); if (rootDocument == null) { document.appendChild(rootElement); } // write properties in the type order for (final PropertyDescriptor desc : type.getDescriptors()) { final Collection<Property> props = feature.getProperties(desc.getName()); for (Property a : props) { final Object valueA = a.getValue(); final PropertyType typeA = a.getType(); final Name nameA = a.getName(); final String nameProperty = nameA.getLocalPart(); String namespaceProperty = nameA.getNamespaceURI(); if (valueA instanceof Collection && !(typeA instanceof GeometryType)) { for (Object value : (Collection) valueA) { final Element element; if (namespaceProperty != null) { element = document.createElementNS(namespaceProperty, nameProperty); } else { element = document.createElement(nameProperty); } element.setTextContent(Utils.getStringValue(value)); if (prefix != null) { element.setPrefix(prefix.prefix); } rootElement.appendChild(element); } } else if (valueA instanceof Map && !(typeA instanceof GeometryType)) { final Map<?, ?> map = (Map) valueA; for (Entry<?, ?> entry : map.entrySet()) { final Element element; if (namespaceProperty != null) { element = document.createElementNS(namespaceProperty, nameProperty); } else { element = document.createElement(nameProperty); } final Object key = entry.getKey(); if (key != null) { element.setAttribute("name", (String) key); } element.setTextContent(Utils.getStringValue(entry.getValue())); if (prefix != null) { element.setPrefix(prefix.prefix); } rootElement.appendChild(element); } } else if (!(typeA instanceof GeometryType)) { String value = Utils.getStringValue(valueA); if (value != null || (value == null && !a.isNillable())) { if ((nameProperty.equals("name") || nameProperty.equals("description")) && !Namespaces.GML.equals(namespaceProperty)) { namespaceProperty = Namespaces.GML; LOGGER.warning( "the property name and description of a feature must have the GML namespace"); } final Element element; if (namespaceProperty != null) { element = document.createElementNS(namespaceProperty, nameProperty); } else { element = document.createElement(nameProperty); } if (value != null) { element.setTextContent(value); } if (prefix != null) { element.setPrefix(prefix.prefix); } rootElement.appendChild(element); } // we add the geometry } else { if (valueA != null) { final Element element; if (namespaceProperty != null) { element = document.createElementNS(namespaceProperty, nameProperty); } else { element = document.createElement(nameProperty); } if (prefix != null) { element.setPrefix(prefix.prefix); } Geometry isoGeometry = JTSUtils.toISO( (com.vividsolutions.jts.geom.Geometry) valueA, type.getCoordinateReferenceSystem()); Marshaller marshaller = null; try { marshaller = POOL.acquireMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); marshaller.marshal(OBJECT_FACTORY.buildAnyGeometry(isoGeometry), element); } catch (JAXBException ex) { LOGGER.log( Level.WARNING, "JAXB Exception while marshalling the iso geometry: " + ex.getMessage(), ex); } finally { if (marshaller != null) { POOL.release(marshaller); } } rootElement.appendChild(element); } } } } // writer.writeEndElement(); return rootElement; }
/** * Creates an {@link AttributeType} that matches the xsd type definition as much as possible. * * <p>The original type definition given by the {@link XSDTypeDefinition} is kept as * AttributeType's metadata stored as a "user data" property using <code>XSDTypeDefinition.class * </code> as key. * * <p>If it is a complex attribute, it will contain all the properties declared in the <code> * typeDefinition</code>, as well as all the properties declared in its super types. TODO: handle * the case where the extension mechanism is restriction. * * @param assignedName * @param typeDefinition * @return */ private AttributeType createType( final Name assignedName, final XSDTypeDefinition typeDefinition, CoordinateReferenceSystem crs, boolean anonymous) { AttributeType attType; // ///////// if (processingTypes.contains(assignedName)) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Recursion found for type " + assignedName + ". Proxying it."); } attType = createProxiedType( assignedName, typeDefinition, anonymous ? anonTypeRegistry : typeRegistry); return attType; } processingTypes.push(assignedName); // ////////// final XSDTypeDefinition baseType = typeDefinition.getBaseType(); AttributeType superType = null; if (baseType != null) { String targetNamespace = baseType.getTargetNamespace(); String name = baseType.getName(); if (name != null) { Name baseTypeName = new NameImpl(targetNamespace, name); superType = getAttributeType(baseTypeName, baseType, crs); } } else { LOGGER.warning(assignedName + " has no super type"); } if (typeDefinition instanceof XSDComplexTypeDefinition) { XSDComplexTypeDefinition complexTypeDef; complexTypeDef = (XSDComplexTypeDefinition) typeDefinition; boolean includeParents = true; List<XSDElementDeclaration> children = Schemas.getChildElementDeclarations(typeDefinition, includeParents); final Collection<PropertyDescriptor> schema = new ArrayList<PropertyDescriptor>(children.size()); XSDElementDeclaration childDecl; AttributeDescriptor descriptor; for (Iterator it = children.iterator(); it.hasNext(); ) { childDecl = (XSDElementDeclaration) it.next(); try { descriptor = createAttributeDescriptor(complexTypeDef, childDecl, crs); } catch (NoSuchElementException e) { String msg = "Failed to create descriptor for '" + childDecl.getTargetNamespace() + "#" + childDecl.getName() + " from container '" + typeDefinition.getTargetNamespace() + "#" + typeDefinition.getName() + "'"; NoSuchElementException nse = new NoSuchElementException(msg); nse.initCause(e); throw nse; } schema.add(descriptor); } if (includeAttributes) { for (XSDAttributeUse attgcontent : complexTypeDef.getAttributeUses()) { XSDAttributeDeclaration att = attgcontent.getContent(); descriptor = createAttributeDescriptor( getXmlAttributeType(), null, new NameImpl(null, "@" + att.getName()), 0, 1, false, null); schema.add(descriptor); } } // set substitution group for descriptors here for (XSDElementDeclaration elemDecl : children) { if (elemDecl.isElementDeclarationReference()) { elemDecl = elemDecl.getResolvedElementDeclaration(); } PropertyDescriptor att = null; for (PropertyDescriptor desc : schema) { if (desc.getName().getLocalPart().equals(elemDecl.getName()) && desc.getName().getNamespaceURI().equals(elemDecl.getTargetNamespace())) { att = desc; break; } } setSubstitutionGroup(complexTypeDef, elemDecl, att, crs); } attType = createComplexAttributeType(assignedName, schema, complexTypeDef, superType); } else { Class<?> binding = String.class; boolean isIdentifiable = false; boolean isAbstract = false; List<Filter> restrictions = Collections.emptyList(); InternationalString description = null; attType = typeFactory.createAttributeType( assignedName, binding, isIdentifiable, isAbstract, restrictions, superType, description); } attType.getUserData().put(XSDTypeDefinition.class, typeDefinition); processingTypes.pop(); // even if the type is anonymous, it still has to be registered somewhere because // it's needed for the proxied types to find them. That's why we have 2 registries, // typeRegistry // and anonTypeRegistry. TypeRegistry is the global one, since anonymous types are meant to // be // local and shouldn't be searchable. register(attType, anonymous); return attType; }
private void setSubstitutionGroup( XSDComplexTypeDefinition container, XSDElementDeclaration elemDecl, PropertyDescriptor descriptor, CoordinateReferenceSystem crs) { if (descriptor.getUserData().get("substitutionGroup") != null) { // this has been done before return; } List<AttributeDescriptor> substitutionGroup = new ArrayList<AttributeDescriptor>(); descriptor.getUserData().put("substitutionGroup", substitutionGroup); int minOccurs = Schemas.getMinOccurs(container, elemDecl); int maxOccurs = Schemas.getMaxOccurs(container, elemDecl); boolean nillable = elemDecl.isNillable(); Iterator substitutions = elemDecl.getSubstitutionGroup().iterator(); XSDElementDeclaration sub; while (substitutions.hasNext()) { sub = (XSDElementDeclaration) substitutions.next(); if (!(sub.getName().equals(elemDecl.getName())) || !(sub.getTargetNamespace().equals(elemDecl.getTargetNamespace()))) { Name elemName = Types.typeName(sub.getTargetNamespace(), sub.getName()); AttributeType type = getTypeOf(sub, crs); if (type != null) { substitutionGroup.add( createAttributeDescriptor(type, crs, elemName, minOccurs, maxOccurs, nillable, null)); } } } XSDTypeDefinition typeDef = elemDecl.getType(); if (typeDef instanceof XSDComplexTypeDefinition) { Name typeName = Types.typeName(typeDef.getTargetNamespace(), typeDef.getName()); AttributeType attType = typeRegistry.get(typeName); if (!processingTypes.contains(typeName)) { // ignore processingTypes to avoid endless recursion if (attType == null || attType instanceof AbstractLazyComplexTypeImpl) { // type is not yet registered or it's a lazy type from foundation types // recreate lazy type to ensure everything is loaded // it will eventually call this method so substitution groups will be set then LOGGER.finest("Creating attribute type " + typeName); createType(typeName, typeDef, crs, false); LOGGER.finest("Registering attribute type " + typeName); } else if (attType instanceof ComplexType) { // ensure substitution groups are set for children including non lazy foundation // types ComplexType complexType = (ComplexType) attType; Collection<PropertyDescriptor> children = complexType.getDescriptors(); List<XSDParticle> childParticles = Schemas.getChildElementParticles(typeDef, true); for (XSDParticle particle : childParticles) { XSDElementDeclaration element = (XSDElementDeclaration) particle.getContent(); if (element.isElementDeclarationReference()) { element = element.getResolvedElementDeclaration(); } PropertyDescriptor childDesc = null; for (PropertyDescriptor desc : children) { if (desc.getName().getLocalPart().equals(element.getName()) && desc.getName().getNamespaceURI().equals(element.getTargetNamespace())) { childDesc = desc; break; } } if (childDesc != null) { setSubstitutionGroup((XSDComplexTypeDefinition) typeDef, element, childDesc, crs); } } } } } }