Pythonを使用して都市の経度と緯度を取得するにはどうすればよいですか?
都市の経度と緯度を取得するには、 geopyを使用します モジュール。 ジオピー サードパーティのジオコーダーやその他のデータソースを使用して、住所、都市、国などの座標を特定します。
まず、ジオピーを確認してください モジュールがインストールされています-
pip install geopy
次の例では、 Nominatimを使用します 都市「ハイデラバード」の経度と緯度を見つけるためのジオコーダー。
ステップ-
-
ノミナティムジオコーダーをインポートします geopyから モジュール。
-
Nominatim APIを初期化し、 geocodeを使用します 入力文字列の場所を取得するメソッド。
-
最後に、 location.latitudeで場所の緯度と経度を取得します およびlocation.longitude 。
例1
# Import the required library
from geopy.geocoders import Nominatim
# Initialize Nominatim API
geolocator = Nominatim(user_agent="MyApp")
location = geolocator.geocode("Hyderabad")
print("The latitude of the location is: ", location.latitude)
print("The longitude of the location is: ", location.longitude) 出力
コンソールに次の出力が出力されます-
The latitude of the location is: 17.360589 The longitude of the location is: 78.4740613
この例では、例1の反対を実行します。まず、座標のセットを提供し、それらの座標が表す都市、州、国を見つけます。コンソールに出力を印刷する代わりに、出力を表示する4つのラベルを持つtkinterウィンドウを作成します。
ステップ-
-
NominatiumAPIを初期化します。
-
geolocator.reverse()を使用します 関数を使用して座標(緯度と経度)を指定し、位置データを取得します。
-
location.raw ['address']を使用して場所のアドレスを取得します データをトラバースして、 address.get()を使用して都市、州、国を見つけます 。
-
tkinterウィンドウ内にラベルを作成してデータを表示します。
例2
from tkinter import *
from geopy.geocoders import Nominatim
# Create an instance of tkinter frame
win = Tk()
# Define geometry of the window
win.geometry("700x350")
# Initialize Nominatim API
geolocator = Nominatim(user_agent="MyApp")
# Latitude & Longitude input
coordinates = "17.3850 , 78.4867"
location = geolocator.reverse(coordinates)
address = location.raw['address']
# Traverse the data
city = address.get('city', '')
state = address.get('state', '')
country = address.get('country', '')
# Create a Label widget
label1=Label(text="Given Latitude and Longitude: " + coordinates, font=("Calibri", 24, "bold"))
label1.pack(pady=20)
label2=Label(text="The city is: " + city, font=("Calibri", 24, "bold"))
label2.pack(pady=20)
label3=Label(text="The state is: " + state, font=("Calibri", 24, "bold"))
label3.pack(pady=20)
label4=Label(text="The country is: " + country, font=("Calibri", 24, "bold"))
label4.pack(pady=20)
win.mainloop() 出力
次の出力が生成されます-
-
Pythonを使用して一連のポイントの中心を取得するにはどうすればよいですか?
一連の点の中心を取得するには、リストのすべての要素を追加し、その合計をリストの長さで割って、結果が対応する軸の中心になるようにします。 ステップ データポイントのリストを2つ作成します。 plot()を使用してxおよびyデータポイントをプロットします メソッド。 xおよびyデータポイントの中央のタプルを取得します。 プロットに中心点を配置します。 xおよびyデータポイントの中心のラベルとして中心に注釈を付けます。 図を表示するには、 show()を使用します メソッド。 例 from matplotlib import pyplot as plt pl
-
PythonでTkinterを使用してディレクトリを選択し、場所を保存するにはどうすればよいですか?
私たちはダイアログボックスに精通しており、多くの種類のアプリケーションでダイアログボックスを操作しました。このようなタイプのダイアログは、ユーザーの操作が最も必要なアプリケーションを作成する場合に役立ちます。ダイアログボックスを使用して、ユーザーにさまざまな種類のファイルを選択してから、ファイルの読み取り、ファイルへの書き込みなどの特定の操作を実行するように求めることができます。ダイアログボックスは、ファイルダイアログ Pythonのモジュール。 例 この例では、ローカルディレクトリからファイルを選択するようにユーザーに要求し、ラベルを使用してディレクトリの場所を表示するアプリケーションを作