| 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.cc : GSHHS process |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
//#include <netinet/in.h> |
| 34 |
#include "common.hpp" |
| 35 |
#include "gshhs.hpp" |
| 36 |
|
| 37 |
static double u2d(int u) |
| 38 |
{ |
| 39 |
return (double)u * 1.0e-6; |
| 40 |
} |
| 41 |
|
| 42 |
//////////////////////////////////////////////////////////////////// |
| 43 |
// GVertexList |
| 44 |
// |
| 45 |
GVertexList::GVertexList(void) |
| 46 |
: GpcVertexList(0, NULL) |
| 47 |
{ |
| 48 |
/* do nothing */ |
| 49 |
} |
| 50 |
|
| 51 |
GVertexList::~GVertexList(void) |
| 52 |
{ |
| 53 |
} |
| 54 |
|
| 55 |
bool GVertexList::readHeader(FILE *fp) |
| 56 |
{ |
| 57 |
if (fread(&header, sizeof(header), 1, fp) == 0) { |
| 58 |
return false; // EOF |
| 59 |
} |
| 60 |
|
| 61 |
header.id = ntohl(header.id); |
| 62 |
header.num = ntohl(header.num); |
| 63 |
header.level = htonl(header.level); |
| 64 |
header.xmin = htonl(header.xmin); |
| 65 |
header.xmax = htonl(header.xmax); |
| 66 |
header.ymin = htonl(header.ymin); |
| 67 |
header.ymax = htonl(header.ymax); |
| 68 |
header.area = htonl(header.area); |
| 69 |
header.greenwich= htons(header.greenwich); |
| 70 |
header.source = htons(header.source); |
| 71 |
|
| 72 |
bbox.xmin = u2d(header.xmin); |
| 73 |
bbox.xmax = u2d(header.xmax); |
| 74 |
bbox.ymin = u2d(header.ymin); |
| 75 |
bbox.ymax = u2d(header.ymax); |
| 76 |
|
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
void GVertexList::printHeader(void) |
| 81 |
{ |
| 82 |
printf("P I:%6d N:%8d L:%2d %s W:%-8.3f E:%-8.3f S:%-8.3f N:%-8.3f\n", |
| 83 |
header.id, header.num, header.level, |
| 84 |
header.source == 1 ? "WVS" : "CIA", |
| 85 |
u2d(header.xmin), u2d(header.xmax), u2d(header.ymin), u2d(header.ymax)); |
| 86 |
} |
| 87 |
|
| 88 |
bool GVertexList::readData(FILE *fp, bool skip, bool isWestHemisphere) |
| 89 |
{ |
| 90 |
vlist->num_vertices = header.num; |
| 91 |
if (skip) { |
| 92 |
fseek(fp, sizeof(int) * 2 * vlist->num_vertices, SEEK_CUR); |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
if (vlist->vertex) { |
| 97 |
delete [] vlist->vertex; |
| 98 |
} |
| 99 |
vlist->vertex = new gpc_vertex[vlist->num_vertices]; |
| 100 |
|
| 101 |
int i, p[2]; |
| 102 |
for (i = 0; i < vlist->num_vertices; i++) { |
| 103 |
if (fread(p, sizeof(int), 2, fp) <= 0) { |
| 104 |
// Ouch! |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
p[0] = ntohl(p[0]); |
| 109 |
p[1] = ntohl(p[1]); |
| 110 |
|
| 111 |
double x = u2d(p[0]); |
| 112 |
double y = u2d(p[1]); |
| 113 |
|
| 114 |
// When polygon is crossing greenwich? |
| 115 |
if (header.greenwich) { |
| 116 |
// assure polygon continuity |
| 117 |
if (x > 200.0) { |
| 118 |
x -= 360.0; |
| 119 |
} |
| 120 |
|
| 121 |
// for west hemisphere, move polygon crossing greenwich. |
| 122 |
if (isWestHemisphere) { |
| 123 |
x += 360.0; |
| 124 |
} |
| 125 |
} |
| 126 |
vlist->vertex[i].x = x; |
| 127 |
vlist->vertex[i].y = y; |
| 128 |
} |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
bool GVertexList::inBound(BoundingBox &clip) |
| 133 |
{ |
| 134 |
if (!bbox.isCrossing(clip)) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
bool GVertexList::inBoundVertices(BoundingBox &clip) |
| 141 |
{ |
| 142 |
// check all vertices |
| 143 |
int n = numVertices(); |
| 144 |
for (int i = 0; i < n; i++) { |
| 145 |
if (clip.isInbox(vx(i), vy(i))) { |
| 146 |
return true; |
| 147 |
} |
| 148 |
} |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
//////////////////////////////////////////////////////////////////// |
| 153 |
// GPolygon |
| 154 |
// |
| 155 |
GPolygon::GPolygon() |
| 156 |
{ |
| 157 |
/* do nothing */ |
| 158 |
} |
| 159 |
|
| 160 |
GPolygon::~GPolygon() |
| 161 |
{ |
| 162 |
} |
| 163 |
|
| 164 |
// |
| 165 |
// Load GSHHS binary file |
| 166 |
// |
| 167 |
int GPolygon::LoadFromGshhsBinary(const char *fname, int level, BoundingBox &bbox, int skipid) |
| 168 |
{ |
| 169 |
FILE *fp = fopen(fname, "rb"); |
| 170 |
if (!fp) { |
| 171 |
fprintf(stderr, "Can't open file: %s\n", fname); |
| 172 |
exit(1); |
| 173 |
} |
| 174 |
|
| 175 |
bool isWestHemisphere = false; |
| 176 |
if (bbox.xmin > 200.0) { |
| 177 |
isWestHemisphere = true; |
| 178 |
} |
| 179 |
|
| 180 |
GVertexList *vl = new GVertexList; |
| 181 |
|
| 182 |
while (vl->readHeader(fp)) { |
| 183 |
if (vl->level() != level || |
| 184 |
!vl->inBound(bbox) || |
| 185 |
vl->id() <= skipid) { |
| 186 |
// skip this polygon |
| 187 |
vl->readData(fp, true); |
| 188 |
continue; |
| 189 |
} |
| 190 |
|
| 191 |
vl->readData(fp, false, isWestHemisphere); |
| 192 |
|
| 193 |
if (vl->inBoundVertices(bbox)) { |
| 194 |
vl->printHeader(); |
| 195 |
AddVertexList(vl, false); |
| 196 |
} |
| 197 |
|
| 198 |
delete vl; |
| 199 |
vl = new GVertexList; |
| 200 |
} |
| 201 |
delete vl; |
| 202 |
|
| 203 |
BuildVertexListFromGpcPolygon(); |
| 204 |
|
| 205 |
fclose(fp); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
// |
| 210 |
// Save GPC data |
| 211 |
// |
| 212 |
int GPolygon::SaveGpcFile(const char *fname) |
| 213 |
{ |
| 214 |
GpcPolygon::SaveToFile(fname); |
| 215 |
return 0; |
| 216 |
} |