I need help with a python program. I am looking to obtain crs codes/definitions from Pyrpoj library, add them to a list, allow the user to select a crs from the list and print it out, but it does not seem to work. I have made sure to update the library to the latest version. I will be grateful if you guys can help me out here.
Here is the code I tried.
import tkinter as tk
from tkinter import ttk
import pyproj
def get_crs_list():
# Get a list of available CRS codes from the pyproj CRS database
crs_list = list(pyproj.database.get_codes("CRS"))
return sorted(crs_list)
def print_selected_crs():
selected_crs = combobox.get()
print("Selected CRS:", selected_crs)
# Create the main application window
root = tk.Tk()
root.title("CRS Selector")
# Create a label and a drop-down list (Combobox) for selecting the CRS
label = ttk.Label(root, text="Select a CRS:")
label.pack(pady=10)
crs_list = get_crs_list()
combobox = ttk.Combobox(root, values=crs_list)
combobox.pack()
# Create a button to print the selected CRS
button = ttk.Button(root, text="Print CRS", command=print_selected_crs)
button.pack(pady=10)
# Start the main event loop
root.mainloop()
Error message:-
line 7, in get_crs_list
crs_list = pyproj.get_crs_list()
AttributeError: module 'pyproj' has no attribute 'get_crs_list'
def get_crs_list():
crs_info_list = pyproj.database.query_crs_info(auth_name=None, pj_types=None)
crs_list = ["EPSG:" + info[1] for info in crs_info_list]
print(crs_list)
return sorted(crs_list)
Output: ['EPSG:2000', 'EPSG:2001', 'EPSG:2002', 'EPSG:2003', 'EPSG:2004',.......]
As mentioned by @acw1668 in the comments, your error message looks different from the code you tried. However, if I run the same code you posted, I get the following error -
TypeError: get_codes() takes at least 2 positional arguments (1 given)
Following the official documentation, the function "pyproj.database.get_codes()" requires atleast 2 arguments. These are "auth_name" and "pj_type"
You can get the list of acceptable values for "auth_name" using -
pyproj.get_authorities()
The output that I get from above is -
['EPSG', 'ESRI', 'IAU_2015', 'IGNF', 'NKG', 'OGC', 'PROJ']
Now, for "pj_type" you have mentioned to use "CRS", therefore, a fix for your problem would be -
def get_crs_list():
# Get a list of available CRS codes from the pyproj CRS database
# SOLUTION
crs_list = list(pyproj.database.get_codes(auth_name="all", pj_type="CRS"))
return sorted(crs_list)
def get_crs_list():
crs_info_list = pyproj.database.query_crs_info(auth_name=None, pj_types=None)
crs_list = ["EPSG:" + info[1] for info in crs_info_list]
print(crs_list)
return sorted(crs_list)
Output: ['EPSG:2000', 'EPSG:2001', 'EPSG:2002', 'EPSG:2003', 'EPSG:2004',.......]
Thanks @Hanzo Hasashi for updating!