﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34302	SpatialReference.srid incorrectly assumes first AUTHORITY value to be projection SRID	Stefan Brand	Stefan Brand	"== Problem

When reading data into our PostGIS database, we noticed strange locations for our geometries. The data was supposed to be `EPSG:31287`, but investigation showed that Django was assuming `EPSG:6312`. This led to wrong EWKTs when saving geometries to PostGIS.

It turns out that the dataset did not have any SRID stored for the projection, just for the DATUM. Django assumed the DATUM's AUTHORITY value to be the SRID of the dataset's projection. This '''contradicts the [https://docs.djangoproject.com/en/4.1/ref/contrib/gis/gdal/#django.contrib.gis.gdal.SpatialReference.srid documentation]''' for `SpatialReference.srid`:

> Returns the SRID of top-level authority, or None if undefined. 

Since the layer's `srid` cannot be set to a different value, the only workaround is to set the `srid` for each `OGRGeometry` individually.

== Current Result

[https://github.com/django/django/blob/0265b1b49ba10f957abfd1311d0bae0ecefc3111/django/contrib/gis/gdal/srs.py#L208 `SpatialReference.srid`] calls `int(self.attr_value(""AUTHORITY"", 1))`, which returns `6312` (wrong SRID).

== Expected Result

`SpatialReference.srid` should call `auth_code(target=None)` to get the top-level SRID.

Django should not assume a wrong SRID if the projection does not have a SRID. For example, QGIS reads the whole spatial reference information, correctly projects the data, but displays ""Unknown CRS"" because it does not assume any AUTHORITY value to be the dataset SRID.

== Steps to reproduce

{{{
#!python
from django.contrib.gis.gdal import SpatialReference

WKT = """"""PROJCRS[""MGI / Austria Lambert"",
    BASEGEOGCRS[""MGI"",
        DATUM[""Militar-Geographische Institut"",
            ELLIPSOID[""Bessel 1841"",6377397.155,299.1528128,
                LENGTHUNIT[""metre"",1]],
            ID[""EPSG"",6312]],
        PRIMEM[""Greenwich"",0,
            ANGLEUNIT[""Degree"",0.0174532925199433]]],
    CONVERSION[""unnamed"",
        METHOD[""Lambert Conic Conformal (2SP)"",
            ID[""EPSG"",9802]],
        PARAMETER[""Latitude of false origin"",47.5,
            ANGLEUNIT[""Degree"",0.0174532925199433],
            ID[""EPSG"",8821]],
        PARAMETER[""Longitude of false origin"",13.3333333333333,
            ANGLEUNIT[""Degree"",0.0174532925199433],
            ID[""EPSG"",8822]],
        PARAMETER[""Latitude of 1st standard parallel"",49,
            ANGLEUNIT[""Degree"",0.0174532925199433],
            ID[""EPSG"",8823]],
        PARAMETER[""Latitude of 2nd standard parallel"",46,
            ANGLEUNIT[""Degree"",0.0174532925199433],
            ID[""EPSG"",8824]],
        PARAMETER[""Easting at false origin"",400000,
            LENGTHUNIT[""metre"",1],
            ID[""EPSG"",8826]],
        PARAMETER[""Northing at false origin"",400000,
            LENGTHUNIT[""metre"",1],
            ID[""EPSG"",8827]]],
    CS[Cartesian,2],
        AXIS[""easting"",east,
            ORDER[1],
            LENGTHUNIT[""metre"",1,
                ID[""EPSG"",9001]]],
        AXIS[""northing"",north,
            ORDER[2],
            LENGTHUNIT[""metre"",1,
                ID[""EPSG"",9001]]]]""""""



geodjango_srs = SpatialReference(WKT)
geodjango_srs.validate()  # raises for invalid SRS
print(geodjango_srs.attr_value(""AUTHORITY"", 1))
print(geodjango_srs.auth_code(target=None))
}}}

> 6312
> None

"	Bug	closed	GIS	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
