wiki:UsingTheParserToCreateCSML

Version 3 (modified by domlowe, 13 years ago) (diff)

more editing

How to create CSML using the CSML Parser classes

As you will know if you read the other parser how to, the CSML parser creates a python representation of a CSML document, using python classes to represent XML data types.

Each 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.

So to create CSML from scratch using python, all you need to do is:

  • Create some parser objects - a Dataset, a FeatureCollection, some Features etc.
  • Set their attributes to the right values for your data*.
  • Call the toXML method of the root level Dataset() object.
  • Sit back and enjoy your CMSL document.

*In this example I have hard-coded data.

The following code snippets go through this process for a couple of sample features. To 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.

The Code:

Here is the full code listing which we will look at in more detail:

from Parser import *
import parser_extra


  #########################################
##  construction of CSML objects ##
  #########################################

# 
# ###############Reference System Definitions #######################
#empty list to hold definitionMembers
dm=[]
tc=TimeCoordinateSystem()
dm.append(tc)
rsd=ReferenceSystemDefinitions(definitionMembers=dm)



# #################### FeatureCollection #######################
# 
# #empty list to hold featureMembers
fms =[]
# 

# #### add a PointSeriesFeature: #####
ptsf=PointSeriesFeature()
ptsf.id='testbed270401'
ptsf.description=Description('Station ESBJERG')
ptsd=PointSeriesDomain()
t=Trajectory()
t.srsName='urn:EPSG:geographicCRS:4326'
t.locations=DirectPositionList(vals='0.1 1.5 25')
t.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')
ptsd.domainReference=t
ptsf.domain=ptsd
rs=RangeSet()
rs.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')
ptsf.rangeSet=rs
fms.append(ptsf)



# #instantiate FeatureCollection object:
fc=FeatureCollection(members=fms)
etp = EnvelopeWithTimePeriod()
etp.lowerCorner=DirectPosition('42 12',uomLabels='deg deg',axisLabels='lat lon')
etp.upperCorner=DirectPosition('42 26',uomLabels='deg deg',axisLabels='lat lon')
etp.timePosition='2006-04-26T06:00:00+01'
etp.timePosition2='2006-04-29T012:00:00+01'
fc.boundedBy=etp



#MetaDataProperty Object of <Dataset>
#list to hold multiple metaDataProperty elements
mds = []

md = MetaDataProperty()
md.text=['Data (c) 2006 Oslo Meteorologisk Institutt // www.met.no']
mds.append(md)

md = MetaDataProperty()
md.text=['Test for Oslo Meteorolgisk Institutt']
mds.append(md)
#etc...


########### The Dataset  ##############
#Create an Empty Dataset
ds = Dataset()
#Set objects as attributes of dataset
#NOTE: you can equally write this in the form: ds.id = 'Test001'  as used above.
setattr(ds,'id','Test001')
setattr(ds, 'metaDataProperty', mds)
setattr(ds,'description',Description('This is a test Water level for Oslo Metorolgikk Institute.Period 26/04/06 06h -> 29/04/06 12h'))
setattr(ds, 'referenceSystemDefinitions', rsd)
setattr(ds, 'featureCollection',fc)
########################################

#call the toXML method of the Dataset object:
csml = ds.toXML()
#parse and pretty print the result
strCSML=parser_extra.PrettyPrint(csml)
strCSML=parser_extra.removeInlineNS(strCSML)
print strCSML

#(you could save this to file instead)

Now to look at this in more detail:

First we import the parser and parser_extra (which contains some essential extra functions):

from Parser import *
import parser_extra

Now start construction of CSML objects.

However 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.

########### The Dataset  ##############
#Create an Empty Dataset
ds = Dataset()
#Set objects as attributes of dataset
#NOTE: you can equally write this in the form: ds.id = 'Test001'  as used above.
setattr(ds,'id','Test001')
setattr(ds, 'metaDataProperty', mds)
setattr(ds,'description',Description('This is a test Water level for Oslo Metorolgikk Institute.Period 26/04/06 06h -> 29/04/06 12h'))
setattr(ds, 'referenceSystemDefinitions', rsd)
setattr(ds, 'featureCollection',fc)
########################################

So the featureCollection, referenceSystemDefinitions etc are attributes of the Dataset.

Remember when we call the toXML() method on the Dataset, this also calls the toXML() methods of the featureCollection, referenceSystemDefinitions etc. Anyway, so the if the aim is to get a fully populated Dataset object, we need to create some feature objects:

# #################### FeatureCollection? ####################### # # #empty list to hold featureMembers fms =[] #

# #### add a PointSeriesFeature?: ##### ptsf=PointSeriesFeature?() ptsf.id='testbed270401' ptsf.description=Description('Station ESBJERG') ptsd=PointSeriesDomain?() t=Trajectory() t.srsName='urn:EPSG:geographicCRS:4326' t.locations=DirectPositionList?(vals='0.1 1.5 25') t.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') ptsd.domainReference=t ptsf.domain=ptsd rs=RangeSet?() rs.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') ptsf.rangeSet=rs fms.append(ptsf)

# #instantiate FeatureCollection? object: fc=FeatureCollection?(members=fms) etp = EnvelopeWithTimePeriod?() etp.lowerCorner=DirectPosition?('42 12',uomLabels='deg deg',axisLabels='lat lon') etp.upperCorner=DirectPosition?('42 26',uomLabels='deg deg',axisLabels='lat lon') etp.timePosition='2006-04-26T06:00:00+01' etp.timePosition2='2006-04-29T012:00:00+01' fc.boundedBy=etp

And here is an (empty) example of a ReferenceSystemDefinition? object. Note that UnitDefinitions? and PhenomenonDefinitions? are all handled pretty much the same way.

# ###############Reference System Definitions #######################
#empty list to hold definitionMembers
dm=[]
tc=TimeCoordinateSystem()
dm.append(tc)
rsd=ReferenceSystemDefinitions(definitionMembers=dm)

#MetaDataProperty? Object of <Dataset> #list to hold multiple metaDataProperty elements mds = []

md = MetaDataProperty?() md.text=Data (c) 2006 Oslo Meteorologisk Institutt // www.met.no? mds.append(md)

md = MetaDataProperty?() md.text=Test for Oslo Meteorolgisk Institutt? mds.append(md) #etc...

########### The Dataset ############## #Create an Empty Dataset ds = Dataset() #Set objects as attributes of dataset #NOTE: you can equally write this in the form: ds.id = 'Test001' as used above. setattr(ds,'id','Test001') setattr(ds, 'metaDataProperty', mds) setattr(ds,'description',Description('This is a test Water level for Oslo Metorolgikk Institute.Period 26/04/06 06h -> 29/04/06 12h')) setattr(ds, 'referenceSystemDefinitions', rsd) setattr(ds, 'featureCollection',fc) ########################################

#call the toXML method of the Dataset object: csml = ds.toXML() #parse and pretty print the result strCSML=parser_extra.PrettyPrint?(csml) strCSML=parser_extra.removeInlineNS(strCSML) print strCSML

#(you could save this to file instead) }}}