deadpickle has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to draw a road shapefile over a geotiff using Goo::Canvas and Geo::Shapefile. It seems when I run this part of the script it crashes perl. Specifically, it seems around the drawing of the Polyline. Maybe the monks can help me figure out why? my $road_group = Goo::Canvas::Group->new($root);
my $shapefile = new Geo::ShapeFile("UT_Southeast");
for(1 .. $shapefile->shapes()) {
my @shape = ();
my $shape = $shapefile->get_shp_record($_);
#for(1 .. $shape->num_parts) {
foreach my $p(1 .. $shape->num_parts){
if ($shape->y_max <= $top_lat && $shape->y_min >= $bot_lat &&
+abs($shape->x_max) <= abs($top_long) && abs($shape->x_min) <= abs($bo
+t_long)){
my @part = $shape->get_part($p);
print $shape->dump;
#draw the lines
for(@part) {
#print $_->X,' ',$_->Y,"\n";
push(@shape,[$_->X,',',$_->Y]);
}
foreach (@shape){
print @$_,"\n";
}
my $num_points = @shape;
print "$num_points\n";
my $road = Goo::Canvas::Polyline->new($road_group,FALSE,$n
+um_points,@shape);
}
}
}
Re: Drawing Shapefiles in Goo::Canvas
by starbolin (Hermit) on Jun 14, 2008 at 04:36 UTC
|
my @part = $shape->get_part($p);
for(@part) {
push(@shape,[$_->X,',',$_->Y]);
get_part() returns a scalar. You are already iterating over the parts of the shape. Call one of area_centroid or vertex_centroid to get a Geo::ShapeFile::Point object that you can retrieve the X and Y from.
s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s
|-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,,
$|=1,select$,,$,,$,,1e-1;print;redo}
| [reply] [d/l] |
Re: Drawing Shapefiles in Goo::Canvas
by zentara (Archbishop) on Jun 14, 2008 at 17:02 UTC
|
I don't really follow your code, but since you say you are having problems with the PolyLine, you may want to look at Goo::Canvas Waypoint marker w zoom/save to svg and pdf. First, to update a PolyLine, you must manually set the points option, it is not defined by default. Also, points takes an array_ref , NOT an array. It should go something like this:
my $num_points = \@shape; # an array_ref
print "$num_points\n";
my $road = Goo::Canvas::Polyline->new(
$road_group,FALSE,$num_points, .... other options);
Also note from my example node, that creating a PolyLine with an array_ref is a one shot deal. If you need to update it, your points need to be a Goo::Canvas::Points object, you update the Points object, then set it as the points in the PolyLine.
| [reply] [d/l] |
|
I have changed to try and draw a line connecting the waypoints. I still have the same problem with crashing perl:
my $num_points = (scalar @points)/2;
#$num_points = $num_points / 2;
print $num_points,"\n";
unless ($num_points == 1){
print " $count\n";
for(1..($num_points-$count)*2){
print "here is a point\n";
shift @points;
}
foreach (@points){
print $_,"\n";
}
my $way_lines = Goo::Canvas::Polyline->new($way_group,TRUE
+,$num_points,[\@points]);
}
| [reply] [d/l] |
|
As far as I can tell, you are still doing it wrong. Unless I'm missing some deep magic you are trying to use, the following is not right
my $way_lines = Goo::Canvas::Polyline->new($way_group,
TRUE, $num_points, [\@points]);
#here you have both the $points (assuming its an array_ref of points)
# AND you have a nested array_ref in an array_ref ???
# [ \@points] ???? where did you come up with that?
You can only specify 1 array_ref, like $points = \@points and just use $points. Look closely at the example in Goo::Canvas Waypoint marker w zoom/save to svg and pdf and the man page for PolyLine. There are 2 ways to specify points when creating the PolyLine. One is to drop in an anonymous array_ref of x,y pairs. The other is to set the anonymous array_ref to undef, when creating the PolyLine, then after creation, set the points option to an array_ref. Additionally, if you later want to change(update) the points, the points must be a Goo::Canvas::Points object. The Goo::Canvas::Points object is just an object wrapper around the array_ref (look at my waypoint example closely), but the Goo::Canvas wants it that way. Remember, the Goo::Canvas is still in development, and the author hasn't tightened up those dangling inconsistencies yet. But you can get it to work, with the steps outlined above.
| [reply] [d/l] |
|
|
|
|