| 1 |
// |
| 2 |
// AutoCoast: Automatic Coastline scenery generator for Flight Simulator |
| 3 |
// |
| 4 |
// Copyright (c) 2004, Takuya Murakami. All rights reserved. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions |
| 8 |
// are met: |
| 9 |
// |
| 10 |
// 1. Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
// notice, this list of conditions and the following disclaimer in the |
| 15 |
// documentation and/or other materials provided with the distribution. |
| 16 |
// |
| 17 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
// ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
| 21 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 |
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 |
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
// gshhs_extractor.cc : GSHHS extractor |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
#include "common.hpp" |
| 35 |
#include "gshhs.hpp" |
| 36 |
#include "opt.hpp" |
| 37 |
|
| 38 |
class GOpts : public Options |
| 39 |
{ |
| 40 |
public: |
| 41 |
int skip_id; |
| 42 |
bool smoothing; |
| 43 |
|
| 44 |
public: |
| 45 |
GOpts() { |
| 46 |
skip_id = -1; |
| 47 |
smoothing = true; |
| 48 |
} |
| 49 |
|
| 50 |
void checkOptions(void) { |
| 51 |
if (check("i", "skip-id")) { |
| 52 |
skip_id = atoi(shift()); |
| 53 |
} |
| 54 |
else if (check("n", "no-smoothing")) { |
| 55 |
smoothing = false; |
| 56 |
} |
| 57 |
else { |
| 58 |
Options::checkOptions(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
void usage(void) { |
| 63 |
Options::usage(); |
| 64 |
|
| 65 |
optusage("i", "skip-id", "Specify last polygon number to be skipped"); |
| 66 |
optusage("n", "no-smoothing", "Do not execute polygon smoothing"); |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
static void banner(void) |
| 71 |
{ |
| 72 |
fprintf(stderr, |
| 73 |
"GSHHS data extractor for autocoast.\n" \ |
| 74 |
" Copyright (C) 2004, Takuya Murakami. All rights reserved.\n\n" |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/* main */ |
| 80 |
int main(int argc, char *argv[]) |
| 81 |
{ |
| 82 |
BoundingBox bbox; |
| 83 |
|
| 84 |
banner(); |
| 85 |
|
| 86 |
GOpts opts; |
| 87 |
opts.set_arg_help("datafile west east south north"); |
| 88 |
opts.parse(&argc, &argv); |
| 89 |
|
| 90 |
if (argc != 6) { |
| 91 |
opts.usage(); |
| 92 |
exit(1); |
| 93 |
} |
| 94 |
|
| 95 |
// get arg |
| 96 |
const char *fname = argv[1]; |
| 97 |
bbox.xmin = atof(argv[2]); |
| 98 |
bbox.xmax = atof(argv[3]); |
| 99 |
bbox.ymin = atof(argv[4]); |
| 100 |
bbox.ymax = atof(argv[5]); |
| 101 |
|
| 102 |
if (bbox.xmin >= bbox.xmax || |
| 103 |
bbox.ymin >= bbox.ymax || |
| 104 |
bbox.xmin < -180 || bbox.xmax > 360 || |
| 105 |
bbox.ymin < -90 || bbox.ymax > 90 |
| 106 |
) { |
| 107 |
fprintf(stderr, "Invalid lat/lon.\n"); |
| 108 |
exit(1); |
| 109 |
} |
| 110 |
if (bbox.xmin < 0) { |
| 111 |
if (bbox.xmax > 0) { |
| 112 |
fprintf(stderr, "You can't cross greenwich.\n"); |
| 113 |
exit(1); |
| 114 |
} |
| 115 |
|
| 116 |
bbox.xmin += 360.0; |
| 117 |
bbox.xmax += 360.0; |
| 118 |
} |
| 119 |
|
| 120 |
// create cropping box |
| 121 |
BoundingBox crop; |
| 122 |
crop.xmin = bbox.xmin - 2.0; |
| 123 |
crop.xmax = bbox.xmax + 2.0; |
| 124 |
crop.ymin = bbox.ymin - 2.0; |
| 125 |
crop.ymax = bbox.ymax + 2.0; |
| 126 |
|
| 127 |
GPolygon polygon; |
| 128 |
|
| 129 |
for (int level = 1; level <= 4; level++) { |
| 130 |
// Load GSHHS data |
| 131 |
fprintf(stderr, "Extracting level = %d\n", level); |
| 132 |
fprintf(stderr, "* Reading data...\n"); |
| 133 |
polygon.LoadFromGshhsBinary(fname, level, bbox, opts.skip_id); |
| 134 |
|
| 135 |
polygon.printStat(); |
| 136 |
|
| 137 |
// Smoothing |
| 138 |
if (opts.smoothing) { |
| 139 |
fprintf(stderr, "* Smoothing...\n"); |
| 140 |
polygon.Smoothing(); |
| 141 |
} |
| 142 |
|
| 143 |
// Cropping points |
| 144 |
fprintf(stderr, "* Cropping points...\n"); |
| 145 |
polygon.Crop(crop); |
| 146 |
|
| 147 |
polygon.printStat(); |
| 148 |
|
| 149 |
// Clipping |
| 150 |
fprintf(stderr, "* Clipping...\n"); |
| 151 |
GpcPolygon *clipped = polygon.Clipping(bbox); |
| 152 |
|
| 153 |
clipped->printStat(); |
| 154 |
|
| 155 |
// Write result |
| 156 |
fprintf(stderr, "* Writing...\n"); |
| 157 |
char outfile[128]; |
| 158 |
sprintf(outfile, "level%d.gpc", level); |
| 159 |
clipped->SaveToFile(outfile); |
| 160 |
|
| 161 |
delete clipped; |
| 162 |
polygon.clear(); |
| 163 |
} |
| 164 |
} |