Changes between Version 2 and Version 3 of UsingTheParserToCreateCSML


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

more editing

Legend:

Unmodified
Added
Removed
Modified
  • UsingTheParserToCreateCSML

    v2 v3  
    66 
    77So 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. 
     8 * Create some parser objects - a Dataset, a !FeatureCollection, some Features etc. 
     9 * Set their attributes to the right values for your data*. 
    1010 * Call the toXML method of the root level Dataset() object. 
    1111 * Sit back and enjoy your CMSL document. 
     12 
     13*In this example I have hard-coded data. 
    1214 
    1315The following code snippets go through this process for a couple of sample features. 
     
    113115 
    114116 == Now to look at this in more detail: == 
     117 
     118First we import the parser and parser_extra (which contains some essential extra functions): 
     119 
     120{{{ 
     121#!python 
     122from Parser import * 
     123import parser_extra 
     124}}}  
     125 
     126 
     127Now start construction of CSML objects. 
     128 
     129However it probably helps to look at the end of the code first. Here we create a Dataset object (ds) and set other objects as attributes of the Dataset object. 
     130 
     131{{{ 
     132#!python 
     133########### The Dataset  ############## 
     134#Create an Empty Dataset 
     135ds = Dataset() 
     136#Set objects as attributes of dataset 
     137#NOTE: you can equally write this in the form: ds.id = 'Test001'  as used above. 
     138setattr(ds,'id','Test001') 
     139setattr(ds, 'metaDataProperty', mds) 
     140setattr(ds,'description',Description('This is a test Water level for Oslo Metorolgikk Institute.Period 26/04/06 06h -> 29/04/06 12h')) 
     141setattr(ds, 'referenceSystemDefinitions', rsd) 
     142setattr(ds, 'featureCollection',fc) 
     143######################################## 
     144}}} 
     145 
     146So the featureCollection, referenceSystemDefinitions etc are attributes of the Dataset. 
     147 
     148Remember when we call the toXML() method on the Dataset, this also calls the toXML() methods of the featureCollection, referenceSystemDefinitions etc. 
     149Anyway, so the if the aim is to get a fully populated Dataset object, we need to create some feature objects: 
     150 
     151# #################### FeatureCollection ####################### 
     152#  
     153# #empty list to hold featureMembers 
     154fms =[] 
     155#  
     156 
     157# #### add a PointSeriesFeature: ##### 
     158ptsf=PointSeriesFeature() 
     159ptsf.id='testbed270401' 
     160ptsf.description=Description('Station ESBJERG') 
     161ptsd=PointSeriesDomain() 
     162t=Trajectory() 
     163t.srsName='urn:EPSG:geographicCRS:4326' 
     164t.locations=DirectPositionList(vals='0.1 1.5 25') 
     165t.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') 
     166ptsd.domainReference=t 
     167ptsf.domain=ptsd 
     168rs=RangeSet() 
     169rs.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') 
     170ptsf.rangeSet=rs 
     171fms.append(ptsf) 
     172 
     173 
     174 
     175# #instantiate FeatureCollection object: 
     176fc=FeatureCollection(members=fms) 
     177etp = EnvelopeWithTimePeriod() 
     178etp.lowerCorner=DirectPosition('42 12',uomLabels='deg deg',axisLabels='lat lon') 
     179etp.upperCorner=DirectPosition('42 26',uomLabels='deg deg',axisLabels='lat lon') 
     180etp.timePosition='2006-04-26T06:00:00+01' 
     181etp.timePosition2='2006-04-29T012:00:00+01' 
     182fc.boundedBy=etp 
     183 
     184 
     185 
     186And here is an (empty) example of a ReferenceSystemDefinition object. Note that UnitDefinitions and PhenomenonDefinitions are all handled pretty much the same way. 
     187 
     188 
     189{{{ 
     190#!python 
     191# ###############Reference System Definitions ####################### 
     192#empty list to hold definitionMembers 
     193dm=[] 
     194tc=TimeCoordinateSystem() 
     195dm.append(tc) 
     196rsd=ReferenceSystemDefinitions(definitionMembers=dm) 
     197}}} 
     198 
     199 
     200 
     201 
     202 
     203 
     204 
     205 
     206 
     207 
     208#MetaDataProperty Object of <Dataset> 
     209#list to hold multiple metaDataProperty elements 
     210mds = [] 
     211 
     212md = MetaDataProperty() 
     213md.text=['Data (c) 2006 Oslo Meteorologisk Institutt // www.met.no'] 
     214mds.append(md) 
     215 
     216md = MetaDataProperty() 
     217md.text=['Test for Oslo Meteorolgisk Institutt'] 
     218mds.append(md) 
     219#etc... 
     220 
     221 
     222########### The Dataset  ############## 
     223#Create an Empty Dataset 
     224ds = Dataset() 
     225#Set objects as attributes of dataset 
     226#NOTE: you can equally write this in the form: ds.id = 'Test001'  as used above. 
     227setattr(ds,'id','Test001') 
     228setattr(ds, 'metaDataProperty', mds) 
     229setattr(ds,'description',Description('This is a test Water level for Oslo Metorolgikk Institute.Period 26/04/06 06h -> 29/04/06 12h')) 
     230setattr(ds, 'referenceSystemDefinitions', rsd) 
     231setattr(ds, 'featureCollection',fc) 
     232######################################## 
     233 
     234#call the toXML method of the Dataset object: 
     235csml = ds.toXML() 
     236#parse and pretty print the result 
     237strCSML=parser_extra.PrettyPrint(csml) 
     238strCSML=parser_extra.removeInlineNS(strCSML) 
     239print strCSML 
     240 
     241#(you could save this to file instead) 
     242}}} 
     243