简体   繁体   中英

Postgresql Geometry Type parseing In Perl?

PostgreSQL supports Geometric types and I have a database full of them. I'd like to use Perl with DBIx:Class to retrieve them.

For example the point type is defined as '(x,y)' and thats how DBIx::Class retrieves it. As a string.

I'd like to parse this into an array or some other more tangible datatype.

My current parsing attempts have involved stripping the brackets and then splitting which works fine for point but NOT for polygon:

(( x1 , y1 ) , ... , ( xn , yn ))
( x1 , y1 ) , ... , ( xn , yn )
( x1 , y1   , ... ,   xn , yn )
  x1 , y1   , ... ,   xn , yn

My next thought was replacing ('s for ['s and then JSON parsing them which also works but I don't like it.

Does anyone have any suggestions for a cleaner parsing method?

Thanks

More details on PostgreSQL Geometry

### First Option - just replace ( with ] and eval it as arrays of array
sub parsePolyEval {
        my $polyString = shift;
    $polyString =~ s/\(/\[/g;
    $polyString =~ s/\)/\]/g;

    return eval($polyString);
}

### Second option - parse inner arrays. Assuming That a polygon consists of lists of     points (level 2 array), and not defined recursivley
sub parsePolySPlit {
    my $polyString = shift;
    my @polyArray;
    my @subArrays = ($polyString =~ /\(([^\)\()]+)\)/g);

    foreach (@subArrays) {
        my @subArray = split(/[\s\,]+/,$_);
        @subArray = grep  {$_ ne ""} @subArray; ##ignore empty elements caused by spaces
        push(@polyArray,\@subArray);
    }

    return \@polyArray;
}

sub printPoly {
    my $poly = shift;
    my $i=0;
    foreach (@$poly) {
         print "Point number $i is: ".join("|",@$_)."\n";
        $i++;
        }
}

my $polyString = "(( x1 , y1 ) ,( x2 , y3 ) , ( xn , yn ))";

my $poly1 = parsePolyEval($polyString);
printPoly($poly1);
my $poly2 = parsePolySPlit($polyString);
printPoly($poly2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM