fastest way to convert lon-lat to points
Roberto Bianconi
roberto.bianconi at GMAIL.COM
Wed Jul 26 10:39:15 PDT 2006
On 7/20/06, P Kishor <punkish at eidesis.org> wrote:
> On 7/20/06, Steve Lime <Steve.Lime at dnr.state.mn.us> wrote:
> > Puneet: Why is speed such a big deal or will you be doing this often?
>
> I will be doing this a few times a year (so, often, although not
> necessarily often enough), but this portion is only a part of a longer
> process. So, I have to optimize each step as much as I can for overall
> better rate of return.
>
> >
> > Anyway, if it were me I'd just write a quick MapScript script using
> > perl. Create a new shapefileObj, a new XBase table, open the file and
> > start looping. The loop itself should have like 3 lines of code. The
> > whole script should be about 15...
> >
Hi,
suppose your csv file is made of these records:
12.500,41.890,790380
12.530,41.890,790480
...
In Perl you might go like this. Actually a bit more than 15 lines, but
you can reduce it if you like :))
use mapscript;
use XBase;
my $csv = 'dummy.csv';
my $outfile = 'dummy_out'; # any name with no extension
my $table = XBase->create(name => "$outfile.dbf",
field_names =>['X','Y','VALUE'],
field_types => ['N','N','N'],
field_lengths => [8,8,10],
field_decimals => [3,3,0]) or die XBase->errstr;
my $shapefile = new mapscript::shapefileObj($outfile,1) or die;
open (CSV,"<$csv") or die "Cannot open $csv: $!\n";
my @lines = <CSV>;
close (CSV);
my $i = -1;
foreach (@lines) {
chomp;
my @rec = split /,/;
my ($x,$y,$value) = @rec;
$i++;
$table->set_record($i, at rec) or die XBase->errstr;
my $point = new mapscript::pointObj();
$point->setXY($x,$y);
my $line = new mapscript::lineObj();
$line->add($point);
my $shape = new mapscript::shapeObj();
$shape->add($line);
$shapefile->add($shape);
}
$shapefile = '';
ciao
Roberto
More information about the MapServer-users
mailing list