python 图上打经纬度的点
时间: 2023-09-06 11:04:15 浏览: 30
在Python中,我们可以使用各种库和模块来制作图表,并在图表上打印经纬度点。其中,matplotlib是最常用的库之一。
首先,我们需要导入matplotlib和其他必要的模块。然后,创建一个新的图表和坐标系。
```python
import matplotlib.pyplot as plt
# 创建一个新的图表和坐标系
fig, ax = plt.subplots()
```
接下来,我们可以通过使用scatter()函数在坐标轴上打印经纬度点。scatter()函数可以接受一个包含经度和纬度的列表作为参数。
```python
# 经度和纬度数据
longitude = [116.3975, 113.2644, 121.4737]
latitude = [39.9087, 23.1291, 31.2304]
# 打印经纬度点
ax.scatter(longitude, latitude, color='red', marker='o')
# 添加标题和坐标轴标签
ax.set_title('经纬度点分布图')
ax.set_xlabel('经度')
ax.set_ylabel('纬度')
# 显示图表
plt.show()
```
以上代码会在图表上打印三个经纬度点,分别对应北京(116.3975, 39.9087)、广州(113.2644, 23.1291)和上海(121.4737, 31.2304)的经纬度。
通过适当调整图表的大小、坐标轴的范围等参数,我们可以根据需求进行进一步的定制,以满足特定的需求。
相关问题
python 读取无人机图片 经纬度信息
可以使用python的Pillow库来读取图片,然后使用EXIF数据来获取图片的经纬度信息。以下是示例代码:
```
from PIL import Image
from PIL.ExifTags import GPSTAGS, TAGS
def get_exif_data(image):
"""
获取图片的元数据
"""
exif_data = {}
with Image.open(image) as img:
info = img._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
exif_data[decoded] = value
return exif_data
def get_geotagging(exif_data):
"""
获取图片的经纬度信息
"""
lat = None
lng = None
gps_info = {}
if "GPSInfo" in exif_data:
# 获取GPS信息
gps_info = exif_data["GPSInfo"]
gps_latitude = get_gps_coords(gps_info, "GPSLatitude")
gps_latitude_ref = gps_info.get("GPSLatitudeRef")
gps_longitude = get_gps_coords(gps_info, "GPSLongitude")
gps_longitude_ref = gps_info.get("GPSLongitudeRef")
lat = convert_to_degrees(gps_latitude)
if gps_latitude_ref != "N":
lat = 0 - lat
lng = convert_to_degrees(gps_longitude)
if gps_longitude_ref != "E":
lng = 0 - lng
return lat, lng
def get_gps_coords(gps_info, gps_tag):
"""
获取GPS信息中的经纬度坐标值
"""
coords = gps_info.get(gps_tag)
if coords is None:
return None
num, den = coords[0], coords[1]
val = float(num) / float(den)
for i in range(2, len(coords)):
num, den = coords[i], coords[i + 1] if i + 1 < len(coords) else 1
val += (float(num) / float(den)) / 60.0
return val
def convert_to_degrees(value):
"""
将经纬度转换为以度为单位的值
"""
deg, minutes = int(value[0]), value[1:]
val = deg + (float(minutes[0]) / float(minutes[1])) / 60.0
return val
# 调用上述函数来获取图片的经纬度信息
exif_data = get_exif_data("image.jpg")
lat, lng = get_geotagging(exif_data)
print("经度:", lng)
print("纬度:", lat)
```
需要注意的是,无人机拍摄的图片可能不包含GPS信息,或是GPS信息不完整甚至不正确,此时获取到的经纬度信息就可能是不准确的。
python 两个经纬度点判断方位
可以使用以下代码来判断两个经纬度点之间的方位:
```python
import math
def bearing(lat1, lon1, lat2, lon2):
"""
计算两个经纬度点之间的方位角度
"""
lat1 = math.radians(lat1)
lon1 = math.radians(lon1)
lat2 = math.radians(lat2)
lon2 = math.radians(lon2)
delta_lon = lon2 - lon1
y = math.sin(delta_lon) * math.cos(lat2)
x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(delta_lon)
bearing = math.atan2(y, x)
return math.degrees(bearing)
# 输入两个经纬度点
lat1 = 39.908722
lon1 = 116.397499
lat2 = 39.908722
lon2 = 116.397500
# 计算方位角度
bearing_angle = bearing(lat1, lon1, lat2, lon2)
# 判断方位
if bearing_angle >= 0 and bearing_angle < 90:
print('东北')
elif bearing_angle >= 90 and bearing_angle < 180:
print('东南')
elif bearing_angle >= 180 and bearing_angle < 270:
print('西南')
else:
print('西北')
```
其中,使用了`math`库的`radians()`函数将角度转换为弧度,然后根据经纬度计算出两个点之间的方位角度,最后根据方位角度判断方位。