From 16e500b65005df5a9f4c8f8d676210a9217bcd03 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Fri, 10 Feb 2012 16:53:15 +0000 Subject: [PATCH] Clamp SRID valuesu > SRID_MAXIMUM to fall in the reserved range (#1505) 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 | 19 +++++++++----- utils/postgis_restore.pl | 55 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/liblwgeom/lwutil.c b/liblwgeom/lwutil.c index a9bcd8a5f..f09de91fc 100644 --- a/liblwgeom/lwutil.c +++ b/liblwgeom/lwutil.c @@ -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; } diff --git a/utils/postgis_restore.pl b/utils/postgis_restore.pl index 0b92e49ef..3c09da6ad 100755 --- a/utils/postgis_restore.pl +++ b/utils/postgis_restore.pl @@ -51,6 +51,11 @@ Usage: $me [-v] 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 = ) { } 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 = ) { next; } + # Clamp SRIDS in spatial_ref_sys + elsif ( $l =~ /COPY spatial_ref_sys /) + { + print STDOUT $l; + while( my $subline = ) + { + 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. -- 2.50.1