[mapserver-users] txt to shp file conversion
woodbri at swoodbridge.com
woodbri at swoodbridge.com
Tue Sep 24 14:11:11 PDT 2002
I also can offer the attachment which uses Shape.pm which can be
fetched from
http://www.water.hut.fi/pub/Grid/Shape-0.03.tar.gz
Or if you just need a single file or two, I can probably just email
it to you.
-Steve
On 24 Sep 2002 at 13:57, Joe Bussell wrote:
> Greetings,
> You are in luck! That is if you can run PERL with XBase. I offer
> you the following snippet of code:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> use XML::Parser;
> use mapscript;
> use XBase;
> use Carp;
>
> my $shapefileDir = ( "/your/destination/directory/" );
>
> my $fileName = $shapefileDir . "userData.shp";
> my $dbfFileName = $shapefileDir . "userData.dbf";
> my $lineNumber = 0;
>
> my $shapeFile = new shapefileObj( $fileName,
> $mapscript::MS_SHAPEFILE_POLYGON ) or croak "Cannot open $fileName
> $!\n"; my $dbfTable = XBase->create( "name" => $dbfFileName,
> "field_names" => [ "DATA" ],
> "field_types" => ["N"],
> "field_lengths" => [10],
> "field_decimals" => [0] ) or croak
> "Cannot create dbfTable $!\n";
>
>
> parserStreaming();
>
> undef $shapeFile;
> undef $dbfTable;
> # this creates a line in the shapefile and writes a DBF line
> sub createLine
> {
> my $llat = shift;
> my $llon = shift;
> my $rlat = shift;
> my $rlon = shift;
> my $data = shift;
> my $number = shift;
>
> my $shape = new shapeObj($mapscript::MS_SHAPE_POINT);
> my $line = new lineObj();
> my $startPoint = new pointObj();
> my $endPoint = new pointObj();
> $lineNumber++;
>
> $startPoint->{x} = $llon;
> $startPoint->{y} = $llat;
> $line->add( $startPoint );
> $endPoint->{x} = $rlon;
> $endPoint->{y} = $rlat;
> $line->add( $endPoint );
> $shape->add( $line );
> $shapeFile->add( $shape );
>
> $dbfTable->set_record( $lineNumber, $data );
> #carp "Should have made a line in the shapefile = $userData from
> ($llat, $llon) to ($rlat, $rlon)\n";
> }
>
> # this is the main loop where the file is cracked and the calls are
> # made to create the file lines
> sub parserStreaming
> {
> my $dataFileName = $dataDir."CurrentData.txt";
> if ( open(DATA_FILE, "<$dataFileName") )
> {
> my @contents = <DATA_FILE>;
> my $line;
> foreach $line (@contents)
> {
> chomp( $line );
> my ( $llat, $llon, $rlat, $rlon, $userData );
> createLine( $atts{llat}, $atts{llon}, $atts{rlat},
> $atts{rlon}, $userData );
>
> #createPoint( $lat, $lon, $userData );
> }
> }
> }
>
>
> # this creates a point in the shapefile and writes a DBF line
> # this is offered only as an example as it will not work with the
> # associated file creates up above the idea is here though
> sub createPoint
> {
> my $lat = shift;
> my $lon = shift;
> my $id = shift;
> my $number = shift;
>
> my $shape = new shapeObj($mapscript::MS_SHAPE_LINE);
> my $line = new lineObj();
> my $point = new pointObj();
>
> $lineNumber++;
>
> $point->{x} = $lon;
> $point->{y} = $lat;
> $line->add( $point );
> $shape->add( $line );
> $shapeFile->add( $shape );
>
> $dbfTable->set_record( $lineNumber, $id, -1 );
> #carp "Should have made a point in the shapefile = $id $lat
> $lon\n";
> }
>
>
> On Tue, Sep 24, 2002 at 06:34:32PM +0200, Alain FERRIOL wrote:
> > Hi all,
> >
> > I downloaded some files from nima :
> > ftp://ftp.nima.mil/pub/gns_data
> >
> > But they are in txt format.
> >
> > Anyone knows how can i handle them?
> >
> > Maybe it's possible to convert them in shp files ?
> >
> > Note : I don't have arcview.
> >
> > Thank's a lot
> >
> > Alain Ferriol
> >
> >
>
> --
>
> Cordially,
>
> Joe Bussell
> On Time Systems
> www.TrafficDodger.com
>
>
-------------- next part --------------
#!/usr/bin/perl -w
use strict;
use Shape qw/:all/;
# Global variables to control debuging output and behavior
my $debug = 0;
my $shpdir = "shapes"; #### directory to put shapes into
sub main()
{
if ($#ARGV > -1) {
print "Usage: gnps2shp file_to_convert\n";
print " like: aa.txt or ??.txt\n";
exit(0);
}
mkdir($shpdir, 0777);
foreach my $f (@ARGV) {
do_nima($f);
}
} # end of main()
# -----------------------------------------------------------------
sub do_nima {
my $f = shift;
my ($shape);
open(IN, $f) || die;
$shape = new Shape;
$shape->{Shapetype} = 1; # point
$shape->{FieldNames} = ['NAME','STATE','CC','TYPE'];
$shape->{FieldTypes} = ['String','String','String','String'];
my $i = 0;
my $lno = 0;
while (!eof(IN)) {
$lno++;
my $x = <IN>;
$x =~ s/\n|\r//g;
$x =~ s/\s*$//g;
next if $x =~ m/^RC\tUFI\t/;
next if $x =~ m/^\s*$/;
my @record = ();
my @field = split(/\t/, $x);
push @{$shape->{Shapes}}, {
SHPType=>1,
ShapeId=>$i,
NParts=>0,
NVertices=>1,
Vertices=>[[$field[ 4], $field[ 3]]]
};
push @{$shape->{ShapeRecords}}, [$field[23], '', $field[12], $field[10]];
$i++;
} # end while
$shape->save("$shpdir/$f");
close(IN)
}
# -----------------------------------------------------------------
main;
More information about the MapServer-users
mailing list