本节将阐述如何创建和使用模式,并将解释一些模式管理相关的问题,例如如何使用FDO要素模式来描述空间要素。
1.1.1 模式概述
FDO要素模式提供了一种对空间要素的逻辑描述机制,而FDO Provider负责将要素模式映射为底层的物理数据结构。要素模式是空间要素数据的逻辑视图,它完全独立于数据存储,一切FDO中的数据操作都是针对逻辑要素模式中定义的类以及关系。FDO要素模式在某种程度上是基于OpenGIS和ISO要素模型的子集的,它既可以描述非空间要素,也可以描述空间要素。
1) 基本属性(Base Properties)
基本属性是在FDO API或某个FDO Provider中预定义的属性,要素模式中的所有类都支持基本属性,例如模式中所有的类都有“ClassName”和“SchemaName”两个基本属性。表9‑2列出了FDO API中预定义的基本属性。
属性名 | 是否必须 | 描述 |
SchemaName | 是 | 类实例所属的模式名,只读字符串属性。 |
ClassName | 是 | 实例的类名,只读字符串属性。 |
RevisionNumber | 否 | 实例的修订号,只读64位整型属性。有些Provider用该属性来支持乐观锁。 |
表 9‑2 FDO API预定义的基本属性
2) 模式间的交叉引用(Cross-Schema References)
一些FDO Provider支持多个模式,于是就引出了模式间交叉引用的概念,对于这些Provider一个模式中的类可以是从另一个模式中的类继承而来的,关联于另一个模式中的类,或者包含了一个基于另一个模式中的类的对象属性。
3) 模式类导航(Parenting in the Schema Classes)
FDO API定义的要素模式对象模型支持完全导航,也就是说,从任何一个FdoFeatureSchema对象的模式元素都就可以向上导航到该对象的根节点,而从根节点也可以向下导航到任何要素模式元素。这个功能是在FdoSchemaElement抽象基类中定义的。要实现完全导航,就要求子元素在插入到父元素时记录父元素的ID。
4) 物理映射(Physical Mappings)
每个要素Provider都要负责逻辑要素模式和物理存储之间的物理映射,其中一些Provider甚至要对映射提供多级别的控制,例如基于RDBMS的要素Provider可能会允许用户为类和属性指定表名和列名。由于各个Provider的映射差别很大,于是FDO API提供了一些接口,由每个Provider分别实现这些接口。这些类有FdoPhysicalSchemaMapping,FdoPhysicalClassMapping,FdoPhysicalPropertyMapping和FdoPhysicalElementMapping。
1.1.2 模式的组成
FDO使用类FdoFeatureSchema表示一个模式,FdoClass表示一个类,FdoFeatureClass表示一个要素类。每个模式由一个或多个类或要素类组成,类用于表示非空间数据,要素类用于表示空间数据。每个类和要素类由一个或多个属性组成,要素类必须有一个几何属性。
对于大部分Provider说来,每个类和要素类需要有一个关键属性(Identity Property)来加以标识每一个要素,关键属性可以由一个属性或多个属性组成。如果一个类和要素类是从某个类或要素类继承而来,关键属性只能在基类中定义,子类的关键属性只能从基类中继承而来。也就是说,任何一个有父类的类或要素类都不能设置关键属性
图 9‑3 FDO模式元素类图
1.1.3 模式的使用
模式管理主要包括模式的创建、模式的描述和模式的修改三种操作。
1) 创建模式
以下是创建模式所需的基本步骤(其中一些步骤可以省略,也有一些步骤之间可以交换顺序):
l 调用FdoFeatureSchema::Create("SchemaName", "FeatureSchemaDescription")方法创建一个模式。
l 调用FdoFeatureSchema::GetClasses()方法得到一个类的集合。
l 调用FdoClass::Create("className","classDescription")或者FdoFeatureClass::Create(
"className","classDescription")方法得到FdoClass或FdoFeatureClass类的实例。
l 调用FdoClassCollection::Add(class)方法将FdoClass或FdoFeatureClass类的实例加入到GetClasses()得到的类的集合中。
l 调用FdoGeometricPropertyDefinition::Create("name","Description")方法来创建FdoGeometryProperty的实例。
l 调用FdoDataPropertyDefinition::Create("name","Description")方法来创建FdoDataProperty的实例。
l 调用FdoObjectPropertyDefinition::Create("name","Description")方法来创建FdoObjectProperty的实例。
l 调用FdoClassDefinition::GetProperties()和Add(property)方法把以上的属性实例加入到类定义中。
l 调用FdoClassDefinition::GetIdentityProperties()和Add(Property)方法来添加FdoClass或FdoFeatureClass的ID。
l 调用FdoIApplySchemaCommand::SetFeatureSchema(featureSchema)方法为IFdoApplySchemaCommand设置模式对象。
l 用FdoAssociationPropertydefinition类描述两类之间的关系,当然,类已经在要素模式中定义并且不能是抽象类。
l 最后调用FdoIApplySchemaCommand::Execute()方法来应用对要素schema的改动。
2) 描述模式
为了得到现存模式的信息,可以调用FdoIDescribeSchema::Execute()方法得到一个FdoFeatureSchemaCollection实例,它包含了Data Store中的所有FdoFeatureSchema,并且可以通过它得到包括FdoFeatureSchema、FdoClass和FdoFeatureClass在内的任何模式对象,以及它们各自的属性。以下方法返回能够得到模式对象信息的主要集合:
l FdoFeatureSchema::GetClasses()方法得到FdoFeatureClasses。
l FdoClassDefinition::GetProperties()方法得到FdoPropertyDefinitionCollection。
l FdoClassDefinition::GetBaseProperties()方法得到从基类继承而来的FdoPropertyDefinitionCollection。
3) 修改模式
在修改模式前,一般会通过描述模式先得到一个FdoFeatureSchemaCollection实例,然后再修改此实例,最后调用方法FdoIApplySchema::Execute()。
插入操作非常简单,只需要把模式元素插入到相应的集合。删除操作可以通过如下任意一种办法:
l 调用方法FdoSchemaElement::Delete(),该方法只是把元素做一个删除标记,当该修改被接受(一般是通过FdoIApplySchema)时才真正删除掉。
l 调用FdoSchemaCollection::Remove()或FdoSchemaCollection::RemoveAt()方法从相应的集合删除元素。
模式元素都有一个状态标记,该标记可以通过调用FdoSchemaElement::GetElementState()得到。状态不能直接设置,它只能随着以下的操作而改变:
l Unchanged:当schema原型通过FdoIDescribeSchema得到时,所有的元素被初始化为Unchanged状态。
l Detached:调用特有集合的删除元素操作,该元素将被标记为Detached。
l Deleted:调用元素的Delete()方法把该元素标记为Deleted。
l Added:把元素添加到一个集合,该元素将被标记为Added。
l Modified:当添加或删除一个元素的子元素,该元素将被标记为Modified。另外,一旦一个元素被标记为Modified,那么包含它的元素也要被标记为Modified。例如,SchemaAttributeDictionary中的“Class3”元素添加了一个新值,那么SchemaAttributeDictionary和“Class3”元素都将被标记为Modified。
状态标记一直保留到改动被接受为止,也就是说,保留到执行了IApplySchema为止。此时,所有标记为Deleted的元素都将被清除,而所有其他元素都将被标记为Unchanged。
FdoFeatureSchema的回滚机制可以让模式返回到上一次接受变动的状态。例如,一个原型通过FdoIDescribeSchema做了添加类,删除属性等操作,所有这些操作都将通过调用FdoFeatureSchema::RejectChanges()方法被拒绝,所有元素的状态又重新被标记为Unchanged。该方法的相反操作是FdoFeatureSchema::AcceptChanges(),一般情况下,该方法只有在FDO Provider在执行FdoIApplySchema::Execute()命令时才被调用到。
1.1.4 FDO XML格式
FDO支持使用GML(Geography Markup Language)格式的描述模式。GML(Geography Markup Language)即地理标识语言,是一种使用XML语法描述地理空间要素的标记语言,它由OGC于1999年提出,它由OGC于1999年提出,并得到了许多公司的大力支持。
FdoFeatureSchema和FdoFeatureSchemaCollection两个类都实现了FdoXmlSerializable接口,这意味着FDO模式可以写入到一个GML格式XML文件;FdoFeatureSchemaCollection还实现了FdoXmlDeserializable接口,这意味着也可以从一个GML格式XML文件得到FDO模式。
在指定模式覆盖时,往往需要手工编写GML格式XML文件指定模式和模式覆盖,这时我们必须了解GML的语法。表 9‑3显示了FDO模式元素和GML元素之间的对应关系,其中使用双引号括起来的尖括号部分的内容和使用花括号括起来的内容代表需要用户替换的内容。
FDO模式元素 | GML模式片段 |
FeatureSchema | <xs:schema xmlns:xs=“http://www.w3.org/2001/XMLSchema” targetNamespace=“http://<customer_url>/<FeatureSchemaName>“ xmlns:fdo=“http://fdo.osgeo.org/isd/schema” xmlns:gml=“http://www.opengis.net/gml” xmlns:<FeatureSchemaName>=“http://<customer_url>/<FeatureSchemaName>“ elementFormDefault=“qualified” attributeFormDefault=“unqualified”> { 可选元素 xs:import 元素用来使schema生效 <xs:import namespace=“http://fdo.osgeo.org/schema” schemaLocation=“<FDO SDK安装位置>/docs/XmlSchema/FdoDocument.xsd”/> } {<每个schema中只能有一个 xs:element 和/或 xs:complexType >} </xs:schema> |
ClassDefinition (带有主键属性) | <xs:element name=“<className>“ type=“<className>Type” abstract=“<true | false>“ substitutionGroup=“gml:_Feature”> <xs:key name=“<className>Key”> <xs:selector xpath=“.//<className>“/> <xs:field xpath=“<identityProperty1Name>“/> <xs:field xpath=“...”/> <xs:field xpath=“<identityProperty<n>Name>“ </xs:key> </xs:element> |
FeatureClass | <xs:element ...请参考 ClassDefinition (带有主键属性)... </xs:element> <xs:complexType name=“<className>Type” abstract=“<true | false>“/> { 请参考FeatureClass.GeometryProperty } <xs:complexContent> <xs:extension base=“{baseClass}?{baseClass.schema.name}:{baseClass.name} : ‘gml:AbstractFeatureType’ “ > <xs:sequence> {属性列表请参考DataProperty和Geometric属性} </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> |
FeatureClass. GeometryProperty | <!--这些是xs:complexType元素的属性--> fdo:geometryName=“<geometryPropertyName>“ fdo:geometricTypes=“<list of FdoGeometricTypes>“ fdo:geometryReadOnly=“<true | false>“ fdo:hasMeasure=“<true | false>“ fdo:hasElevation=“<true | false>“ fdo:srsName=“<spatialContextName>“/> |
DataProperty (decimal或string类型) | <!--minOccurs属性仅当值是1的时候才生成;Default属性仅当存在默认值时才生成;fdo:readOnly属性仅当值是1的时候才生成。--> <xs:element name=“<propertyName>“ minOccurs=“{isNullable ? 0 : 1}” default=“<defaultValue>“ fdo:readOnly=“<true | false>“> <xs:simpleType> {请参考 DataType String或DataType Decimal } </xs:simpleType> </xs:element> |
DataProperty (其他类型) | <xs:element name=“<propertyName>“ type=“<datatype>“ minOccurs=“{isNullable ? 0 : 1}” default=“<defaultValue>“ fdo:readOnly=“<true | false>“> </xs:element> |
DataType String | <xs:restriction base=“xs:string”> <xs:maxLength value=“<length>“/> </xs:restriction> |
DataType Decimal | <xs:restriction base=“xs:decimal”> <xs:totalDigits value=“<precision>“/> <xs:fractionDigits value=“<scale>“/> </xs:restriction> |
GeometricProperty
| <xs:element name=“<propertyName>“ type=“gml:AbstractGeometryType” fdo:geometryName=“<propertyName>“ fdo:geometricTypes=“<list of FdoGeometricTypes>“ fdo:geometryReadOnly=“<true | false>“ fdo:hasMeasure=“<true | false>“ fdo:hasElevation=“<true | false>“ fdo:srsName=“<spatialContextName>“/> > </xs:element> |
MetaData | <!--在xs:schema元素中为FeatureSchema引用的模式--> <xs:annotation> <xs:documentation> {对FdoFeatureSchema::Create()的描述} </xs:documentation> </xs:annotation> <!--在xs:schema元素中为DataProperty引用的样式--> <xs:annotation> <xs:documentation> {对FdoDataPropertyDefinition::Create()的描述} </xs:documentation> </xs:annotation> <!--在xs:element元素为非要素GeometricProperty引用的模式--> <xs:annotation> <xs:documentation> {对FdoGeometricPropertyDefinition::Create()的描述} </xs:documentation> </xs:annotation> <!--在xs:complexType元素为FeatureClass引用的样式--> <xs:annotation> <xs:documentation> {对FdoFeatureClass::Create()的描述} </xs:documentation> <xs:appinfo source=“<uri>“/> <xs:documentation> {对FdoGeometricPropertyDefinition::Create()的描述} </xs:documentation> </xs:annotation> |
表 9‑3 FDO模式元素和GML模式片段的对应关系
表 9‑4显示了Map FDO数据类型和GML数据类型对照关系。
FDO数据类型 | GML数据类型 |
Boolean | xs:boolean |
Byte | fdo:Byte |
DateTime | xs:dateTime |
Double | xs:double |
Int16 | fdo:Int16 |
Int32 | fdo:Int32 |
Int64 | fdo:Int64 |
Single | xs:float |
BLOB | xs:base64Binary |
CLOB | xs:string |
表 9‑4 FDO数据类型和GML数据类型对照表
1.1.5 GML 模式文件的创建
以下示例为“Buildings”要素定义了一个表格,这是Open GIS联盟的文档98-046r1中的一个示例。本节中,我们将通过为要素类“Buildings”创建GML模式文件来展示如何创建GML模式文件。
<ogc-sfsql-table> <table-definition> <name>buildings</name> <column-definition> <name>fid</name> <type>INTEGER</type> <constraint>NOT NULL</constraint> <constraint>PRIMARY KEY</constraint> </column-definition> <column-definition> <name>address</name> <type>VARCHAR(64)</type> </column-definition> <column-definition> <name>position</name> <type>POINT</type> </column-definition> <column-definition> <name>footprint</name> <type>POLYGON</type> <column-definition> </table-definition> |
1.1.5.1 添加模式
让我们从一个基本的GML框架开始添加一个模式。
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema targetNamespace=“http://<customer_url>/<FeatureSchemaName>“ xmlns:fdo=http://fdo.osgeo.org/schema xmlns:gml=http://www.opengis.net/gml xmlns:<FeatureSchemaName>=“http://<customer_url>/<FeatureSchemaName>“ elementFormDefault=“qualified” attributeFormDefault=“unqualified”> <xs:annotation> <xs:documentation>{对FdoFeatureSchema::Create()的描述}</xs:documentation> </xs:annotation> {<每个类中只能有一个 xs:element 和/或 xs:complexType >} </xs:schema> |
通过如下的替换,可以设置要素模式的名称和描述。
l 用“fdo_customer”代替“<customer_url>”。
l 用“OGC980461FS”代替“<FeatureSchemaName>”,从而设置要素模式名称。
l 设置要素模式描述:用“OGC Simple Features Specification for SQL” 代替“{对FdoFeatureSchema::Create()的描述}”,从而设置要素模式描述。
1.1.5.2 添加要素类
在以上GML模式的基础上,让我们接着添加一个要素类。
<xs:schema xmlns:xs=“http://www.w3.org/2001/XMLSchema” targetNamespace=“http://fdo_customer/OGC980461FS” xmlns:fdo=“http://fdo.osgeo.org/schema” xmlns:gml=“http://www.opengis.net/gml” xmlns:OGC980461FS=“http://fdo_customer/OGC980461FS” elementFormDefault=“qualified” attributeFormDefault=“unqualified”> <xs:annotation> <xs:documentation>OGC Simple Features Specification for SQL</xs:documentation> </xs:annotation> <xs:element name=“<className>“ type=“<className>Type” abstract=“<true | false>“ substitutionGroup=“gml:_Feature”> <xs:key name=“<className>Key”> <xs:selector xpath=“.//<className>“/> <xs:field xpath=“<identityPropertyName>“/> </xs:key> </xs:element> <xs:complexType name=“<className>Type” abstract=“<true | false>“/> fdo:geometryName=“<geometryPropertyName>“ fdo:geometricTypes=“<list of FdoGeometricTypes>“ fdo:geometryReadOnly=“<true | false>“ fdo:hasMeasure=“<true | false>“ fdo:hasElevation=“<true | false>“ fdo:srsName=“<spatialContextName>“/>> <xs:annotation> <xs:documentation> {对FdoFeatureClass::Create()的描述} </xs:documentation> <xs:appinfo source=“<uri>“/> <xs:documentation> {对FdoGeometricPropertyDefinition::Create()的描述} </xs:documentation> </xs:annotation> <xs:complexContent> <xs:extension base=“{baseClass} ? {baseClass.schema.name}:{baseClass.name} : ‘gml:AbstractFeatureType’ “> <xs:sequence> {属性类表;请参考 DataProperty, GeometricProperty} </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> </xs:schema> |
通过如下的替换,可以添加一个要素类,并且设置了这个要素类的几何属性和主键属性。
l 用“buildings”代替“<className>”,从而设置要素类的名称:。
l 将“xs:element”元素的“abstract”属性置为“false”。
l 用“fid”代替“<identityPropertyName>”,从而设置要素类的主键属性名称。
l 将“xs:complexType”元素的“abstract”属性置为false。
l 用“footprint”代替“<geometryPropertyName>”,从而设置要素类的几何属性名称。
l 用“surface”代替“<list of FdoGeometricTypes>”,从而设置几何属性的类型。
l 将“fdo:geometryReadOnly”,“fdo:hasMeasure”和“fdo:hasElevation”都置为“false”。
l 用“SC_0”代替“<spatialContextName>”,从而设置要素相关联的空间上下文。
l 用“OGC 98-046r1 buildings”代替“{对FdoFeatureClass::Create()的描述}”,从而设置要素类的描述。
l 用“http://fdo.osgeo.org/schema”代替“<uri>”。
l 用“a polygon defines a building perimeter”代替“{对FdoGeometricPropertyDefinition::Create()的描述}”,从而设置几何属性的描述。
l 该类不存在基类,因此将“xs:extension”元素的“base”属性置为‘gml:AbstractFeatureType’。
1.1.5.3 添加属性
在以上GML模式的基础上,我们再为要素类添加一个名为“fid”的整型数据属性,它已经作为关键属性加入到xs:key元素中了,还将添加名为“name”的数据属性,以及一个名为“position”的几何属性。
<xs:schema xmlns:xs=“http://www.w3.org/2001/XMLSchema” targetNamespace=“http://fdo_customer/OGC980461FS” xmlns:fdo=“http://fdo.osgeo.org/schema” xmlns:gml=“http://www.opengis.net/gml” xmlns:OGC980461FS=“http://fdo_customer/OGC980461FS” elementFormDefault=“qualified” attributeFormDefault=“unqualified” > <xs:annotation> <xs:documentation> OGC Simple Features Specification for SQL </xs:documentation> </xs:annotation> <xs:element name=“buildings” type=“buildingsType” abstract=“false” substitutionGroup=“gml:_Feature”> <xs:key name=“buildingsKey”> <xs:selector xpath=“.//buildings”/> <xs:field xpath=“fid”/> </xs:key> </xs:element> <xs:complexType name=“buildingsType” abstract=“false” fdo:geometryName=“footprint” fdo:geometricTypes=“surface” fdo:geometryReadOnly=“false” fdo:hasMeasure=“false” fdo:hasElevation=“alse” fdo:srsName=“SC_0” > <xs:annotation> <xs:documentation>OGC 98-046r1 buildings</xs:documentation> <xs:appinfo source=“http://fdo.osgeo.org/schema”/> <xs:documentation> a polygon defines the perimeter of a building </xs:documentation> </xs:annotation> <xs:complexContent> <xs:extension base=“gml:AbstractFeatureType”> <xs:sequence> <xs:element name=“<propertyName>“ type=“<datatype>“ minOccurs=“{isNullable ? 0 : 1}” default=“<defaultValue>“ fdo:readOnly=“<true | false>“ > <xs:annotation> <xs:documentation> {对FdoDataPropertyDefinition::Create()的描述} </xs:documentation> </xs:annotation> </xs:element> <xs:element name=“<propertyName>“ minOccurs=“{isNullable ? 0 : 1}” default=“<defaultValue>“ fdo:readOnly=“<true | false>“ > <xs:annotation> <xs:documentation> {对FdoDataPropertyDefinition::Create()的描述} </xs:documentation> </xs:annotation> <xs:simpleType> <xs:restriction base=“xs:string”> <xs:maxLength value=“<length>“/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name=“<propertyName>“ ref=“gml:_Geometry” fdo:geometryName=“<propertyName>“ fdo:geometricTypes=“<list of FdoGeometricTypes>“ fdo:geometryReadOnly=“<true | false>“ fdo:hasMeasure=“<true | false>“ fdo:hasElevation=“<true | false>“ fdo:srsName=“<spatialContextName>“> <xs:annotation> <xs:documentation> {对FdoGeometricPropertyDefinition::Create()的描述} </xs:documentation> </xs:annotation> </xs:element> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> </xs:schema> |
l 用“fid”代替第一个“<propertyName>”,从而设置数据属性“fid”。
l 用“fdo:int32”代替“<dataType>”,从而设置数据属性“fid”的数据类型。
l 不必添加“minOccurs”和“default”属性,因为“minOccurs”的值就是默认值0,也没有“<defaultValue>”。
l 将“fid”属性的“fdo:readOnly”置为“false”。
l 将“fid”属性的“xs:documentation”内容置为“feature id”。
l 用“address”代替第二个“<propertyName>”,从而设置数据属性“address”。
l 不必添加“minOccurs”和“default”属性,因为“minOccurs”的值就是默认值0,也没有“<defaultValue>”。
l 将“address”属性的“fdo:readOnly”置为“false”。
l 将“address”属性的“xs:documentation”内容置为“address of the building”。
l 用64代替“<length>”,从而设置数据属性“address”最大长度。
l 用“position”代替第三个“<propertyName>”,从而设置几何属性“position”。
l 用“point”代替“<list of FdoGeometricTypes>”,从而设置几何属性“position”的几何类型。
l 将“fdo:geometryReadOnly”,“fdo:hasMeasure”和“fdo:hasElevation”置为“false”。
l 用“SC_0”代替“<spatialContextName>”,从而设置几何属性“position”关联的空间上下文。
l 用“position of the building”代替“{对FdoGeometricPropertyDefinition::Create()的描述}”,从而设置几何属性“position”的描述。
1.1.5.4 最终结果
最终得到的包含Buildings要素的模式为:
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema” targetNamespace=”http://fdo_customer/OGC980461FS” xmlns:fdo=”http://fdo.osgeo.org/schema” xmlns:gml=”http://www.opengis.net/gml” xmlns:OGC980461FS=”http://fdo_customer/OGC980461FS” elementFormDefault=”qualified” attributeFormDefault=”unqualified”> <xs:annotation> <xs:documentation> OGC Simple Features Specification for SQL </xs:documentation> </xs:annotation> <xs:element name=”buildings” type=”buildingsType” abstract=”false” substitutionGroup=”gml:_Feature”> <xs:key name=”buildingsKey”> <xs:selector xpath=”.//buildings”/> <xs:field xpath=”fid”/> </xs:key> </xs:element> <xs:complexType name=”buildingsType” abstract=”false” fdo:geometryName=”footprint” fdo:geometricTypes=”surface” fdo:geometryReadOnly=”false” fdo:hasMeasure=”false” fdo:hasElevation=”false” fdo:srsName=”SC_0” > <xs:annotation> <xs:documentation>OGC 98-046r1 buildings</xs:documentation> <xs:appinfo source=”http://fdo.osgeo.org/schema”/> <xs:documentation> a polygon defines the perimeter of a building </xs:documentation> </xs:annotation> <xs:complexContent> <xs:extension base=”gml:AbstractFeatureType“> <xs:sequence> <xs:element name=”fid” type=”fdo:int32” fdo:readOnly=”false” > <xs:annotation> <xs:documentation>feature id </xs:documentation> </xs:annotation> </xs:element> <xs:element name=”address” fdo:readOnly=”false” > <xs:annotation> <xs:documentation>address of the building</xs:documentation> </xs:annotation> <xs:simpleType> <xs:restriction base=”xs:string”> <xs:maxLength value=”64”/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name=”position” ref=”gml:_Geometry” fdo:geometryName=”position” fdo:geometricTypes=”point” fdo:geometryReadOnly=”false” fdo:hasMeasure=”false” fdo:hasElevation=”false” fdo:srsName=”SC_0” > <xs:annotation> <xs:documentation>position of the building</xs:documentation> </xs:annotation> </xs:element> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> </xs:schema> |
1.1.6 模式管理示例
1.1.6.1 创建要素模式
该示例创建一个名为“SampleFeatureSchema”的FdoFeatureSchema对象,该对象定义了一个包含三个属性的类。该类和属性源自Open GIS联盟的文档98-046r1第10页,它定义了一个“Lakes”(湖)要素类。
<ogc-sfsql-table> <table-definition> <name>lakes</name> <column-definition> <name>fid</name> <type>INTEGER</type> <constgraint>NOT NULL</constraint> <constraint>PRIMARY KEY<constraint> </column-definition> <column-definition> <name>name</name> <type>VARCHAR(64)</type> </column-definition> <column-definition> <name>shore</name> <type>POLYGON</type> </column-definition> </table-definition> |
表“lakes”对应于名为“SampleFeatureClass”的FdoFeatureClass对象。列“fid”对应于名为“SampleIdentityDataProperty”的FdoDataPropertyDefinition对象;列“name”对应于名为“SampleNameDataProperty”的FdoDataPropertyDefinition对象;列“shore”对应于名为“SampleGeometricProperty”的FdoGeometricPropertyDefinition对象。
// 创建ApplySchema命令 FdoPtr<FdoIApplySchema> sampleApplySchema = (FdoIApplySchema *) connection->CreateCommand(FdoCommandType_ApplySchema); // 创建要素schema FdoPtr<FdoFeatureSchema> sampleFeatureSchema = FdoFeatureSchema::Create(L"SampleFeatureSchema", L"要素模式描述"); // 得到指向要素模式中类集合的指针,该指针用来向模式中加入类。 FdoPtr<FdoClassCollection> sampleClassCollection = sampleFeatureSchema->GetClasses(); // 创建一个要素类。例如,一个包含一个几何(geometric)属性的要素类 FdoPtr<FdoFeatureClass> sampleFeatureClass = FdoFeatureClass::Create(L"SampleFeatureClass", L"要素类描述"); sampleFeatureClass->SetIsAbstract(false); // 得到指向要素类中属性集合的指针,该指针用来向类中加入数据和其他属性。 FdoPtr<FdoPropertyDefinitionCollection> sampleFeatureClassProperties = sampleFeatureClass->GetProperties(); // 得到指向要素模式中类集合的指针,该指针用来向模式中加入类。 FdoPtr<FdoClassCollection> sampleClassCollection = sampleFeatureSchema->GetClasses(); // 得到指向要素类关键属性集合的指针,该指针用来向要素类中加入主键属性。 FdoPtr<FdoDataPropertyDefinitionCollection> sampleFeatureClassIdentityProperties = sampleFeatureClass->GetIdentityProperties(); // 创建一个Int32类型的数据属性,并将其设置为主键属性 FdoPtr<FdoDataPropertyDefinition> sampleIdentityDataProperty = FdoDataPropertyDefinition::Create(L"SampleIdentityDataProperty", L"关键数据属性描述"); sampleIdentityDataProperty->SetDataType(FdoDataType_Int32); sampleIdentityDataProperty->SetReadOnly(false); sampleIdentityDataProperty->SetNullable(false); sampleIdentityDataProperty->SetIsAutoGenerated(false); // 向sampleFeatureClass中加入关键属性 sampleFeatureClassProperties->Add(sampleIdentityDataProperty); sampleFeatureClassIdentityProperties->Add(sampleIdentityDataProperty); // 创建一个String类型的数据属性 FdoPtr<FdoDataPropertyDefinition> sampleNameDataProperty = FdoDataPropertyDefinition::Create(L"SampleNameDataProperty", L"数据属性描述"); sampleNameDataProperty->SetDataType(FdoDataType_String); sampleNameDataProperty->SetLength(64); sampleNameDataProperty->SetReadOnly(false); sampleNameDataProperty->SetNullable(false); sampleNameDataProperty->SetIsAutoGenerated(false); // 添加名称属性到sampleFeatureClass sampleFeatureClassProperties->Add(sampleNameDataProperty); // 创建一个几何属性 FdoPtr<FdoGeometricPropertyDefinition> sampleGeometricProperty = FdoGeometricPropertyDefinition::Create(L"SampleGeometricProperty", L"几何属性描述"); sampleGeometricProperty->SetGeometryTypes(FdoGeometricType_Surface); sampleGeometricProperty->SetReadOnly(false); sampleGeometricProperty->SetHasMeasure(false); sampleGeometricProperty->SetHasElevation(false); // 添加几何属性到sampleFeatureClass sampleFeatureClassProperties->Add(sampleGeometricProperty); // 将其标记为几何属性 sampleFeatureClass->SetGeometryProperty(sampleGeometricProperty); // 将要素类添加到schema sampleClassCollection->Add(sampleFeatureClass); // 为ApplySchema命令指定新建的模式,并且执行此命令 sampleApplySchema->SetFeatureSchema(sampleFeatureSchema); sampleApplySchema->Execute(); |
1.1.6.2 描述和导出模式
以下示例代码展示了如何描述一个模式并把它导出到XML文件。
// 创建DescribeSchema命令 FdoPtr<FdoIDescribeSchema> sampleDescribeSchema = (FdoIDescribeSchema *) connection->CreateCommand(FdoCommandType_DescribeSchema); // 执行DescribeSchema命令将得到Data Store中所有要素模式的集合 FdoPtr<FdoFeatureSchemaCollection> sampleFeatureSchemaCollection = sampleDescribeSchema->Execute(); // 从集合中找出想要导出的要素模式,然后将其写入XML文件,最后清空集合 sampleFeatureSchema = sampleFeatureSchemaCollection->FindItem(L”SampleFeatureSchema”); sampleFeatureSchema->WriteXml(L"SampleFeatureSchema.xml"); sampleFeatureSchemaCollection->Clear(); |
以下是在调用FdoFeatureSchema::WriteXml(...)方法后导出的XML文件的内容。
<?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://fdo_customer/SampleFeatureSchema" xmlns:fdo="http://fdo.osgeo.org/schema" xmlns:gml="http://www.opengis.net/gml" xmlns:SampleFeatureSchema="http://fdo_customer/ SampleFeatureSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:annotation> <xs:documentation>Sample Feature Schema Description </xs:documentation> <xs:appinfo source="http://fdo.osgeo.org/schema" /> </xs:annotation> <xs:element name="SampleFeatureClass" type="SampleFeatureSchema:SampleFeatureClassType" abstract="false" substitutionGroup="gml:_Feature"> <xs:key name="SampleFeatureClassKey"> <xs:selector xpath=".//SampleFeatureClass" /> <xs:field xpath="SampleIdentityDataProperty" /> </xs:key> </xs:element> <xs:complexType name="SampleFeatureClassType" abstract="false" fdo:geometryName="SampleGeometricProperty" fdo:hasMeasure="false" fdo:hasElevation="false" fdo:srsName="SC_0" fdo:geometricTypes="surface"> <xs:annotation> <xs:documentation>Sample Feature Class Description </xs:documentation> <xs:appinfo source="http://fdo.osgeo.org/schema" /> <xs:documentation>Sample Geometric Property Description</xs:documentation> </xs:annotation> <xs:complexContent> <xs:extension base="gml:AbstractFeatureType"> <xs:sequence> <xs:element name="SampleIdentityDataProperty" default="" type="fdo:int32"> <xs:annotation> <xs:documentation> Sample Identity Data Property Description </xs:documentation> </xs:annotation> </xs:element> <xs:element name="SampleNameDataProperty" default=""> <xs:annotation> <xs:documentation> Sample Name Data Property Description </xs:documentation> </xs:annotation> <xs:simpleType> <xs:restriction base="xs:string"> <xs:maxLength value="64" /> </xs:restriction> </xs:simpleType> </xs:element> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> </xs:schema> |
1.1.6.3 销毁模式
以下示例代码展示了如何销毁一个模式。
// 创建DestroySchema命令 FdoPtr<FdoIDestroySchema> sampleDestroySchema = (FdoIDestroySchema *) connection->CreateCommand(FdoCommandType_DestroySchema); // 销毁模式 sampleDestroySchema->SetSchemaName(L"SampleFeatureSchema"); sampleDestroySchema->Execute(); |
1.1.6.4 从XML中读入一个模式
以下示例代码展示了如何从一个XML中读入一个模式。
sampleFeatureSchemaCollection->ReadXml(L"SampleFeatureSchema.xml"); sampleFeatureSchema = sampleFeatureSchemaCollection->FindItem(L"SampleFeatureSchema"); sampleApplySchema->SetFeatureSchema(sampleFeatureSchema); sampleApplySchema->Execute(); sampleFeatureSchemaCollection->Clear(); |