#!/usr/bin/perl use strict; use warnings; use Math::Round; use Geo::ShapeFile; use Geo::ShapeFile::Shape; use Geo::ShapeFile::Point; # Declare variables my $blockid; my $bcount; my $totalblocks; my $progress; my $shapefile; # used for loading the TIGER shapefile my %attr; # used to read the dbf file my $polygon; # used to read the shp file my @points; # used to read the shape into an array of points my $coor; # used to read the coordinates of each point my $x; my $y; my $pcount; # counter for the coordinates being written into output my $totalpoints; # total number of coordinates in each polygon # Variables for time keeping my $start = time; my $duration; # Prepare the TIGER spreadsheet. open (TIGER, ">tiger.txt") or die "Failed to open"; print TIGER "BLOCKID\tPolygon\n"; # make a header # Load the TIGER shapefile $shapefile = 0; $shapefile = Geo::ShapeFile->new ("tabblock2010_42_pophu"); # use the TIGER census block special release (population & housing units only) $totalblocks = $shapefile->shapes(); for (1 .. $totalblocks) { %attr = $shapefile->get_dbf_record($_); $blockid = $attr{BLOCKID10}; $bcount++; print TIGER "$blockid\t\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[["; # The polygon column is in the GEOJSON format $polygon = $shapefile->get_shp_record($_); @points = $polygon->points(); $pcount = 0; $totalpoints = scalar (@points); foreach $coor (@points) { $x = $coor->get_x(); $y = $coor->get_y(); print TIGER "[$x,$y]"; $pcount++; if ($pcount != $totalpoints) { # All but the last coordinate should be followed by a comma print TIGER ","; } } print TIGER "]]}\n"; # finish the entry print "Done $bcount out of $totalblocks.\n"; %attr = (); $blockid = 0; $polygon = 0; @points = (); $totalpoints = 0; } close (TIGER); $shapefile = 0; select()->autoflush(1); # Show stopwatch $duration = time - $start; $duration = round ($duration / 60); print "\n\nThis script took $duration minutes\n\n"; exit;