On 18/11/09 15:28, Ben Caradoc-Davies wrote:
> If you want an optional element, define a top-level feature type, and
> declare inside it an optional element in the xs:sequence using
> minOccurs="0".

Sorry, that was rather unclear.

If you want an optional element, define a top-level feature type and 
declare your geometry property inside it an optional element in the 
xs:sequence using minOccurs="0".

For example Schema2Test.xml:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.example.org/example";
xmlns:xs="http://www.w3.org/2001/XMLSchema"; 
xmlns:gml="http://www.opengis.net/gml";
xmlns:ex="http://www.example.org/example"; elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import namespace="http://www.opengis.net/gml";
schemaLocation="http://schemas.opengis.net/gml/3.1.1/base/gml.xsd"; />
<xs:element name="MyData" type="ex:MyDataType" />
<xs:complexType name="MyDataType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geometry" type="gml:GeometryPropertyType"
minOccurs="0" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>

Then code like this:

String namespace = "http://www.example.org/example";;
String schemaFileName = "Schema2Test.xml";
String wkt = "LINESTRING (60 380, 60 20, 200 400, 280 20, 360 400, 420 
20, 500 400, 580 20, 620 400)";
Geometry geometry = (new WKTReader2()).read(wkt);
// build the geometry attribute inside out: type, descriptor, attribute
GeometryType geometryType = new GeometryTypeImpl(new NameImpl(GML.NAMESPACE,
"GeometryPropertyType"), Geometry.class, null, false, false, 
Collections.EMPTY_LIST,
XSSchema.ANYTYPE_TYPE, null);
final GeometryDescriptor geometryDescriptor = new 
GeometryDescriptorImpl(geometryType,
new NameImpl(namespace, "geometry"), 0, 1, true, null);
final GeometryAttribute geometryAttribute = new 
GeometryAttributeImpl(geometry,
geometryDescriptor, null);
// build the feature inside out: type, descriptor, feature
FeatureType featureType = new FeatureTypeImpl(new NameImpl(namespace, 
"MyData"),
new ArrayList<PropertyDescriptor>() {
{
add(geometryDescriptor);
}
}, geometryDescriptor, false, Collections.EMPTY_LIST,
GMLSchema.ABSTRACTFEATURETYPE_TYPE, null);
AttributeDescriptor featureDescriptor = new 
AttributeDescriptorImpl(featureType,
new NameImpl(namespace, "MyData"), 0, -1, false, null);
Feature feature = new FeatureImpl(new ArrayList<Property>() {
{
add(geometryAttribute);
}
}, featureDescriptor, new FeatureIdImpl("my-data.1"));
// configure encoder and encode
Configuration configuration = new ApplicationSchemaConfiguration(namespace,
Schema2Test.class.getResource(schemaFileName).toExternalForm());
// you might not want boundedBy encoded on your feature
configuration.getProperties().add(org.geotools.gml2.GMLConfiguration.NO_FEATURE_BOUNDS);
org.geotools.xml.Encoder encoder = new 
org.geotools.xml.Encoder(configuration);
encoder.encode(feature, new QName(namespace, "MyData"), System.out);

Then you get something like this:

<?xml version="1.0" encoding="UTF-8"?>
<ex:MyData gml:id="my-data.1" xmlns:ex="http://www.example.org/example";
xmlns:gml="http://www.opengis.net/gml";>
<ex:geometry>
<gml:LineString>
<gml:posList>60.0 380.0 60.0 20.0 200.0 400.0 280.0 20.0 360.0 400.0
420.0 20.0 500.0 400.0 580.0 20.0 620.0 400.0</gml:posList>
</gml:LineString>
</ex:geometry>
</ex:MyData>

Yes, the code to create it is very horrible. You might like to look in 
the app-schema module, in modules/unsupported/app-schema/app-schema, 
which gives some degree of application schema support, intended to allow 
mapping from simple features to application schemas.

I'd welcome any suggestions about easier ways to encode an arbitrary 
complex type defined in a schema.

Kind regards,

-- 
Ben Caradoc-Davies <ben.caradoc-dav...@csiro.au>
Software Engineer, CSIRO Earth Science and Resource Engineering
Australian Resources Research Centre
26 Dick Perry Ave, Kensington WA 6151, Australia

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to