[postgis-users] function to find UTM zone SRID from POINT

Alex Mayrhofer axelm-postgis at nona.net
Wed Dec 7 06:42:58 PST 2005


Paul Ramsey wrote:
> Nope, no function. When you invent that wheel, submit it as a patch. A 
> pl/pgsql or sql function should be sufficient.

Here's a prototype which works for me. It currently does _not_ handle the 
exceptions in UTM zones around norway etc...

I'd appreciate if someone could doublecheck my algorithm. BTW, the function 
requires PostGIS, but neither GEOS nor PROJ.

---snip---

--- returns UTM zone SRID of a given point
--- point is a POINT of  2 or more dimensions in WGS84
--- (means that input point SRID must be 4326)
CREATE OR REPLACE FUNCTION find_utm_srid (
         p geometry) RETURNS integer AS $$

DECLARE
         out_srid integer;
         base_srid integer;

BEGIN
         IF srid(p) != 4326 THEN
                 RAISE NOTICE 'find_utm_srid: input geometry has wrong SRID 
(%)',
  srid(p);
                 RETURN NULL;
         END IF;

         IF y(p) < 0 THEN
                 --- south hemisphere
                 base_srid := 32700;
         ELSE
                 --- north hemisphere or on equator
                 base_srid := 32600;
         END IF;

         out_srid := base_srid + floor((x(p)+186)/6);
         IF (x(p) = 180) THEN
                 out_srid := base_srid + 60;
         END IF;

         --- TODO: consider special cases around norway etc.
         RETURN out_srid;
END;
$$ LANGUAGE plpgsql;

---snip---

cheers

alex



More information about the postgis-users mailing list