OSGeo / PROJ

PROJ - Cartographic Projections and Coordinate Transformations Library
https://proj.org
Other
1.74k stars 788 forks source link

BUG: Isssues parsing WKT string with newline and whitespace #2498

Closed snowman2 closed 3 years ago

snowman2 commented 3 years ago

Example of problem

projinfo 'PROJCS["Albers Conical Equal Area",\n    GEOGCS["NAD83",\n        DATUM["North_American_Datum_1983",\n            SPHEROID["GRS 1980",6378137,298.257222101,\n                AUTHORITY["EPSG","7019"]],\n            TOWGS84[0,0,0,-0,-0,-0,0],\n            AUTHORITY["EPSG","6269"]],\n        PRIMEM["Greenwich",0,\n            AUTHORITY["EPSG","8901"]],\n        UNIT["degree",0.0174532925199433,\n            AUTHORITY["EPSG","9122"]],\n        AUTHORITY["EPSG","4269"]],\n    PROJECTION["Albers_Conic_Equal_Area"],\n    PARAMETER["standard_parallel_1",29.5],\n    PARAMETER["standard_parallel_2",45.5],\n    PARAMETER["latitude_of_center",23],\n    PARAMETER["longitude_of_center",-96],\n    PARAMETER["false_easting",0],\n    PARAMETER["false_northing",0],\n    UNIT["meters",1]]'
input string: parsing of user string failed: missing [

Same string with \n removed.

$ projinfo 'PROJCS["Albers Conical Equal Area",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,-0,-0,-0,0],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["standard_parallel_1",29.5],PARAMETER["standard_parallel_2",45.5],PARAMETER["latitude_of_center",23],PARAMETER["longitude_of_center",-96],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["meters",1]]'
PROJ.4 string:
+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs

WKT2:2019 string:
BOUNDCRS[
    SOURCECRS[
        PROJCRS["Albers Conical Equal Area",

I had the same issue with proj_create:

>>> from pyproj import CRS
>>> import re
>>> crs = """
... PROJCS["Albers Conical Equal Area",
...     GEOGCS["NAD83",
...         DATUM["North_American_Datum_1983",
...             SPHEROID["GRS 1980",6378137,298.257222101,
...                 AUTHORITY["EPSG","7019"]],
...             TOWGS84[0,0,0,-0,-0,-0,0],
...             AUTHORITY["EPSG","6269"]],
...         PRIMEM["Greenwich",0,
...             AUTHORITY["EPSG","8901"]],
...         UNIT["degree",0.0174532925199433,
...             AUTHORITY["EPSG","9122"]],
...         AUTHORITY["EPSG","4269"]],
...     PROJECTION["Albers_Conic_Equal_Area"],
...     PARAMETER["standard_parallel_1",29.5],
...     PARAMETER["standard_parallel_2",45.5],
...     PARAMETER["latitude_of_center",23],
...     PARAMETER["longitude_of_center",-96],
...     PARAMETER["false_easting",0],
...     PARAMETER["false_northing",0],
...     UNIT["meters",1]]
... """
>>> CRS(crs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/snowal/scripts/pyproj/pyproj/crs/crs.py", line 296, in __init__
    super().__init__(projstring)
  File "pyproj/_crs.pyx", line 2302, in pyproj._crs._CRS.__init__
pyproj.exceptions.CRSError: Invalid projection: 
PROJCS["Albers Conical Equal Area",
    GEOGCS["NAD83",
        DATUM["North_American_Datum_1983",
            SPHEROID["GRS 1980",6378137,298.257222101,
                AUTHORITY["EPSG","7019"]],
            TOWGS84[0,0,0,-0,-0,-0,0],
            AUTHORITY["EPSG","6269"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4269"]],
    PROJECTION["Albers_Conic_Equal_Area"],
    PARAMETER["standard_parallel_1",29.5],
    PARAMETER["standard_parallel_2",45.5],
    PARAMETER["latitude_of_center",23],
    PARAMETER["longitude_of_center",-96],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["meters",1]]
: (Internal Proj Error: proj_create: unrecognized format / unknown name)
>>> CRS(re.sub(",\s+", ",", crs).strip())
<Bound CRS: PROJCS["Albers Conical Equal Area",GEOGCS["NAD83", ...>
Name: Albers Conical Equal Area
Axis Info [cartesian]:
- E[east]: Easting (meters)
- N[north]: Northing (meters)
Area of Use:
- undefined
Coordinate Operation:
- name: Transformation from NAD83 to WGS84
- method: Position Vector transformation (geog2D domain)
Datum: North American Datum 1983
- Ellipsoid: GRS 1980
- Prime Meridian: Greenwich
Source CRS: Albers Conical Equal Area

But, it works fine with GDAL python bindings:

>>> from osgeo import osr
>>> srs = osr.SpatialReference()
>>> crs = """
... PROJCS["Albers Conical Equal Area",
...     GEOGCS["NAD83",
...         DATUM["North_American_Datum_1983",
...             SPHEROID["GRS 1980",6378137,298.257222101,
...                 AUTHORITY["EPSG","7019"]],
...             TOWGS84[0,0,0,-0,-0,-0,0],
...             AUTHORITY["EPSG","6269"]],
...         PRIMEM["Greenwich",0,
...             AUTHORITY["EPSG","8901"]],
...         UNIT["degree",0.0174532925199433,
...             AUTHORITY["EPSG","9122"]],
...         AUTHORITY["EPSG","4269"]],
...     PROJECTION["Albers_Conic_Equal_Area"],
...     PARAMETER["standard_parallel_1",29.5],
...     PARAMETER["standard_parallel_2",45.5],
...     PARAMETER["latitude_of_center",23],
...     PARAMETER["longitude_of_center",-96],
...     PARAMETER["false_easting",0],
...     PARAMETER["false_northing",0],
...     UNIT["meters",1]]
... """
>>> srs.ImportFromWkt(crs)
0
>>> srs.ExportToWkt()
'PROJCS["Albers Conical Equal Area",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["standard_parallel_1",29.5],PARAMETER["standard_parallel_2",45.5],PARAMETER["latitude_of_center",23],PARAMETER["longitude_of_center",-96],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["meters",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'

Expected Output

No error.

Environment Information

Installation method

rouault commented 3 years ago

For the first part, I don't see this as a bug. \n here is understood as 2 characters, not the newline character.

If instead you try the following, it will work:

projinfo "$(printf 'PROJCS["Albers Conical Equal Area",\n    GEOGCS["NAD83",\n        DATUM["North_American_Datum_1983",\n            SPHEROID["GRS 1980",6378137,298.257222101,\n                AUTHORITY["EPSG","7019"]],\n            TOWGS84[0,0,0,-0,-0,-0,0],\n            AUTHORITY["EPSG","6269"]],\n        PRIMEM["Greenwich",0,\n            AUTHORITY["EPSG","8901"]],\n        UNIT["degree",0.0174532925199433,\n            AUTHORITY["EPSG","9122"]],\n        AUTHORITY["EPSG","4269"]],\n    PROJECTION["Albers_Conic_Equal_Area"],\n    PARAMETER["standard_parallel_1",29.5],\n    PARAMETER["standard_parallel_2",45.5],\n    PARAMETER["latitude_of_center",23],\n    PARAMETER["longitude_of_center",-96],\n    PARAMETER["false_easting",0],\n    PARAMETER["false_northing",0],\n    UNIT["meters",1]]')"

Regarding leading space/newline, addressed per 93d8fcc

snowman2 commented 3 years ago

Thanks :+1: