[mapserver-users] txt to shp file conversion

Joe Bussell joe at otsys.com
Tue Sep 24 16:57:13 EDT 2002


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




More information about the mapserver-users mailing list