pygplates专栏——Sample code
本文档包含演示pyGPlates解决常见板块构造问题的示例代码。
Import
本节介绍如何将几何数据转换为可以通过pyGPlates重建的状态。
导入几何形状并指定板块ID
从文本文件导入点并分配板块ID
本实例:
- 从文本文件中读取多个坐标点
- 为每个点分配一个板块ID和有效的时间段
- 将指定的点写入GPML文件,以便在GPlates中使用。
* 示例代码
import pygplates
# 加载一个板块运动模型
rotation_model = pygplates.RotationModel("Muller2019-Young2019-Cao2020_CombinedRotations.rot")
# 加载静态的多边形
static_polygons_filename = "Global_EarthByte_GPlates_PresentDay_StaticPlatePolygons.gpmlz"
# 读取的输入文本文件
input_points_filename = "2-example inputs points.txt"
# 指定输出文件(gpml文件可以被GPlates读取)
output_points_filename = "2-output_points.gpml"
# 输入文件的格式:lon/lat点
# 经度/纬度是GMT文件的常用格式(.xy, .gmt)
# pyGPlates定义点用了相反的顺序:纬度/经度
input_points = []
with open(input_points_filename, 'r') as input_points_file:
for line_number, line in enumerate(input_points_file):
# 将行号从0开始变为1开始
line_number = line_number + 1
# 将每行切分为字符串(通过空格分隔)
line_string_list = line.split()
# 每行至少分出两个字符串(纬度和经度)
if len(line_string_list) < 2:
print('Line %d: Ignoring point - line does not have at least two white-space separated strings.' % line_number)
continue
# 尝试将每个字符串转换为浮点数
try:
# 使用 GMT(lon/lat)顺序
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
# 新建一个pyGPlates点对象并追加到点列表中
# 注意pyGPlates(lat/lon)与GMT(lon/lat)的顺序相反
input_points.append(pygplates.PointOnSphere(lat, lon))
# 为每个点创建一个板块对象列表
point_features = []
for point in input_points:
# 创建一个未分类板块对象
point_feature = pygplates.Feature()
# 将板块的geometry设置为输入的点
point_feature.set_geometry(point)
point_features.append(point_feature)
# 使用静态几何图形来分配板块IDs和有效的时间段
# 每个点feature被分配到某个静态多边形,获取它的重建板块ID和有效时间段
assigned_point_featured = pygplates.partition_into_plates(
static_polygons_filename,
rotation_model,
point_features,
properties_to_copy=[
pygplates.PartitionProperty.reconstruction_plate_id,
pygplates.PartitionProperty.valid_time_period
]
)
# 将结果写入GPML文件
assigned_point_feature_collection = pygplates.FeatureCollection(assigned_point_featured)
assigned_point_feature_collection.write(output_points_filename)
* 输入
输入的文本文件内容(经度/纬度):
-79.747867 -1.444159
-79.786712 -1.654002
-79.872547 -2.221801
-79.858122 -6.951201
-78.850008 -9.359851
-76.020448 -13.798207
-75.549659 -14.315297
-75.411320 -14.456342
-74.335501 -15.543422
-72.539796 -17.187214
-71.922547 -17.773935
-71.381735 -18.373316
-70.979182 -18.850190
-70.786266 -19.126329
-70.571175 -19.417365
-70.343507 -19.716224
-70.280285 -19.858811
-70.107565 -20.531859
-70.059697 -22.248895
* 详解
板块运动模型加载到一个pygplates.RotationModel对象
rotation_model = pygplates.RotationModel('rotations.rot')
坐标点的文本文件(输入)和GPML文件(输出文件)
input_points_filename = 'input_points.txt'
output_points_filename = 'output_points.gpml'
逐行读取输入文件
with open(input_points_filename, 'r') as input_points_file:
for line_number, line in enumerate(input_points_file):
每行包含一个纬度字符串和一个经度字符串
尝试将它们转换为浮点数
如果转换失败,捕捉ValueError并跳过该行
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
我们将为读取的每个点创建一个新的feature对象。保持pygplates.Feature()的类型为空,该对象默认类型为pygplates.FeatureType.gpml_unclassified_feature。理想情况下,我们应该选择一个特定的类型,比如pygplates.FeatureType.gpml_hot_spot,也许可以从输入文件中读取。
另外我们也可以导入额外的元数据,比如feature的name和description。
point_feature = pygplates.Feature()
使用pygplates.Feature.set_geometry()为点的feature对象设置geometry属性。
如果不这么做,那么该对象就不能用于空间计算而且不能显示在GPlates的球体上。
point_feature.set_geometry(point)
使用pygplates.partition_into_plates()将每个点feature分到某一静态多边形中,分配了它的重建板块ID和有效时间段
静态多边形目前具有全局覆盖(pygplates.partition_into_plates()的默认重建时间),因此应该划分所有输入点。
我们还显式地指定参数properties_to_copy来分配重建板块ID和有效时间段(默认情况下只分配重建板块ID)。
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])
最后将所有已分配的features列表合并到一个pygplates.FeatureCollection对象,然后通过pygplates.FeatureCollection.write()输出文件
assigned_point_feature_collection = pygplates.FeatureCollection(assigned_point_features)
assigned_point_feature_collection.write(output_points_filename)
从GMT文件导入点并分配板块ID
与上一方法的不同之处在于从GMT文件中读取点,避免了手动处理文本文件。
* 示例代码
import pygplates
# 加载一个板块运动模型
rotation_model = pygplates.RotationModel("Muller2019-Young2019-Cao2020_CombinedRotations.rot")
# 加载静态的多边形
static_polygons_filename = "Global_EarthByte_GPlates_PresentDay_StaticPlatePolygons.gpmlz"
# 加载GMT文件
point_features = pygplates.FeatureCollection("2-example inputs points.gmt")
# 指定输出文件(gpml文件可以被GPlates读取)
output_points_filename = "2-output_points_read_by_gmt.gpml"
# 使用静态几何图形来分配板块IDs和有效的时间段
# 每个点feature被分配到某个静态多边形,获取它的重建板块ID和有效时间段
assigned_point_featured = pygplates.partition_into_plates(
static_polygons_filename,
rotation_model,
point_features,
properties_to_copy=[
pygplates.PartitionProperty.reconstruction_plate_id,
pygplates.PartitionProperty.valid_time_period
]
)
# 将结果写入GPML文件
assigned_point_feature_collection = pygplates.FeatureCollection(assigned_point_featured)
assigned_point_feature_collection.write(output_points_filename)
* 输入
输入的GMT文件内容(经度/纬度):
-79.747867 -1.444159
-79.786712 -1.654002
-79.872547 -2.221801
-79.858122 -6.951201
-78.850008 -9.359851
-76.020448 -13.798207
-75.549659 -14.315297
-75.411320 -14.456342
-74.335501 -15.543422
-72.539796 -17.187214
-71.922547 -17.773935
-71.381735 -18.373316
-70.979182 -18.850190
-70.786266 -19.126329
-70.571175 -19.417365
-70.343507 -19.716224
-70.280285 -19.858811
-70.107565 -20.531859
-70.059697 -22.248895
* 详解
因为GPlates可以直接加载GMT。另一种方法是将文本文件的扩展名更改为’.gmt’。特性元数据将从文本文件中丢失,因此只有几何数据将被加载,但这实现了与上面示例相同的效果。
与前面的示例一样,每行可以有两个以上的数字,但只使用前两个(作为经度和纬度)-注意,如果要加载’.gmt’文件,额外的数据将导致它发出关于将2.5D扁平化到2D的警告。
请注意,与前面的示例一样,数据应该是GMT(经度/纬度)顺序。
从文本文件中导入折线并分配板块ID
本实例与从文本文件中导入点并分配板块ID相似,不同于导入了折线而不是点
* 示例代码
import pygplates
# 自定义函数:创建一个折线feature
def add_polyline_feature_from_points(polyline_features, points, line_number):
# 如果没有点
if not points:
return
# 至少两个点才能创建一个polyline
if len(points) >= 2:
polyline = pygplates.PolylineOnSphere(points)
polyline_feature = pygplates.Feature() # 未分类feature
polyline_feature.set_geometry(polyline)
polyline_features.append(polyline_feature)
# 如果仅有1个点,抛出警告
else:
print("Line %d: Ignoring polyline - polyline has only one point." % (line_number-1))
# 清理点列表
del points[:]
# 加载一个板块运动模型
rotation_model = pygplates.RotationModel("Muller2019-Young2019-Cao2020_CombinedRotations.rot")
# 加载静态的多边形
static_polygons_filename = "Global_EarthByte_GPlates_PresentDay_StaticPlatePolygons.gpmlz"
# 读取的输入文本文件
input_polylines_filename = "2-example input polylines.txt"
# 指定输出文件(gpml文件可以被GPlates读取)
output_polylines_filename = "2- output_polylines.gpml"
# 解析折线文本文件,分组的lon/lat点
# 经度/纬度是GMT文件的常用格式(.xy, .gmt)
# pyGPlates定义点用了相反的顺序:纬度/经度
polyline_features = []
polyline_points = []
with open(input_polylines_filename, 'r') as input_polylines_file:
for line_number, line in enumerate(input_polylines_file):
# 将行号从0开始变为1开始
line_number = line_number + 1
# 行开头是否为 >,这是折线的分组点
if line.strip().startswith(">"):
# 使用自定义函数创建polyline feature
add_polyline_feature_from_points(polyline_features, polyline_points, line_number)
# 继续处理下一行
continue
# 将每行拆分
line_string_list = line.split()
# 每行至少拆分出两个字符串(经度和纬度)
if len(line_string_list) < 2:
print("Line %d: Ignoring point - line does not have at least two white-space separated strings." % line_number)
continue
# 尝试将每个字符串转换为浮点数
try:
# 使用GMT(经度/纬度)顺序
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
# 新建一个pyGPlates点对象并追加到点列表中
# 注意pyGPlates(lat/lon)与GMT(lon/lat)的顺序相反
polyline_points.append(pygplates.PointOnSphere(lat, lon))
# 如果我们有任何剩余点,然后生成最后的折线特征。
# 如果最后一行不是以'>'开头,就会发生这种情况。
add_polyline_feature_from_points(polyline_features, polyline_points, line_number)
# 使用静态几何图形来分配板块IDs和有效的时间段
# 每个折线feature被分配到某个静态多边形,获取它的重建板块ID和有效时间段
assigned_polyline_features = pygplates.partition_into_plates(
static_polygons_filename,
rotation_model,
polyline_features,
properties_to_copy=[
pygplates.PartitionProperty.reconstruction_plate_id,
pygplates.PartitionProperty.valid_time_period
]
)
# 将结果写入GPML文件
assigned_polyline_feature_collection = pygplates.FeatureCollection(assigned_polyline_features)
assigned_polyline_feature_collection.write(output_polylines_filename)
* 输入
输入的文本文件内容(经度/纬度):
>
-79.747867 -1.444159
-79.786712 -1.654002
-79.872547 -2.221801
-79.858122 -6.951201
-78.850008 -9.359851
-76.020448 -13.798207
>
-75.549659 -14.315297
-75.411320 -14.456342
-74.335501 -15.543422
-72.539796 -17.187214
-71.922547 -17.773935
-71.381735 -18.373316
-70.979182 -18.850190
-70.786266 -19.126329
>
-70.571175 -19.417365
-70.343507 -19.716224
-70.280285 -19.858811
-70.107565 -20.531859
-70.059697 -22.248895
* 详解
板块运动模型加载到pygplates.RotationModel类
rotation_model = pygplates.RotationModel('rotations.rot')
指定作为输入的文本文件和作为输出的GPML文件
input_polylines_filename = 'input_polylines.txt'
output_polylines_filename = 'output_polylines.gpml'
逐行读取输入的文本文件
with open(input_polylines_filename, 'r') as input_polylines_file:
for line_number, line in enumerate(input_polylines_file):
如果某行的起始字符为">",之后将这些点坐标拆分为线段
if line.strip().startswith('>'):
add_polyline_feature_from_points(polyline_features, polyline_points, line_number)
continue
每行包含一个纬度字符串和一个经度字符串,尝试将它们转换为浮点数,如果转换失败,捕捉ValueError并跳过该行
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
随时监听当前线段的点坐标,一旦抵达当前线段的最后一点,可以创建新的线段
polyline_points.append(pygplates.PointOnSphere(lat, lon))
自定义的add_polyline_feature_from_points函数,通过多个点坐标创建一个折线feature,再将该折线feature追加到折线features列表中
def add_polyline_feature_from_points(polyline_features, points, line_number):
...
如果用于创建折线feature的点列表长度至少为2,那么就基于点坐标创建pygplates.PolylineOnSphere对象
if len(points) >= 2:
polyline = pygplates.PolylineOnSphere(points)
将每个折线设置为未分类feature。保持pygplates.Feature()的类型为空,该对象默认类型为pygplates.FeatureType.gpml_unclassified_feature。
polyline_feature = pygplates.Feature()
通过pygplates.Feature.set_geometry()设置折线feature的多边形属性,如果不这么做,那么该对象就不能用于空间计算而且不能显示在GPlates的球体上。
polyline_feature.set_geometry(polyline)
使用pygplates.partition_into_plates()将每个折线feature分到某一静态多边形中,分配了它的重建板块ID和有效时间段
静态多边形目前具有全局覆盖(pygplates.partition_into_plates()的默认重建时间),因此应该划分所有输入点。
我们还显式地指定参数properties_to_copy来分配重建板块ID和有效时间段(默认情况下只分配重建板块ID)。
assigned_polyline_features = pygplates.partition_into_plates(
static_polygons_filename,
rotation_model,
polyline_features,
properties_to_copy = [
pygplates.PartitionProperty.reconstruction_plate_id,
pygplates.PartitionProperty.valid_time_period])
最后将所有已分配的features列表合并到一个pygplates.FeatureCollection对象,然后通过pygplates.FeatureCollection.write()输出文件
assigned_polyline_feature_collection = pygplates.FeatureCollection(assigned_polyline_features)
assigned_polyline_feature_collection.write(output_polylines_filename)
从GMT文件导入折线并分配板块ID
* 示例代码
import pygplates
# 加载一个板块运动模型
rotation_model = pygplates.RotationModel("Muller2019-Young2019-Cao2020_CombinedRotations.rot")
# 加载静态的多边形
static_polygons_filename = "Global_EarthByte_GPlates_PresentDay_StaticPlatePolygons.gpmlz"
# 读取的输入文本文件
input_polylines_filename = "2-example input polylines.gmt"
# 指定输出文件(gpml文件可以被GPlates读取)
output_polylines_filename = "2- output_polylines_gmt.gpml"
# 加载GMT文件
polyline_features = pygplates.FeatureCollection(input_polylines_filename)
# 使用静态几何图形来分配板块IDs和有效的时间段
# 每个折线feature被分配到某个静态多边形,获取它的重建板块ID和有效时间段
assigned_polyline_features = pygplates.partition_into_plates(
static_polygons_filename,
rotation_model,
polyline_features,
properties_to_copy=[
pygplates.PartitionProperty.reconstruction_plate_id,
pygplates.PartitionProperty.valid_time_period
]
)
# 将结果写入GPML文件
assigned_polyline_feature_collection = pygplates.FeatureCollection(assigned_polyline_features)
assigned_polyline_feature_collection.write(output_polylines_filename)
* 输入
输入的GMT文件内容(经度/纬度):
>
-79.747867 -1.444159
-79.786712 -1.654002
-79.872547 -2.221801
-79.858122 -6.951201
-78.850008 -9.359851
-76.020448 -13.798207
>
-75.549659 -14.315297
-75.411320 -14.456342
-74.335501 -15.543422
-72.539796 -17.187214
-71.922547 -17.773935
-71.381735 -18.373316
-70.979182 -18.850190
-70.786266 -19.126329
>
-70.571175 -19.417365
-70.343507 -19.716224
-70.280285 -19.858811
-70.107565 -20.531859
-70.059697 -22.248895