Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

พล็อต Google Map โดยใช้แพ็คเกจ gmplot ใน Python หรือไม่


คุณสามารถวาดพิกัดทางภูมิศาสตร์บน Google Maps ได้หลายวิธี อย่างไรก็ตาม ในกรณีที่คุณต้องการบันทึกลงในไฟล์ในเครื่อง วิธีหนึ่งที่ดีกว่าที่จะทำได้คือการใช้โมดูลหลามที่เรียกว่า gmplot

Python library gmplot ช่วยให้เราสามารถลงจุดข้อมูลบน Google Maps gmplot มีอินเทอร์เฟซเหมือน matplotlib เพื่อสร้าง HTML และจาวาสคริปต์เพื่อส่งข้อมูลเพิ่มเติมทั้งหมดบน Google แผนที่

การติดตั้ง

ง่ายต่อการติดตั้ง gmplot โดยใช้ pip ในกรณีที่ยังไม่ได้ติดตั้ง gmplot -

pip install gmplot

เมื่อรันคำสั่งข้างต้น คุณอาจเห็นผลลัพธ์บางอย่างเช่น −

พล็อต Google Map โดยใช้แพ็คเกจ gmplot ใน Python หรือไม่

จากด้านบน เราจะเห็นการติดตั้ง gmplot-1.2.0 เวอร์ชันล่าสุดบนคอมพิวเตอร์ของเรา

ไลบรารี gmplot มีวิธีการวางแผนหลายวิธีเพื่อสร้างมุมมองแผนที่สำรวจได้ง่ายมาก Gmplot มีความยืดหยุ่นมากในการสร้าง Google map เนื่องจากเราสามารถใช้เพื่อสร้าง html ได้โดยตรง

ด้านล่างนี้คือวิธีต่างๆ ในการทำให้สำเร็จ -

Case1 − การสร้างแผนที่ฐานโดยใช้ gmplot

หากคุณต้องการวางแผนที่ไปยังตำแหน่งใดตำแหน่งหนึ่ง คุณต้องเขียนค่าละติจูด-ลองจิจูดของตำแหน่งนั้นและความละเอียดการซูม

# Import gmplot library.
from gmplot import *
# Place map
# First two arugments are the geogrphical coordinates .i.e. Latitude and Longitude
#and the zoom resolution.
gmap = gmplot.GoogleMapPlotter(17.438139, 78.39583, 18)
# Location where you want to save your file.
gmap.draw( "C:\\Users\\rajesh\\Desktop\\map11.html" )

ผลลัพธ์1

พล็อต Google Map โดยใช้แพ็คเกจ gmplot ใน Python หรือไม่

หมายเหตุ − การแสดงผลด้านบนของหน้าจอ เราเห็นสิ่งนี้เนื่องจากบริการ Google Maps ไม่ฟรีในขณะนี้ ในกรณีที่คุณเข้าถึงผ่าน API คุณต้องเพิ่ม API_KEY เพื่อดูมุมมองแผนที่ Google ที่ดีขึ้น ด้านล่างนี้คือรหัสที่จะบรรลุสิ่งนี้ -

กรณีที่ 1 (โดยเพิ่ม GOOGLE_API_KEY)

การสร้างแผนที่ฐานโดยใช้ gmplot

# Import gmplot library.
from gmplot import *
# Place map
# First two arugments are the geogrphical coordinates .i.e. Latitude and Longitude
#and the zoom resolution.
gmap=gmplot.GoogleMapPlotter(17.438139, 78.39583, 18)
# Because google maps is not a free service now, you need to get an api key. Else commenting
# below line you will see the maps with "For Development Purpose Only" on the screen and maps
# with low resolution.
#gmap.apikey = "Your_API_KEY"
gmap.apikey = "AIzaSyDeRNMnZ__VnQDiATiuz4kPjF_c9r1kWe8"
# Location where you want to save your file.
gmap.draw( "C:\\Users\\rajesh\\Desktop\\map11.html" )

หมายเหตุ - คุณต้องเพิ่มคีย์ API ของ Google แผนที่ ('Your_API_KEY') และตั้งค่าให้เท่ากับ gmap.apikey ผลลัพธ์ด้านล่างกำลังมาเพราะฉันใช้รหัสของตัวเอง ซึ่งคุณสามารถรับได้จากลิงก์ด้านล่าง −

https://developers.google.com/maps/documentation/embed/get-api-key

ผลลัพธ์ 1

พล็อต Google Map โดยใช้แพ็คเกจ gmplot ใน Python หรือไม่

กรณีที่ 2 − การวาดรูปหลายเหลี่ยมบน Google map

# import gmplot package
import gmplot
latitude_list = [ 17.4567417, 17.5587901, 17.6245545]
longitude_list = [ 78.2913637, 78.007699, 77.9266135 ]
gmap = gmplot.GoogleMapPlotter(17.438139, 78.3936413, 11)
gmap.scatter( latitude_list, longitude_list, '# FF0000', size = 40, marker = False)
# polygon method Draw a polygon with
# the help of coordinates
gmap.polygon(latitude_list, longitude_list, color = 'cornflowerblue')
gmap.apikey = "Your_API_KEY"
gmap.draw( "C:\\Users\\rajesh\\Desktop\\map3.html" )

ผลลัพธ์ 2

พล็อต Google Map โดยใช้แพ็คเกจ gmplot ใน Python หรือไม่

กรณีที่ 3 − กระจายจุดบนแผนที่ google และลากเส้นระหว่างพิกัดที่กำหนด

# import gmplot package
import gmplot
#Set different latitude and longitude points
Charminar_top_attraction_lats, Charminar_top_attraction_lons = zip(*[
   (17.3833, 78.4011),(17.4239, 78.4738),(17.3713, 78.4804),(17.3616, 78.4747),
   (17.3578, 78.4717),(17.3604, 78.4736),(17.2543, 78.6808),(17.4062, 78.4691),
   (17.3950, 78.3968),(17.3587, 78.2988),(17.4156, 78.4750)])
#declare the center of the map, and how much we want the map zoomed in
gmap3 = gmplot.GoogleMapPlotter(17.3616, 78.4747, 13)
# Scatter map
gmap3.scatter( Charminar_top_attraction_lats, Charminar_top_attraction_lons, '#FF0000',size = 50, marker = False )
# Plot method Draw a line in between given coordinates
gmap3.plot(Charminar_top_attraction_lats, Charminar_top_attraction_lons, 'cornflowerblue', edge_width = 3.0)
#Your Google_API_Key
gmap.apikey = " API_Key”
# save it to html
gmap3.draw(r"c:\users\rajesh\desktop\maps\scatter.html")

ผลลัพธ์ 3

พล็อต Google Map โดยใช้แพ็คเกจ gmplot ใน Python หรือไม่

กรณีที่ 4:แผนที่ความร้อนและกระจายในกราฟเดียวเพื่อแสดงแผ่นดินไหว

#Import important libraries
import gmplot
import numpy as np
# generate 700 random lats and lons
latitude = (np.random.random_sample(size = 700) - 0.5) * 180
longitude = (np.random.random_sample(size = 700) - 0.5) * 360
# declare the center of the map, and how much we want the map zoomed in
gmap = gmplot.GoogleMapPlotter(0, 0, 2)
# plot heatmap
gmap.heatmap(latitude, longitude)
gmap.scatter(latitude, longitude, c='r', marker=True)
#Your Google_API_Key
gmap.apikey = " Your_Google_API_Key "
# save it to html
gmap.draw(r"c:\users\rajesh\desktop\maps\country_heatmap.html")

ผลลัพธ์

พล็อต Google Map โดยใช้แพ็คเกจ gmplot ใน Python หรือไม่