Issue with add a new property to a feature in pygplates

Hi Thomas,

Oh I see what the problem is (thanks for posting your code by the way). Actually this is the first time I’ve noticed this problem. The problem has to do with writing out to GPML and then reading back in again (that is, properties not defined in the GPlates information model). On reading back in, they are not interpreted/parsed because it’s not a property that’s recognised/defined by the GPlates information model (so it doesn’t really know how to read/create it). This is different than directly adding a property to a feature using pyGPlates (with pygplates.VerifyInformationModel.no) such as:

feature.add(
    pygplates.PropertyName.create_gpml('isochron_age'),
    pygplates.XsDouble(begin_time),
    pygplates.VerifyInformationModel.no)

…because there you know the type (XsDouble) of the property (ie, you obviously know begin_time is a float and not something else like a string). However when the same property is read from a GPML file, the only information pyGPlates has is:

<gpml:isochron_age>100</gpml:isochron_age>

…which says nothing about the type (ie, 100 could be a string or a number). If this property had been in the information model then GPlates would know that gpml:isochron_age was a number (and be able to load it into an XsDouble or XsInteger rather than an XsString or something else). There are ways to define the type in XML but, for simple non-structural types in GPlates, this is how we currently do it.

This is basically an issue we are starting to deal with, which is loosening up restrictions of the GPlates information model to better allow arbitrary user-defined properties (and also easier/better mapping between attributes in other formats, like Shapefiles, and the GPlates information model).

In the meantime, a workaround is to co-opt one of the existing properties in the GPlates information model. Or, even better, use the Shapefile attributes (which is actually one of those properties whose value is a dictionary of attributes). These attributes will survive writing out to GPML and reading back in again (because their contents are rather simple, and they are not dictated by the GPlates information model):

output_feature.set_shapefile_attribute('isochron_age', begin_time)

# Write feature out to GPML and read back in again.
pygplates.FeatureCollection(output_feature).write('tmp.gpml')
input_feature = list(pygplates.FeatureCollection('tmp.gpml'))[0]

begin_time = input_feature.get_shapefile_attribute('isochron_age')

You can write as many Shapefile attributes as you want. Each attribute can be either a string, integer or float (in your case begin_time is a float). I guess just be careful not to clobber any existing Shapefile attribute (eg, if your feature originally came from a Shapefile that had some of its own attributes that you want to keep around).

Regards,
John