http://forum.celestialmatters.org/

a Perl script for Locations
http://forum.celestialmatters.org/viewtopic.php?f=3&t=343
Page 1 of 1

Author:  fenerit [ Sat, 31-10-09, 1:14 GMT ]
Post subject:  a Perl script for Locations

In building my minerals add-on I've been helped by this script, which allow to assign more coordinates to the same name, useful whether one wish to place ricorsive objects (mines in my case).
For example:
gold mines coordinates (shorten):
Code:
-105.65500   39.81400
-106.00190   38.63013
-106.08830   39.38123
-106.50520   38.77402
-106.48660   38.77402
-106.68920   39.61874
-107.02780   38.66134


script:
Code:
#    Author Fenerit <fenerit@interfree.it>
#    Version 1.0, 31.10.2009
#    This script build a structured Celestia's location .SSC file from a LongLat table.
#   Such coordinates will be assigned to an user-defined location,
#   (see below how to do whether you have LatLong instead).
#   Acknowledgements: Dr. Fridger Schrempp <www.celestialmatters.org> for its basic routines.

#   USAGE:
#   1) Script and coordinates' text files must be in the same folder;
#   2) Trim the space at init and at end of the LongLat table if any;
#   3) Replace float commas with dots (for example: -109,24330 > -109.24330) if any, and save;
#   4) Run the script. A new file called "longlat2locs.ssc" will be created in the same folder;

   open(SSC, " > longlat2locs.ssc") || die "Can not create longlat2locs.ssc\n";

#    set the data of locations (edit below for your own);

   my $locsname    = "Gold mine";       # set the name;
   my $locsplace    = "Sol/Earth";       # set the place;
   my $labelcolor    = "[ 1 1 0 ]";      # set the color;
   my $type       = "RU";            # set the type (USGS gazetteer IAU nomenclature);
   my $size       = 20;             # set the size;
   my $elevation    = 0;              # set the LongLat elevation;
#   my $importance    = 0;             # uncomment and edit whether you want set also the Importance;

   $l = 0;
   
   while (<>)
{
   ($x,$y) = split (" ",$_);

      $x[$l] = $x;
      $y[$l] = $y;
      
   $l++;
}

   $longlatcount = $l;

   for ($index=0; $index < $longlatcount; $index++)
   
{
   printf SSC "Location \"$locsname\" \"$locsplace\"\n";
   printf SSC "{\n";
   printf SSC "LabelColor $labelcolor\n";
   printf SSC "LongLat [ %8.5f %8.5f $elevation ]\n", $x[$index],$y[$index]; # invert $x $y whether you have LatLong;
   printf SSC "Size $size\n";
#   printf SSC "Importance $importance\n"; # uncomment whether you want print also the Importance;
   printf SSC "Type \"$type\"\n";
   printf SSC "}\n\n";

;}


output:
Code:
Location "Gold mine" "Sol/Earth"
{
LabelColor [ 1 1 0 ]
LongLat [ -105.65500 39.81400 0 ]
Size 20
Type "RU"
}

Location "Gold mine" "Sol/Earth"
{
LabelColor [ 1 1 0 ]
LongLat [ -106.00190 38.63013 0 ]
Size 20
Type "RU"
}

Location "Gold mine" "Sol/Earth"
{
LabelColor [ 1 1 0 ]
LongLat [ -106.08830 39.38123 0 ]
Size 20
Type "RU"
}

Location "Gold mine" "Sol/Earth"
{
LabelColor [ 1 1 0 ]
LongLat [ -106.50520 38.77402 0 ]
Size 20
Type "RU"
}

Location "Gold mine" "Sol/Earth"
{
LabelColor [ 1 1 0 ]
LongLat [ -106.48660 38.77402 0 ]
Size 20
Type "RU"
}

Location "Gold mine" "Sol/Earth"
{
LabelColor [ 1 1 0 ]
LongLat [ -106.68920 39.61874 0 ]
Size 20
Type "RU"
}

Location "Gold mine" "Sol/Earth"
{
LabelColor [ 1 1 0 ]
LongLat [ -107.02780 38.66134 0 ]
Size 20
Type "RU"
}



Note: real minerals add-on use "Au" instead of "Gold mine"; here just for marking. There is a bit of uncompleteness, of course; the usual trimmin and the commas>dots substitution in case these latters did have.

Author:  fenerit [ Sun, 15-11-09, 0:44 GMT ]
Post subject: 

Script updated! V. 1.1

- Commas to dots conversion;
- Spreadsheets columns' header removing: first line;
- Trim spaces init, amongst, end
- Number of resulting locations processed.

Code:

#    Author Fenerit <fenerit@interfree.it>
#    Version 1.1, 14.11.2009
#    This script build a structured Celestia's location .SSC file from a LongLat table.
#   Such coordinates will be assigned to an user-defined location,
#   (see below how to do whether you have LatLong instead).
#   Acknowledgements: Dr. Fridger Schrempp <www.celestialmatters.org> for its basic routines.

#   USAGE:
#   1) Script and coordinates' text files must be in the same folder;
#   2) Run the script. A new file called "longlat2locs.ssc" will be created in the same folder;

   print STDOUT "\n -- Start process... --\n";
   
   open(SSC, " > longlat2locs.ssc") || die "Can not create longlat2locs.ssc\n";

#    set the data of locations (edit below for your own);

   my $locsname    = "Gold mine";            # set the name;
   my $locsplace    = "Sol/Earth";       # set the place;
   my $labelcolor    = "[ 1 1 0 ]";      # set the color;
   my $type       = "RU";            # set the type (USGS gazetteer IAU nomenclature);
   my $size       = 20;             # set the size;
   my $elevation    = 0;              # set the LongLat elevation;
#   my $importance    = 0;             # uncomment and edit whether you want set also the Importance;

   $l = 0;
   
   while (<>)

{
   
   next if ($. == 1);               # skip the first line;      
   next if (/^$/);                  # trim input white spaces at init and at end of file;
   
   s/\b(,)\b/./g;                  # convert commas to dots;
   
   ($long,$lat) = split (" ",$_);

      $long[$l] = $long;
      $lat[$l] = $lat;

   $l++;
}

   $longlatcount = $l;

   for ($index=0; $index < $longlatcount; $index++)
   
{
   printf SSC "Location \"$locsname\" \"$locsplace\"\n";
   printf SSC "{\n";
   printf SSC "LabelColor $labelcolor\n";
   printf SSC "LongLat [ %8.5f %8.5f $elevation ]\n", $long[$index],$lat[$index]; # invert $long $lat whether you have LatLong;
   printf SSC "Size $size\n";
#   printf SSC "Importance $importance\n"; # uncomment whether you want print also the Importance;
   printf SSC "Type \"$type\"\n";
   printf SSC "}\n\n";
;}
   
   print STDOUT "\n -- Finished! ", $longlatcount, " resulting locations for", " ", $locsname, " --\n";



This manage the following situations:

1)

Code:

LONGITUDE   LATITUDE
141,55000   42,65000
139,86670   37,63330
136,15000   36,23330
136,88330   35,13330
138,25000   37,16670
.
.



2)

Code:

LONGITUDE   LATITUDE

141,55000   42,65000
139,86670   37,63330


136,15000   36,23330
136,88330   35,13330
138,25000   37,16670
.
.



3)

Code:

.
.
141,55000   42,65000

139,86670   37,63330

136,15000   36,23330
136,88330   35,13330
138,25000   37,16670
.
.


Author:  t00fri [ Sun, 15-11-09, 1:22 GMT ]
Post subject: 

Perl is fun, isn't it? ;-) I am glad to see some brave people using it, too!

Fridger

Author:  fenerit [ Sun, 15-11-09, 12:36 GMT ]
Post subject: 

t00fri wrote:
Perl is fun, isn't it? ;-) I am glad to see some brave people using it, too!

Fridger


Thanks! But note the differences between a true programmer and a disgrace like me: 15 days to find the commas/dots conversion. :twisted: A thing that the former probably "find" in half second!

Author:  t00fri [ Sun, 15-11-09, 13:28 GMT ]
Post subject: 

fenerit wrote:
t00fri wrote:
Perl is fun, isn't it? ;-) I am glad to see some brave people using it, too!

Fridger


Thanks! But note the differences between a true programmer and a disgrace like me: 15 days to find the commas/dots conversion. :twisted: A thing that the former probably "find" in half second!


;-)

Perl has some MOST powerful "secrets": one is the use of regular expressions aka patterns along with operations that act upon such patterns. There are really good respective tutorials in the net with many examples. For a C++ programmer, Perl's object oriented structures come most familiar, of course. Also in general, there are many syntactic similarities to C++.

Moreover, for writing Perl code, it is most crucial to realize that Perl acts on a line-by-line basis and your code acts like a sophisticated filter for the input file! Generically, one reads some ascii-text in, looks for certain patterns line-by-line, and then applies some operation on the found lines, satisfying the prescribed pattern criteria.
More complex stuff along these lines can be done using ( ) parenthesis pairs in the desired pattern that can be further worked upon with $1, $2 ... carrying the contents of the matched subpatterns in the successive parentheses!

Fridger

Author:  fenerit [ Sun, 15-11-09, 15:38 GMT ]
Post subject: 

Your suggestions will be hold with the due consideration. As newbie, the thing that has shocked me up mostly is the stability and flawless of the "brute force" - like with which it render the output. You know that DOS-Windows systems are unstables for itselves, so you can imagine my skepticism about a new language/interpreter managed within a graphical interface; crashes were my first preoccupations. Insted NOTHING of all this. Whether certain parts of the code doesn't work, Perl stop itself very clean and without problems (I can assure to you that my code errors are terrific). I really like this language.

Author:  fenerit [ Mon, 16-11-09, 20:07 GMT ]
Post subject: 

This post is more for care than for other. Whereas the former set of coordinates are real US gold mine, the latters are japanese Aluminium processing plants, not gold mine. They was taken as an example. :wink:

Author:  fenerit [ Wed, 18-11-09, 14:45 GMT ]
Post subject: 

These changes on the relevant script's lines allows to add the proper name's location:

Code:

#   my $locsname = "Gold mine";  # comment the custom name;

---------------------------------------------------------------------------------

($name,$long,$lat) = split (" ",$_);
      
      $name[$l] = $name;  # add the name of location;
      $long[$l] = $long;
      $lat[$l] = $lat;

   $l++;

---------------------------------------------------------------------------------

printf SSC "Location \"$name[$index]\" \"$locsplace\"\n"; # print each location;
printf SSC "LongLat [ %8.8f %8.8f $elevation ]\n", $long[$index],$lat[$index]; # increase the float precision;

---------------------------------------------------------------------------------

print STDOUT "\n -- Finished! ", $longlatcount, " resulting locations --\n"; # eye candy



INPUT:
Code:
FEATUR_NM   longitude   latitude
Aalborg   9,933333333   57,05000056
Aarhus   10,21666556   56,15000028
Abadan   48,2833325   30,33333389
Abashiri   144,2833325   44,01666861
.
.
.


OUTPUT:
Code:
Location "Aalborg" "Sol/Earth"
{
LabelColor [ 1 0 0 ]
LongLat [ 9.93333333 57.05000056 0 ]
Size 20
Type "RU"
}

Location "Aarhus" "Sol/Earth"
{
LabelColor [ 1 0 0 ]
LongLat [ 10.21666556 56.15000028 0 ]
Size 20
Type "RU"
}

Location "Abadan" "Sol/Earth"
{
LabelColor [ 1 0 0 ]
LongLat [ 48.28333250 30.33333389 0 ]
Size 20
Type "RU"
}

Location "Abashiri" "Sol/Earth"
{
LabelColor [ 1 0 0 ]
LongLat [ 144.28333250 44.01666861 0 ]
Size 20
Type "RU"
}


Author:  fenerit [ Fri, 27-11-09, 21:18 GMT ]
Post subject: 

Add this line before the commas to dots conversion whether you want to skip duplicate values:

Code:
next if ++$seen{$_} > 1;                  # skip duplicate values;

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/