Hi John, since I will deal with reconstructed points in QGIS, I used the set_shapefile_attribute function. After trail and errors, I think it works.
I notice if I do a regular point reconstruction (without adding an additional shapefile attribute), the resulted reconstructed shapefile has ~15 attributes including Anchor, Time, Reconfile1, PlateID…Speard_Asy, Import_Age, etc.
After I used set_shapefile_attribute, I only have 4 attributes: Anchor, Time, Reconfile1 and SiO2. Missing other attributes do not bother me for now since SiO2 are tied with points… I may put the set_shapefile_attribute in a wrong place…
The input text file (input_points_new.txt) has three columns: lat, long and SiO2
Here is my code:
import pygplates
rotation_model = pygplates.RotationModel(‘rotations.rot’)
static_polygons_filename = ‘static_polygons.gpmlz’
input_points_filename = ‘input_points_new.txt’
output_points_filename = ‘output_points.gpmlz’
Parse the input points text file containing a lon/lat point per line.
input_points = []
with open(input_points_filename, ‘r’) as input_points_file:
for line_number, line in enumerate(input_points_file):
# Make line number 1-based instead of 0-based.
line_number = line_number + 1
line_string_list = line.split()
# Attempt to convert each string into a floating-point number.
try:
lon = float(line_string_list[0])
lat = float(line_string_list[1])
except ValueError:
print (‘Line %d: Ignoring point - cannot read lon/lat values.’ % line_number)
continue
# Create a pyGPlates point from the latitude and longitude, and add it to our list of points.
input_points.append(pygplates.PointOnSphere(lat, lon))
get SiO2 values from txt as a list
SiO2_value_list = []
file = open(‘input_points_new.txt’, ‘r’)
lines = file.readlines()
for line in lines:
a = line.split()
x = a[2]
SiO2_value_list.append(x)
file.close()
convert SiO2 values from list to float
SiO2_value_floats = []
for item in SiO2_value_list:
SiO2_value_floats.append(float(item))
Create a feature for each point we read from the input file.
i=0 # counter for SiO2 value float
point_features = []
for point in input_points:
point_feature = pygplates.Feature()
# add additional features to shapefile
point_feature.set_shapefile_attribute('SiO2',SiO2_value_floats[i])
point_feature.set_geometry(point)
point_features.append(point_feature)
i=i+1
Use the static polygons to assign plate IDs and valid time periods.
assigned_point_features = pygplates.partition_into_plates(
static_polygons_filename,
rotation_model,
point_features,
properties_to_copy = [
pygplates.PartitionProperty.reconstruction_plate_id,
pygplates.PartitionProperty.valid_time_period])
Write the assigned point features to the output GPML file (ready for use in GPlates).
assigned_point_feature_collection = pygplates.FeatureCollection(assigned_point_features)
assigned_point_feature_collection.write(output_points_filename)
#%% Restore points to a geological time based on rotation model
rotation_model = pygplates.RotationModel(‘rotations.rot’)
features = pygplates.FeatureCollection(‘output_points.gpmlz’)
reconstruction_time = 100
export_filename = ‘reconstructed_points_{0}Ma.shp’.format(reconstruction_time)
pygplates.reconstruct(features, rotation_model, export_filename, reconstruction_time)