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;
}
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);
}
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 ]*$/;
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;
}
# 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";
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.