Changes between Version 1 and Version 2 of UsingTheParserToCreateCSML


Ignore:
Timestamp:
14/07/06 13:44:52 (13 years ago)
Author:
domlowe
Comment:

Writing how to on CSML creation using parser

Legend:

Unmodified
Added
Removed
Modified
  • UsingTheParserToCreateCSML

    v1 v2  
    11 = How to create CSML using the CSML Parser classes = 
     2 
     3As you will know if you read the [wiki:CSMLParserHowTo other parser how to], the CSML parser creates a python representation of a CSML document, using python classes to represent XML data types. 
     4 
     5Each parser class has a fromXML() and a toXML() method. The fromXML method takes an XML document (or fragment) and populates the attributes of the python object, the toXML method takes the attributes and structure of the python object and creates an XML !ElementTree fragment. 
     6 
     7So to create CSML from scratch using python, all you need to do is: 
     8 * Create some parser objects - A Dataset, A !featureCollection, some features etc. 
     9 * Set their attributes equal to the values you want to be in the document. 
     10 * Call the toXML method of the root level Dataset() object. 
     11 * Sit back and enjoy your CMSL document. 
     12 
     13The following code snippets go through this process for a couple of sample features. 
     14To find out more you will have to explore the data structures of the parser a bit more using !Pydoc or dir(). But the general pattern remains the same for all feature types. 
     15 
     16 
     17 
     18 == The Code: == 
     19 
     20Here is the full code listing which we will look at in more detail: 
     21 
     22{{{ 
     23#!python 
     24from Parser import * 
     25import parser_extra 
     26 
     27 
     28  ######################################### 
     29##  construction of CSML objects ## 
     30  ######################################### 
     31 
     32#  
     33# ###############Reference System Definitions ####################### 
     34#empty list to hold definitionMembers 
     35dm=[] 
     36tc=TimeCoordinateSystem() 
     37dm.append(tc) 
     38rsd=ReferenceSystemDefinitions(definitionMembers=dm) 
     39 
     40 
     41 
     42# #################### FeatureCollection ####################### 
     43#  
     44# #empty list to hold featureMembers 
     45fms =[] 
     46#  
     47 
     48# #### add a PointSeriesFeature: ##### 
     49ptsf=PointSeriesFeature() 
     50ptsf.id='testbed270401' 
     51ptsf.description=Description('Station ESBJERG') 
     52ptsd=PointSeriesDomain() 
     53t=Trajectory() 
     54t.srsName='urn:EPSG:geographicCRS:4326' 
     55t.locations=DirectPositionList(vals='0.1 1.5 25') 
     56t.times=TimePositionList('#pred20060427001','-18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60') 
     57ptsd.domainReference=t 
     58ptsf.domain=ptsd 
     59rs=RangeSet() 
     60rs.quantityList=MeasureOrNullList('MLUnits.xml#m', '0.27 0.25 0.25 0.23 0.22 0.22 0.21 0.17 0.17 0.14 0.14 0.12 0.10 0.08 0.08 0.08 0.11 0.13 0.11 0.11 0.13 0.10 0.06 0.06 0.06 0.01 -0.03 -0.06 -0.09 -0.11 -0.11 -0.13 -0.16 -0.18 -0.17 -0.16 -0.18 -0.16 -0.12 -0.09 -0.08 -0.07 -0.06 -0.05 -0.04 -0.03 -0.03 -0.04 -0.02 -0.01 -0.02 -0.02 -0.02 -0.03 -0.03 -0.02 -0.01 -0.01 -0.02 -0.02 -0.03 -0.04 -0.04 -0.05 -0.04 -0.05 -0.08 -0.09 -0.11 -0.12 -0.12 -0.12 -0.12 -0.12 -0.13 -0.14 -0.15') 
     61ptsf.rangeSet=rs 
     62fms.append(ptsf) 
     63 
     64 
     65 
     66# #instantiate FeatureCollection object: 
     67fc=FeatureCollection(members=fms) 
     68etp = EnvelopeWithTimePeriod() 
     69etp.lowerCorner=DirectPosition('42 12',uomLabels='deg deg',axisLabels='lat lon') 
     70etp.upperCorner=DirectPosition('42 26',uomLabels='deg deg',axisLabels='lat lon') 
     71etp.timePosition='2006-04-26T06:00:00+01' 
     72etp.timePosition2='2006-04-29T012:00:00+01' 
     73fc.boundedBy=etp 
     74 
     75 
     76 
     77#MetaDataProperty Object of <Dataset> 
     78#list to hold multiple metaDataProperty elements 
     79mds = [] 
     80 
     81md = MetaDataProperty() 
     82md.text=['Data (c) 2006 Oslo Meteorologisk Institutt // www.met.no'] 
     83mds.append(md) 
     84 
     85md = MetaDataProperty() 
     86md.text=['Test for Oslo Meteorolgisk Institutt'] 
     87mds.append(md) 
     88#etc... 
     89 
     90 
     91########### The Dataset  ############## 
     92#Create an Empty Dataset 
     93ds = Dataset() 
     94#Set objects as attributes of dataset 
     95#NOTE: you can equally write this in the form: ds.id = 'Test001'  as used above. 
     96setattr(ds,'id','Test001') 
     97setattr(ds, 'metaDataProperty', mds) 
     98setattr(ds,'description',Description('This is a test Water level for Oslo Metorolgikk Institute.Period 26/04/06 06h -> 29/04/06 12h')) 
     99setattr(ds, 'referenceSystemDefinitions', rsd) 
     100setattr(ds, 'featureCollection',fc) 
     101######################################## 
     102 
     103#call the toXML method of the Dataset object: 
     104csml = ds.toXML() 
     105#parse and pretty print the result 
     106strCSML=parser_extra.PrettyPrint(csml) 
     107strCSML=parser_extra.removeInlineNS(strCSML) 
     108print strCSML 
     109 
     110#(you could save this to file instead) 
     111}}} 
     112 
     113 
     114 == Now to look at this in more detail: ==