Computer >> Máy Tính >  >> Lập trình >> Python

Vẽ dữ liệu trên Bản đồ Google bằng gói pygmaps?

Thư viện pygmaps trong Python cung cấp một trình bao bọc cho api javascript của google maps. Với thư viện này, người dùng python có thể tạo giao diện giống như matplotlib để tạo html và javascript và sau đó có thể mô tả tất cả thông tin bổ sung mà người dùng muốn thêm trên Google Maps.

Thư viện bắt buộc

Chúng tôi sẽ chỉ sử dụng thư viện / gói pygmaps. Bạn có thể cài đặt thư viện pygmaps bằng pip, như:

$pip install pygmaps (windows os)
$sudo pip3 install pygmaps (linux os)

Chúng tôi sẽ viết một chương trình sẽ hiển thị -

  • Tạo bản đồ bằng pygmaps bằng cách cung cấp độ dài, vĩ độ và mức thu phóng.

  • Đặt lưới trên bản đồ bằng cách cung cấp điểm bắt đầu và điểm kết thúc dài &vĩ độ cho lưới và kích thước lưới.

  • Thêm một điểm trên bản đồ và hiển thị nó bằng cách đặt một số màu cho nó.

  • Vẽ một vòng tròn xung quanh một điểm trên bản đồ với bán kính tính bằng mét.

  • Vẽ đường vào bản đồ bằng cách cung cấp danh sách các điểm dài và vĩ độ.

Dưới đây là việc triển khai các chức năng khác nhau thông qua pygmap.

import pygmaps
########## CONSTRUCTOR: pygmaps.maps(latitude, longitude, zoom) ##############################
# DESC:initialize a map with latitude and longitude of center point
#and map zoom level "15"
# PARAMETER1:latitude (float) latittude of map center point
# PARAMETER2:longitude (float) latittude of map center point
# PARAMETER3:zoom (int) map zoom level 0~20
# RETURN:the instant of pygmaps
#========================================================================================
mymap = pygmaps.pygmaps(17.45,78.29, 15)


########## FUNCTION: setgrids(start-Lat, end-Lat, Lat-interval, start-Lng, end-Lng, Lng-interval) ######
# DESC:set grids on map
# PARAMETER1:start-Lat (float), start (minimum) latittude of the grids
# PARAMETER2:end-Lat (float), end (maximum) latittude of the grids
# PARAMETER3:Lat-interval (float) grid size in latitude
# PARAMETER4:start-Lng (float), start (minimum) longitude of the grids
# PARAMETER5:end-Lng (float), end (maximum) longitude of the grids
# PARAMETER6:Lng-interval (float) grid size in longitude
# RETURN:no returns
#========================================================================================
mymap.setgrids(17.45, 17.46, 0.001, 78.29,78.30, 0.001)

########## FUNCTION: addpoint(latitude, longitude, [color])#############################
# DESC:add a point into a map and dispaly it, color is optional default is red
# PARAMETER1:latitude (float) latitude of the point
# PARAMETER2:longitude (float) longitude of the point
# PARAMETER3:color (string) color of the point showed in map, using HTML color code
#HTML COLOR CODE: https://www.computerhope.com/htmcolor.htm
#e.g. red "#FF0000", Blue "#0000FF", Green "#00FF00"
# RETURN:no return
#========================================================================================
mymap.addpoint(17.45,78.29, "#FF0000","Hello")


########## FUNCTION: addradpoint(latitude, longitude, radius, [color])##################
# DESC: add a point with a radius (Meter) - Draw cycle
# PARAMETER1:latitude (float) latitude of the point
# PARAMETER2:longitude (float) longitude of the point
# PARAMETER3:radius (float), radius in meter
# PARAMETER4:color (string) color of the point showed in map, using HTML color code
#HTML COLOR CODE: https://www.computerhope.com/htmcolor.htm
#e.g. red "#FF0000", Blue "#0000FF", Green "#00FF00"
# RETURN:no return
#========================================================================================
mymap.addradpoint(17.45,78.29, 150, "#0000FF")


########## FUNCTION: addpath(path,[color])##############################################
# DESC:add a path into map, the data struceture of Path is a list of points
# PARAMETER1:path (list of coordinates) e.g. [(lat1,lng1),(lat2,lng2),...]
# PARAMETER2:color (string) color of the point showed in map, using HTML color code
#HTML COLOR CODE: https://www.computerhope.com/htmcolor.htm
#e.g. red "#FF0000", Blue "#0000FF", Green "#00FF00"
# RETURN:no return
#========================================================================================
path = [(17.45,78.29),
(17.55, 78.39),
(17.65,78.49),
]
mymap.addpath(path,"#00FF00")

########## FUNCTION: draw(file)######################################################
# DESC:create the html map file (.html)
# PARAMETER1:file (string) the map path and file
# RETURN:no return, generate html file in specified directory
#========================================================================================
mymap.draw('./mymap.html')

print('OK')

Lưu ý :Bạn có thể gặp phải Typeerror khi chạy hàm mymap.addpoint, giống như

TypeError :addpoint () nhận từ 3 đến 4 đối số vị trí nhưng 5 đối số đã được đưa ra

Để tránh điều đó, tôi đã thêm đối số tiêu đề vào hàm addpoint của gói pygmaps.py.

Đầu ra

Vẽ dữ liệu trên Bản đồ Google bằng gói pygmaps?

Như bạn có thể thấy từ các bản đồ trên, chúng tôi có thể hình dung nhiều chức năng trong một bản đồ duy nhất.