File: object2shp.pl

package info (click to toggle)
xastir 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 13,676 kB
  • sloc: ansic: 121,100; perl: 7,529; sh: 5,535; makefile: 383; python: 312; sql: 102
file content (145 lines) | stat: -rwxr-xr-x 4,018 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl
#
#  Copyright (C) 2006-2012 Tom Russo

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#  see file COPYING for details

#--------------------------------------------------------------------------

# This script produces an ESRI point shapefile from the object.log file, 
# according to the "Rolling your own shapefile maps" section of README.MAPS
# The point file will display using the TIGER Landmark Point dbfawk file
#
# This enables fast generation of point maps by using xastir to plop down
# objects, then this script to turn the object.log file into a shapefile
#
# Typical usage:  
#   object2shp.pl object.log myshape
#
# Remember to exit xastir and delete "object.log", otherwise xastir will
# never forget your objects.
#--------------------------------------------------------------------------


if ($#ARGV != 1)
{
    print "Usage: $0 <object_file> <shapefile base name>\n";
    exit 1;
}


open(INOBJ,"<$ARGV[0]") || die "Cannot open input object file $ARGV[0]\n";

$cmd[0]="shpcreate $ARGV[1] point";
$cmd[1]="dbfcreate $ARGV[1] -n ID 8 0 -s CFCC 4 -s NAME 30";
$outfile=$ARGV[1];




foreach $command (@cmd)
{
    system($command);
    if ($? == -1) 
    {
        print "failed to execute: $!\n";
    }
    elsif ($? & 127) 
    {
        printf "child died with signal %d, %s coredump\n",
        ($? & 127),  ($? & 128) ? 'with' : 'without';
    }
    elsif ($?&0xF0)
    {
        printf "child exited with value %d\n", $? >> 8;
    }
}

# We now have the shapefile and dbf file created, start populating from the
# objects file:

$i=0;
while (<INOBJ>)
{
    chomp($_);
    $semicolon=substr($_,0,1);
    $name=substr($_,1,9);
    $live_or_dead=substr($_,10,1);
    $timestamp=substr($_,11,7);
    $lat=substr($_,18,8);
    $symtab=substr($_,26,1);
    $long=substr($_,27,9);
    $sym=substr($_,36,1);

#sanity check --- don't try to convert if the line doesn't conform to what
# it should, or if it represents a killed object.  Sometimes objects get
# commented out with #, etc.
    next if ($semicolon ne ";");
    next if ($live_or_dead eq "_");

    $i++;  # bump the ID number so every point has a unique one

    $lat_deg=substr($lat,0,2);
    $lat_min=substr($lat,2,5);
    $lat_hem=substr($lat,7,1);

    $long_deg=substr($long,0,3);
    $long_min=substr($long,3,5);
    $long_hem=substr($long,8,1);

    $lat=$lat_deg+$lat_min/60;
    $lat *= -1 if ($lat_hem eq "S");

    $long=$long_deg+$long_min/60;
    $long *= -1 if ($long_hem eq "W");

    # Construct symbol
    
    if ($symtab ne "/" && $symtab ne "\\")
    {
        print "overlay symbol, symtab is $symtab\n";
        $overlay=$symtab;
        $symtab="\\";
        print " reset values symtab is $symtab, overlay is $overlay\n";
    } 
    else
    {
        $overlay=" ";
    }

    $cmd[0]="shpadd $outfile $long $lat";
    $cmd[1]="dbfadd $outfile $i \'X$symtab$sym$overlay\' $name";
    print $cmd[1]."\n";
    foreach $command (@cmd)
    {
        system($command);
        if ($? == -1) 
        {
            print "failed to execute: $!\n";
        }
        elsif ($? & 127) 
        {
            printf "child died with signal %d, %s coredump\n",
            ($? & 127),  ($? & 128) ? 'with' : 'without';
        }
        elsif ($?&0xF0)
        {
            printf "child exited with value %d\n", $? >> 8;
        }
    }
    
}