Automatic spatial transformation cannot form projection

I have followed this documentation, however, when I run the documentation example, I get the error message at the end of the code block below:

>>> from django.contrib.gis.geos import GEOSGeometry, Point
>>> pnt = Point(954158.1, 4215137.1, srid=32140)
>>> pnt = GEOSGeometry('SRID=32140;POINT(954158.1 4215137.1)')
>>> qs = WorldBorder.objects.filter(mpoly__intersects=pnt)
>>> print(qs.query)
SELECT "world_worldborder"."id", "world_worldborder"."name", "world_worldborder"."area", "world_worldborder"."pop2005", "world_worldborder"."fips", "world_worldborder"."iso2", "world_worldborder"."iso3", "world_worldborder"."un", "world_worldborder"."region", "world_worldborder"."subregion", "world_worldborder"."lon", "world_worldborder"."lat", "world_worldborder"."mpoly"::bytea FROM "world_worldborder" WHERE ST_Intersects("world_worldborder"."mpoly", ST_Transform(ST_GeomFromEWKB('\001\001\000\000 \214}\000\0003333\\\036-AfffFX\024PA'::bytea), 4326))
>>> qs
Traceback (most recent call last):
  File "C:\Users\USER\Documents\Internships\Kodecamp\geodjango_tutorial\proj-venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InternalError_: could not form projection (PJ) from 'srid=32140' to 'srid=4326'

The project’s requirements.txt file is:

asgiref==3.4.1
Django==4.0
sqlparse==0.4.2
tzdata==2021.5
psycopg2==2.9.2

I have also installed PostGIS; and PROJ, GDAL and GEOS libraries.

Maybe you could try:

gdalsrsinfo EPSG:32140

in a terminal. Also please tell us your GDAL installed version.

Thanks, @claudep.

The information I got after trying:

gdalsrsinfo EPSG:32140

can be found below:

USER@DESKTOP-C4A1VA0 MINGW64 ~/Documents/Internships/Kodecamp/geodjango_tutorial/geodjango
$ gdalsrsinfo EPSG:32140

PROJ.4 : +proj=lcc +lat_0=27.8333333333333 +lon_0=-99 +lat_1=30.2833333333333 +lat_2=28.3833333333333 +x_0=600000 +y_0=4000000 +datum=NAD83 +units=m +no_defs

OGC WKT2:2018 :
PROJCRS["NAD83 / Texas South Central",
    BASEGEOGCRS["NAD83",
        DATUM["North American Datum 1983",
            ELLIPSOID["GRS 1980",6378137,298.257222101,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4269]],
    CONVERSION["SPCS83 Texas South Central zone (meters)",
        METHOD["Lambert Conic Conformal (2SP)",
            ID["EPSG",9802]],
        PARAMETER["Latitude of false origin",27.8333333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8821]],
        PARAMETER["Longitude of false origin",-99,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8822]],
        PARAMETER["Latitude of 1st standard parallel",30.2833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8823]],
        PARAMETER["Latitude of 2nd standard parallel",28.3833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8824]],
        PARAMETER["Easting at false origin",600000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8826]],
        PARAMETER["Northing at false origin",4000000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8827]]],
    CS[Cartesian,2],
        AXIS["easting (X)",east,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["northing (Y)",north,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Engineering survey, topographic mapping."],
        AREA["United States (USA) - Texas - counties of Aransas; Atascosa; Austin; Bandera; Bee; Bexar; Brazoria; Brewster; Caldwell; Calhoun; Chambers; Colorado; Comal; De Witt; Dimmit; Edwards; Fayette; Fort Bend; Frio; Galveston; Goliad; Gonzales; Guadalupe; Harris; Hays; Jackson; Jefferson; Karnes; Kendall; Kerr; Kinney; 
La Salle; Lavaca; Live Oak; Matagorda; Maverick; McMullen; Medina; Presidio; Real; Refugio; Terrell; Uvalde; 
Val Verde; Victoria; Waller; Wharton; Wilson; Zavala."],
        BBOX[27.78,-105,30.67,-93.76]],
    ID["EPSG",32140]]

The GDAL version is 3.4.0.

This looks right. Anyway, this is probably still related to your specific install of GIS libraries. Difficult for me to say what’s wrong, sorry :frowning:

1 Like