تحديد الاحداتيات الجغرافية باستخدام كود بسيط بلغة بايتون
سنستخدم في هذه المهمة كود بسيط بلغة بايتون بحيت نعطيه اسم المدينة فيرجع لنا الاحداتيات الجغرافية مع امكانيات حساب المسافة بين مذينتين
اولا سنقوم باستدعاء المكتبات التالية :
#/usr/bin/python3
# encoding -*- utf-8 -*-
from geopy.geocoders import Nominatim
from geopy.distance import geodesic
from geopy.distance import great_circle
قم ب تحميل المكتبة باستخدام الكود التالي تتواجد لديك pip install geopy
cityes = []
locationes = []
lan_long =[]
c = 1
الان قمنا بتعريف تلات مصفوفات و متغير c يحمل قيمة واحد لغرض ايقاف الحلقة
while c <= count:
cit = input("City {} : ".format(c))
cityes.append(cit)
c = c+1
هذه عبارة عن حلقة تاخد منا المدن المراد اذخالها فتخزنها في المصفوفة cityes اعلاه
geolocator = Nominatim(user_agent='myapplication')
print(cityes)
location1 = geolocator.geocode(cityes[0])
location2 = geolocator.geocode(cityes[1])
cl1 = location1.latitude , location1.longitude
cl2 = location2.latitude , location2.longitude
print("Location {} is : {}".format(cityes[0], cl1))
print("Location {} is : {}".format(cityes[1], cl2))
هنا نقوم بجلب الاحداتيات خطوط الطول والعرض لكل مذينة ونطبعها
if len(cityes) == 2:
space = geodesic(cl1, cl2).km
print("\033[95m Scpace Betwen {} and {} is :\033[0m \033[96m {} km \033[0m".format(cl1,cl2,space))
#print(great_circle(cl1, cl2).km)
else:
c1,c2 =input("\n \033[95m Chose your City to Calculate ex= a-b : \033[0m ").split("-")
location1 = geolocator.geocode(c1)
location2 = geolocator.geocode(c2)
cl1 = location1.latitude , location1.longitude
cl2 = location2.latitude , location2.longitude
space = geodesic(cl1, cl2).km
print("\033[95m Scpace Betwen {} and {} is :\033[0m \033[96m {} km \033[0m".format(c1,c2,space))
هنا نتحقق ان كان مجموع المدن المدخلة تساوي اتنان تم نقوم بحساب المسافة بينهم مباشرة
اما ان كانو اكتر من اتناء فنساله لكي يختار مذينتين من اختياره
وهدا سكريبت بسيط يمكنك اخده وتطويره حسب غرضك والتجريب لصدد التعلم