之前,有分享整理有关Shapefile与KML两种文件格式进行相互转换的内容。今天,我们再来一起探索一下在GIS中,对于经纬度的表达度分秒和十进制度这两种形式间的转换。
一、基于ArcMap的转换
1.ArcMap将度-分-秒值转换为十进制度值
这种方法主要使用字段计算器,将保存在表中以度分秒表示的经纬度数值转换为十进制度的形式,代码用VB编写。注:这里有一前提,即假定度分秒是以字符串(文本)的形式存储的,数字间留有空格且不带任何符号。
①首先,将保存有经纬度的表格加载到ArcMap中,并为其添加两个双精度类型字段Lat和Long用于保存转换后的经纬度。

②这里以Latitude为例,右键单击新建的Lat字段,点击字段计算器,勾选"显示代码"选项,将以下代码粘贴到预逻辑脚本代码框(注:在以 DMS = 开头的第六行中,括号 [ ] 中的文字应当是保存纬度值字段的名称,应根据实际替换)。
python
Dim Degrees
Dim Minutes
Dim Seconds
Dim DMS
Dim DD
DMS = Split([Latitude])
Degrees = CDbl(DMS(0))
Minutes = CDbl(DMS(1))
Seconds = CDbl(DMS(2))
If Degrees < 0 Then
DD = -(Seconds/3600) - (Minutes/60) + Degrees
Else
DD = (Seconds/3600) + (Minutes/60) + Degrees
End If
③再将以下代码粘贴到对话框底部的 Lat = 框中,点击确定,完成转换。对于longtitude的转换,只需要重复上述步骤并注意替换字段名称即可。
python
CDbl(DD)

这里是我使用虚构数据的测试结果:

针对这一方法,ESRI官方也有相关文档予以介绍,感兴趣的朋友可以自行了解一二。
官方网址:
2.ArcMap将十进制度值转换为度-分-秒值
这里同样使用字段计算器将存储在数字字段中的十进制度转换为存储在文本字段中的度分秒。
①首先,将数据加载到ArcMap中;
②为其添加两个文本类型的字段DMSLat和DMSLong,设置字段长度为20,用于保存转化后的经纬度;

③右键单击新建的字段,选择字段计算器,勾选代码表达为Python,勾选Show Codeblock (显示代码块) 复选框,并将下列代码复制到Pre-Logic Script Code(预逻辑脚本代码) 框中。
python
def decimalDegrees2DMS(value,type):
"""
Converts a Decimal Degree Value into
Degrees Minute Seconds Notation.
Pass value as double
type = {Latitude or Longitude} as string
returns a string as D:M:S:Direction
created by: anothergisblog.blogspot.com
"""
degrees = int(value)
submin = abs( (value - int(value) ) * 60)
minutes = int(submin)
subseconds = abs((submin-int(submin)) * 60)
direction = ""
if type == "Longitude":
if degrees < 0:
direction = "W"
elif degrees > 0:
direction = "E"
else:
direction = ""
elif type == "Latitude":
if degrees < 0:
direction = "S"
elif degrees > 0:
direction = "N"
else:
direction = ""
notation = str(degrees) + u"\u00b0" + str(minutes) + "\'" +\
str(subseconds)[0:5] + "\"" + direction
return notation
④根据所需的纬度或经度数据,将以下代码之一粘贴到对话框底部的 DMSLat = expression 框中。(注:需要将!Latitude_Field_Name!参数值替换为包含纬度十进制度值的字段)
python
decimalDegrees2DMS( !Latitude_Field_Name! ,"Latitude")
decimalDegrees2DMS( !<Longitude_Field_Name! ,"Longitude")

对经度值的转换则重复上述步骤,仅需将 DMSLong 表达式框中的第一个参数更改为表中包含经度十进制度值的字段,并将第二个参数中的单词 Latitude 替换为单词 Longitude。
同样,我也使用虚构数据进行了测试,测试结果如下:

针对这一方法,ESRI官方也有相关文档予以介绍,感兴趣的朋友可以自行了解一二。
官方网址:
当然,除此之外,还有其他方便快捷的方式方法,大家可以根据具体需要灵活使用。今天的分享就到这里,后续也会继续更新相关内容,欢迎大家关注!