49 OpenShapeFile(
const char *fn );
53 CustomErrors(
const char *message );
57 #define OpenMap OpenShapeFile
58 #define CloseMap SHPClose
62 #define MAP_FILE ".map"
63 #define OpenMap plLibOpenPdfstrm
64 #define CloseMap pdf_close
65 #define OFFSET ( 180 * 100 )
67 #define W_BUFSIZ ( 32 * 1024 )
70 #define SHPT_POLYGON 3
72 #endif //HAVE_SHAPELIB
79 if ( *lon > midlon + 180.0 )
80 *lon -= floor( ( *lon - midlon - 180.0 ) / 360.0 + 1.0 ) * 360.0;
81 else if ( *lon < midlon - 180.0 )
82 *lon += floor( ( midlon - 180.0 - *lon ) / 360.0 + 1.0 ) * 360.0;
95 for ( i = 0; i < n; ++i )
96 temp[i] = ( *array )[i];
107 int *temp = (
int *) malloc( ( n + 1 ) *
sizeof ( int ) );
110 for ( i = 0; i < n; ++i )
111 temp[i] = ( *array )[i];
125 for ( i = 0; i < n; ++i )
126 temp[i] = ( *array )[i];
152 return ( (
ABS( x[0] - resultx ) < 1.0e-12 ) && (
ABS( y[0] - resulty ) < 1.0e-12 ) );
159 drawmapdata(
void ( *
mapform )(
PLINT,
PLFLT *,
PLFLT * ),
int shapetype,
PLINT n,
PLFLT *x,
PLFLT *y,
PLFLT dx,
PLFLT dy,
PLFLT just,
const char *
text )
165 ( *mapform )( n, x, y );
170 for ( i = 0; i < n; ++i )
171 plptex( x[i], y[i], dx, dy, just, text );
175 else if ( shapetype == SHPT_ARCZ || shapetype == SHPT_ARCM )
177 else if ( shapetype ==
SHPT_POLYGON || shapetype == SHPT_POLYGONZ || shapetype == SHPT_POLYGONM )
179 else if ( shapetype ==
SHPT_POINT || shapetype == SHPT_POINTM || shapetype == SHPT_POINTZ )
180 for ( i = 0; i < n; ++i )
181 plptex( x[i], y[i], dx, dy, just, text );
182 #endif //HAVE_SHAPELIB
218 #if defined ( HAVE_SHAPELIB ) || defined ( PL_DEPRECATED )
220 char *filename = NULL;
221 char truncatedfilename[900];
224 PLFLT minsectlon, maxsectlon, minsectlat, maxsectlat;
225 PLFLT *bufx = NULL, *bufy = NULL;
228 PLFLT **splitx = NULL;
229 PLFLT **splity = NULL;
230 int *splitsectionlengths = NULL;
232 PLFLT lastsplitpointx;
233 PLFLT lastsplitpointy;
234 PLFLT penultimatesplitpointx;
235 PLFLT penultimatesplitpointy;
249 SHPObject *
object = NULL;
252 char *prjfilename = NULL;
254 char prjtype[] = { 0, 0, 0, 0, 0, 0, 0 };
255 int appendresult = 0;
259 unsigned char n_buff[2], buff[800];
269 if ( strstr( name,
".shp" ) )
270 filenamelen = (int) ( name - strstr( name,
".shp" ) );
272 filenamelen = (int) strlen( name );
273 filename = (
char *) malloc( filenamelen + strlen(
MAP_FILE ) + 1 );
276 plabort(
"Could not allocate memory for concatenating map filename" );
279 strncpy( filename, name, filenamelen );
280 filename[ filenamelen ] =
'\0';
284 if ( strlen( filename ) < 899 )
285 strcpy( truncatedfilename, filename );
288 memcpy( truncatedfilename, filename, 896 );
289 truncatedfilename[896] =
'.';
290 truncatedfilename[897] =
'.';
291 truncatedfilename[898] =
'.';
292 truncatedfilename[899] =
'\0';
295 strcpy( warning,
"Could not find " );
296 strcat( warning, filename );
297 strcat( warning,
" file." );
300 if ( ( in = OpenShapeFile( filename ) ) == NULL )
306 SHPGetInfo( in, &nentries, &shapetype, mins, maxs );
309 prjfilename = (
char *) malloc( filenamelen + 5 );
313 plabort(
"Could not allocate memory for generating map projection filename" );
316 strncpy( prjfilename, name, filenamelen );
317 prjfilename[ filenamelen ] =
'\0';
318 strcat( prjfilename,
".prj" );
320 if ( prjfile && prjfile->
file )
322 fread( prjtype, 1, 6, prjfile->
file );
323 if ( strcmp( prjtype,
"PROJCS" ) == 0 )
352 if ( ( !plotentries && ( entrynumber == nentries ) ) || ( plotentries && ( entryindex == nplotentries ) ) )
356 if ( partnumber == 0 )
359 object = SHPReadObject( in, plotentries[entryindex] );
361 object = SHPReadObject( in, entrynumber );
365 if (
object == NULL )
374 if ( object->nParts == 0 )
375 nVertices =
object->nVertices;
376 else if ( partnumber == ( object->nParts - 1 ) )
377 nVertices =
object->nVertices -
object->panPartStart[partnumber];
379 nVertices =
object->panPartStart[partnumber + 1] -
object->panPartStart[partnumber];
382 if ( nVertices > bufsize )
387 bufx = (
PLFLT *) malloc( (
size_t) bufsize *
sizeof (
PLFLT ) );
388 bufy = (
PLFLT *) malloc( (
size_t) bufsize *
sizeof (
PLFLT ) );
389 if ( !bufx || !bufy )
391 plabort(
"Could not allocate memory for map data" );
403 if ( object->nParts > 0 )
405 bufxraw =
object->padfX +
object->panPartStart[partnumber];
406 bufyraw =
object->padfY +
object->panPartStart[partnumber];
410 bufxraw =
object->padfX;
411 bufyraw =
object->padfY;
414 for ( i = 0; i < nVertices; i++ )
416 bufx[i] = (
PLFLT) bufxraw[i];
417 bufy[i] = (
PLFLT) bufyraw[i];
421 minsectlon =
object->dfXMin;
422 maxsectlon =
object->dfXMax;
423 minsectlat =
object->dfYMin;
424 maxsectlat =
object->dfYMax;
428 if ( partnumber == object->nParts - 1 || object->nParts == 0 )
433 SHPDestroyObject(
object );
439 if ( nVertices == 0 )
443 if (
pdf_rdx( n_buff, (
long)
sizeof (
unsigned char ) * 2, in ) == 0 )
445 nVertices = ( n_buff[0] << 8 ) + n_buff[1];
446 if ( nVertices == 0 )
449 pdf_rdx( buff, (
long)
sizeof (
unsigned char ) * 4 * nVertices, in );
450 if ( nVertices == 1 )
453 for ( j = i = 0; i < nVertices; i++, j += 2 )
455 t = ( buff[j] << 8 ) + buff[j + 1];
458 for ( i = 0; i < nVertices; i++, j += 2 )
460 t = ( buff[j] << 8 ) + buff[j + 1];
490 splitx = (
PLFLT **) malloc(
sizeof (
PLFLT* ) );
491 splity = (
PLFLT **) malloc(
sizeof (
PLFLT* ) );
493 splitsectionlengths = (
int *) malloc(
sizeof (
size_t ) );
494 if ( !splitx || !splity || !splitsectionlengths )
496 plabort(
"Could not allocate memory for longitudinally split map data" );
502 free( splitsectionlengths );
505 splitsectionlengths[0] = nVertices;
511 minsectlon =
MIN( minsectlon, bufx[0] );
512 maxsectlon =
MAX( minsectlon, bufx[0] );
513 minsectlat =
MIN( minsectlat, bufy[0] );
514 maxsectlat =
MAX( maxsectlat, bufy[0] );
517 rebaselon( &bufx[0], ( minx + maxx ) / 2.0 );
518 for ( i = 1; i < nVertices; i++ )
521 rebaselon( &bufx[i], ( minx + maxx ) / 2.0 );
524 if ( bufx[i - 1] - bufx[i] > 180. || bufx[i - 1] - bufx[i] < -180. )
530 appendresult +=
appendfltptr( &splitx, nsplitsections, bufx + i );
531 appendresult +=
appendfltptr( &splity, nsplitsections, bufy + i );
532 appendresult +=
appendint( &splitsectionlengths, nsplitsections, nVertices - i );
533 splitsectionlengths[nsplitsections - 1] -= splitsectionlengths[nsplitsections] - 1;
540 appendresult +=
appendfltptr( &splitx, nsplitsections, bufx + i - 1 );
541 appendresult +=
appendfltptr( &splity, nsplitsections, bufy + i - 1 );
542 appendresult +=
appendint( &splitsectionlengths, nsplitsections, nVertices - i + 1 );
543 splitsectionlengths[nsplitsections - 1] -= splitsectionlengths[nsplitsections] - 2;
546 if ( appendresult > 0 )
548 plabort(
"Could not allocate memory for appending to longitudinally split map data" );
554 free( splitsectionlengths );
560 minsectlon =
MIN( minsectlon, bufx[i] );
561 maxsectlon =
MAX( minsectlon, bufx[i] );
562 minsectlat =
MIN( minsectlat, bufy[i] );
563 maxsectlat =
MAX( maxsectlat, bufy[i] );
567 if ( ( maxsectlat > miny ) && ( minsectlat < maxy )
568 && ( maxsectlon > minx ) && ( minsectlon < maxx ) )
572 for ( i = 0; i < nsplitsections; ++i )
577 if ( splitsectionlengths[i] > 2 )
579 if ( splitx[i][1] - splitx[i][2] > 180. )
580 splitx[i][1] -= 360.0;
581 else if ( splitx[i][1] - splitx[i][2] < -180. )
582 splitx[i][1] += 360.0;
585 if ( splitx[i][0] - splitx[i][1] > 180. )
586 splitx[i][0] -= 360.0;
587 else if ( splitx[i][0] - splitx[i][1] < -180. )
588 splitx[i][0] += 360.0;
590 if ( splitx[i][splitsectionlengths[i] - 2] - splitx[i][splitsectionlengths[i] - 1] > 180. )
591 splitx[i][splitsectionlengths[i] - 1] += 360.0;
592 else if ( splitx[i][splitsectionlengths[i] - 2] - splitx[i][splitsectionlengths[i] - 1] < -180. )
593 splitx[i][splitsectionlengths[i] - 1] -= 360.0;
597 lastsplitpointx = splitx[i][splitsectionlengths[i] - 1];
598 lastsplitpointy = splity[i][splitsectionlengths[i] - 1];
599 penultimatesplitpointx = splitx[i][splitsectionlengths[i] - 2];
600 penultimatesplitpointy = splity[i][splitsectionlengths[i] - 2];
603 drawmapdata(
mapform, shapetype, splitsectionlengths[i], splitx[i], splity[i], dx, dy, just, text );
605 for ( j = 1; j < splitsectionlengths[i]; ++j )
607 if ( ( splitx[i][j] < 200.0 && splitx[i][j - 1] > 260.0 ) || ( splitx[i][j - 1] < 200.0 && splitx[i][j] > 260.0 ) )
608 plwarn(
"wrapping error" );
612 splitx[i][splitsectionlengths[i] - 1] = lastsplitpointx;
613 splity[i][splitsectionlengths[i] - 1] = lastsplitpointy;
614 splitx[i][splitsectionlengths[i] - 2] = penultimatesplitpointx;
615 splity[i][splitsectionlengths[i] - 2] = penultimatesplitpointy;
626 free( splitsectionlengths );
639 #else // defined (HAVE_SHAPELIB) || defined (PL_DEPRECATED)
640 plwarn(
"Use of the old plplot map file format is deprecated.\nIt is recommended that the shapelib library be used to provide map support.\n" );
641 #endif // defined (HAVE_SHAPELIB) || defined (PL_DEPRECATED)
691 miny, maxy, NULL, 0 );
714 const PLINT *plotentries,
PLINT nplotentries )
717 miny, maxy, plotentries, nplotentries );
731 const char *
name,
const char *
string,
733 const PLINT *plotentries,
PLINT nplotentries )
736 miny, maxy, plotentries, nplotentries );
755 miny, maxy, &plotentry, 1 );
772 miny, maxy, plotentries, nplotentries );
816 if ( minlong > maxlong )
822 if ( minlat > maxlat )
828 dx = ( maxlong - minlong ) /
NSEG;
829 dy = ( maxlat - minlat ) /
NSEG;
833 for ( yy = dlat * ceil( minlat / dlat ); yy <= maxlat; yy += dlat )
841 for ( xx = minlong; xx < maxlong; xx +=
dx )
846 ( *mapform )( 2, x, y );
854 for ( xx = dlong * ceil( minlong / dlong ); xx <= maxlong; xx += dlong )
862 for ( yy = minlat; yy < maxlat; yy +=
dy )
867 ( *mapform )( 2, x, y );
896 void CustomErrors(
const char *message )
898 if ( strstr( message,
"Unable to open" ) == NULL )
899 fprintf( stderr,
"%s\n", message );
904 OpenShapeFile(
const char *fn )
907 char *fs = NULL, *dn = NULL;
911 SASetupDefaultHooks( &sHooks );
912 sHooks.Error = CustomErrors;
918 #define SHPOpenLL( a, b, c ) SHPOpen( a, b )
927 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
933 #if defined ( PLPLOT_LIB_ENV )
938 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
942 #endif // PLPLOT_LIB_ENV
946 if ( ( file = SHPOpenLL( fn,
"rb", &sHooks ) ) != NULL )
948 pldebug(
"OpenShapeFile",
"Found file %s in current directory.\n", fn );
955 #if defined ( PLPLOT_HOME_ENV )
960 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
964 #endif // PLPLOT_HOME_ENV/lib
968 #if defined ( DATA_DIR )
971 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
980 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
985 pldebug(
"OpenShapeFile",
"File %s not found.\n", fn );
990 pldebug(
"OpenShapeFile",
"SHPOpen successfully opened two files with basename %s\n", fs );