]> granicus.if.org Git - postgis/commitdiff
Clamp SRID valuesu > SRID_MAXIMUM to fall in the reserved range (#1505)
authorSandro Santilli <strk@keybit.net>
Fri, 10 Feb 2012 16:53:15 +0000 (16:53 +0000)
committerSandro Santilli <strk@keybit.net>
Fri, 10 Feb 2012 16:53:15 +0000 (16:53 +0000)
The reserved range is SRID_USER_MAXIMUM+1 to SRID_MAXIMUM.
Core takes care of typmod clamping, postgis_restore.pl takes care
of clamping table definition and spatial_ref_sys entries.

git-svn-id: http://svn.osgeo.org/postgis/trunk@9145 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/lwutil.c
utils/postgis_restore.pl

index a9bcd8a5f604f4ce7b595334168398e30fcd270d..f09de91fcf9ae376799f9f44a2df43625f56b108 100644 (file)
@@ -372,15 +372,20 @@ error_if_srid_mismatch(int srid1, int srid2)
 int
 clamp_srid(int srid)
 {
-       if ( srid <= 0 ) {
-               if ( srid != SRID_UNKNOWN ) {
-                       lwnotice("SRID value %d converted to the officially unknown SRID value %d", srid, SRID_UNKNOWN);
-                       srid = SRID_UNKNOWN;
+       int newsrid = srid;
+
+       if ( newsrid <= 0 ) {
+               if ( newsrid != SRID_UNKNOWN ) {
+                       newsrid = SRID_UNKNOWN;
+                       lwnotice("SRID value %d converted to the officially unknown SRID value %d", srid, newsrid);
                }
        } else if ( srid > SRID_MAXIMUM ) {
-               /* should this be a NOTICE instead ? */
-               lwerror("SRID value %d > SRID_MAXIMUM (%d)",srid,SRID_MAXIMUM);
+    newsrid = SRID_USER_MAXIMUM + 1 +
+      /* -1 is to reduce likelyhood of clashes */
+      /* NOTE: must match implementation in postgis_restore.pl */
+      ( srid % ( SRID_MAXIMUM - SRID_USER_MAXIMUM - 1 ) );
+               lwnotice("SRID value %d > SRID_MAXIMUM converted to %d", srid, newsrid);
        }
        
-       return srid;
+       return newsrid;
 }
index 0b92e49efab5e11210e59eee9025f77fa131ddac..3c09da6adfb55fb563aee19106406cd5562c8a68 100755 (executable)
@@ -51,6 +51,11 @@ Usage:       $me [-v] <dumpfile>
 
 my $DEBUG = 0;
 
+# NOTE: the SRID limits here are being discussed:
+# http://postgis.refractions.net/pipermail/postgis-devel/2012-February/018463.html
+my $SRID_MAXIMUM = 999999;
+my $SRID_USER_MAXIMUM = 998999; 
+
 if ( @ARGV && $ARGV[0] eq '-v' ) {
   $DEBUG = 1;
   shift(@ARGV);
@@ -208,7 +213,13 @@ while( my $l = <INPUT> ) {
       }
       if ( $subline =~ /CONSTRAINT enforce_srid_/i ) {
         $subline =~ s/\.srid\(/.st_srid(/;
-        $subline =~ s/\(-1\)/(0)/;
+        if ( $subline =~ /=\s\(?([-0-9][0-9]*)\)/ ) {
+          my $oldsrid = $1;
+          my $newsrid = clamp_srid($oldsrid);
+          $subline =~ s/=\s*(\(?)[-0-9][0-9]*/= $1$newsrid/;
+        } else {
+          print STDERR "WARNING: could not find SRID value in: $subline";
+        }
       }
       push(@sublines, $subline);
       last if $subline =~ /;[\t ]*$/;
@@ -217,6 +228,23 @@ while( my $l = <INPUT> ) {
     next;
   }
 
+  # Clamp SRIDS in spatial_ref_sys
+  elsif ( $l =~ /COPY spatial_ref_sys /)
+  {
+    print STDOUT $l;
+    while( my $subline = <INPUT>)
+    {
+      if ( $subline =~ /([0-9]*)\t/ ) {
+        my $oldsrid = $1;
+          my $newsrid = clamp_srid($oldsrid);
+          $subline =~ s/^[0-9]*\t/${newsrid}\t/;
+      }
+      print STDOUT $subline;
+      last if $subline =~ /^\.$/;
+    }
+    next;
+  }
+
   print STDOUT $l;
 
 }
@@ -251,7 +279,7 @@ print STDOUT "DROP TABLE _pgis_restore_spatial_ref_sys;\n";
 # but you'd still have your data
 print STDOUT "ALTER TABLE spatial_ref_sys ADD constraint " 
            . "spatial_ref_sys_srid_check check "
-           . "( srid > 0 and srid < 999000 ) ;\n";
+           . "( srid > 0 and srid < " . ($SRID_USER_MAXIMUM+1) ." ) ;\n";
 
 
 print STDERR "Done.\n";
@@ -333,6 +361,29 @@ canonicalize_typename
        return $arg;
 }
 
+# Change SRID to be within allowed ranges
+sub
+clamp_srid
+{
+  my $oldsrid = shift;
+  my $newsrid = $oldsrid;
+
+  if ( $oldsrid < 0 ) {
+    $newsrid = 0;
+  } elsif ( $oldsrid > $SRID_MAXIMUM ) {
+    $newsrid = $SRID_USER_MAXIMUM + 1 +
+      # -1 is to reduce likelyhood of clashes 
+      # NOTE: must match core implementation (lwutil.c)
+      ( $oldsrid % ( $SRID_MAXIMUM - $SRID_USER_MAXIMUM - 1 ) );
+  }
+
+  if ( $oldsrid != $newsrid ) {
+    printf STDERR "  WARNING: SRID value $oldsrid converted to $newsrid\n";
+  }
+
+  return $newsrid;
+}
+
 
 ######################################################################
 # Here are all the signatures we want to skip.