Skip to content

ENH: add guess_wkt_version to return the WKT version of a string#1606

Open
madhavcodez wants to merge 1 commit into
pyproj4:mainfrom
madhavcodez:enh/guess-wkt-version
Open

ENH: add guess_wkt_version to return the WKT version of a string#1606
madhavcodez wants to merge 1 commit into
pyproj4:mainfrom
madhavcodez:enh/guess-wkt-version

Conversation

@madhavcodez

Copy link
Copy Markdown

ENH: add guess_wkt_version to return the WKT version of a string

What

Adds pyproj.crs.guess_wkt_version(wkt), which returns the
pyproj.enums.WktVersion of a Well-Known Text string, or None when the
string is not WKT.

Why

is_wkt only answers whether a string is WKT (a bool). PROJ's
proj_context_guess_wkt_dialect already reports the specific dialect, so this
exposes that information. This is the guess_wkt_version API proposed by the
maintainer in #1031.

Dialect mapping:

  • PJ_GUESSED_WKT2_2019 (and its legacy alias PJ_GUESSED_WKT2_2018) -> WktVersion.WKT2_2019
  • PJ_GUESSED_WKT2_2015 -> WktVersion.WKT2_2015
  • PJ_GUESSED_WKT1_GDAL -> WktVersion.WKT1_GDAL
  • PJ_GUESSED_WKT1_ESRI -> WktVersion.WKT1_ESRI
  • PJ_GUESSED_NOT_WKT -> None

Changes

  • pyproj/_crs.pyx: new guess_wkt_version function mirroring is_wkt.
  • pyproj/proj.pxi: declare the current PJ_GUESSED_WKT2_2019 enum member.
  • pyproj/_crs.pyi: type stub.
  • pyproj/crs/__init__.py: export the function and add it to __all__.
  • docs/api/crs/crs.rst: autofunction entry.
  • docs/history.rst: changelog bullet.
  • test/crs/test_crs.py: per-dialect round-trip tests plus a non-WKT case.

Test plan

  • Built locally against PROJ 9.8.1 (conda-forge) and ran test/crs/test_crs.py.
  • Per-dialect round-trip via CRS.to_wkt(version=...) returns the matching WktVersion.
  • A PROJ string returns None.

Closes #1031

is_wkt only reports whether a string is WKT. guess_wkt_version wraps
proj_context_guess_wkt_dialect and returns the matching
pyproj.enums.WktVersion, or None when the string is not WKT.

Closes pyproj4#1031
Comment thread pyproj/_crs.pyx
Comment on lines +73 to +81
if dialect == PJ_GUESSED_WKT2_2019:
return WktVersion.WKT2_2019
if dialect == PJ_GUESSED_WKT2_2015:
return WktVersion.WKT2_2015
if dialect == PJ_GUESSED_WKT1_GDAL:
return WktVersion.WKT1_GDAL
if dialect == PJ_GUESSED_WKT1_ESRI:
return WktVersion.WKT1_ESRI
return None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is just the python programmer in me, but this feels like it would work better as a dictionary .get:

return WKT_DIALECTS.get(dialect)

where the dictionary is a mapping of the dialect strings and the WktVersion value. I assume these constants being compared against are just strings or integers so the dictionary hashing isn't a problem. I don't feel that strongly about it, but I do think it may be cleaner and more flexible in the long run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: allow to check for specific WKT flavor in the is_wkt() function

2 participants