2

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'

Found a solution that worked for me.

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',.......]
rr_goyal
  • 467
  • 2
  • 8
  • What's the version of pyproj you are using? – rr_goyal Jul 25 '23 at 07:08
  • pip install pyproj --no-binary pyproj Requirement already satisfied: pyproj in c:\users\fujitsu\pycharmprojects\seismic\venv\lib\site-packages (3.6.0) Requirement already satisfied: certifi in c:\users\fujitsu\pycharmprojects\seismic\venv\lib\site-packages (from pyproj) (2023.5.7) – Hanzo Hasashi Jul 25 '23 at 07:43
  • 1
    Your posted code uses `crs_list = get_crs_list()`, but in the error message it is `crs_list = pyproj.get_crs_list()`. Your posted code should not get such error. – acw1668 Jul 25 '23 at 08:14
  • Hey. Sorry about that I think I pasted the error message from another solution I tried. I managed to find a solution for my issue. Thanks anyways. – Hanzo Hasashi Jul 26 '23 at 05:35

1 Answers1

0

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)

After referring to the documentation, the modified final solution -

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!

rr_goyal
  • 467
  • 2
  • 8
  • Let me know how it goes! – rr_goyal Jul 25 '23 at 23:57
  • Thanks for the assist. crs_list = list(pyproj.database.get_codes(auth_name="all", pj_type="CRS")) print(crs_list) return sorted(crs_list) I ran this to check the list, but the output shows an empty list, but the program does work correctly. Output: [] Process finished with exit code 0 – Hanzo Hasashi Jul 26 '23 at 04:46
  • 1
    Hi again. Looking at the documentation again, I ran this code and the output is what I needed. 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',.......] Thank you so much for the help. – Hanzo Hasashi Jul 26 '23 at 05:34
  • Glad to know it helped you! – rr_goyal Jul 26 '23 at 06:06