56 #define OpenMap OpenShapeFile
57 #define CloseMap SHPClose
64 if ( *lon > midlon + 180.0 )
65 *lon -= floor( ( *lon - midlon - 180.0 ) / 360.0 + 1.0 ) * 360.0;
66 else if ( *lon < midlon - 180.0 )
67 *lon += floor( ( midlon - 180.0 - *lon ) / 360.0 + 1.0 ) * 360.0;
74 int appendflt(
PLFLT **array,
size_t n,
PLFLT val )
80 for ( i = 0; i < n; ++i )
81 temp[i] = ( *array )[i];
89 int appendint(
int **array,
size_t n,
int val )
92 int *temp = (
int *) malloc( ( n + 1 ) *
sizeof ( int ) );
95 for ( i = 0; i < n; ++i )
96 temp[i] = ( *array )[i];
104 int appendfltptr(
PLFLT ***array,
size_t n,
PLFLT *val )
110 for ( i = 0; i < n; ++i )
111 temp[i] = ( *array )[i];
137 return ( (
ABS( x[0] - resultx ) < 1.0e-12 ) && (
ABS( y[0] - resulty ) < 1.0e-12 ) );
149 if ( mapform != NULL )
150 ( *mapform )( n, x, y );
152 if ( shapetype == SHPT_ARC )
154 else if ( shapetype == SHPT_POINT )
155 for ( i = 0; i < n; ++i )
156 plptex( x[i], y[i], dx, dy, just, text );
157 else if ( shapetype == SHPT_POLYGON )
159 else if ( shapetype == SHPT_ARCZ || shapetype == SHPT_ARCM )
161 else if ( shapetype == SHPT_POLYGON || shapetype == SHPT_POLYGONZ || shapetype == SHPT_POLYGONM )
163 else if ( shapetype == SHPT_POINT || shapetype == SHPT_POINTM || shapetype == SHPT_POINTZ )
164 for ( i = 0; i < n; ++i )
165 plptex( x[i], y[i], dx, dy, just, text );
202 char *filename = NULL;
205 PLFLT minsectlon, maxsectlon, minsectlat, maxsectlat;
206 PLFLT *bufx = NULL, *bufy = NULL;
209 PLFLT **splitx = NULL;
210 PLFLT **splity = NULL;
211 int *splitsectionlengths = NULL;
213 PLFLT lastsplitpointx;
214 PLFLT lastsplitpointy;
215 PLFLT penultimatesplitpointx;
216 PLFLT penultimatesplitpointy;
218 int appendresult = 0;
230 SHPObject *
object = NULL;
233 char *prjfilename = NULL;
235 char prjtype[] = { 0, 0, 0, 0, 0, 0, 0 };
242 if ( strstr( name,
".shp" ) )
243 filenamelen = ( strlen( name ) - 4 );
245 filenamelen = strlen( name );
246 filename = (
char *) malloc( filenamelen + 1 );
249 plabort(
"Could not allocate memory for map filename root" );
252 strncpy( filename, name, filenamelen );
253 filename[ filenamelen ] =
'\0';
256 if ( ( in = OpenShapeFile( filename ) ) == NULL )
258 strcpy( warning,
"Could not find " );
259 strcat( warning, filename );
260 strcat( warning,
" file." );
265 SHPGetInfo( in, &nentries, &shapetype, mins, maxs );
268 prjfilename = (
char *) malloc( filenamelen + 5 );
272 plabort(
"Could not allocate memory for generating map projection filename" );
275 strncpy( prjfilename, name, filenamelen );
276 prjfilename[ filenamelen ] =
'\0';
277 strcat( prjfilename,
".prj" );
279 if ( prjfile && prjfile->
file )
281 fread( prjtype, 1, 6, prjfile->
file );
282 if ( strcmp( prjtype,
"PROJCS" ) == 0 )
303 if ( ( !plotentries && ( entrynumber == nentries ) ) || ( plotentries && ( entryindex == nplotentries ) ) )
307 if ( partnumber == 0 )
310 object = SHPReadObject( in, plotentries[entryindex] );
312 object = SHPReadObject( in, entrynumber );
316 if (
object == NULL )
325 if ( object->nParts == 0 )
326 nVertices =
object->nVertices;
327 else if ( partnumber == ( object->nParts - 1 ) )
328 nVertices =
object->nVertices -
object->panPartStart[partnumber];
330 nVertices =
object->panPartStart[partnumber + 1] -
object->panPartStart[partnumber];
332 if ( nVertices > bufsize )
337 bufx = (
PLFLT *) malloc( (
size_t) bufsize *
sizeof (
PLFLT ) );
338 bufy = (
PLFLT *) malloc( (
size_t) bufsize *
sizeof (
PLFLT ) );
339 if ( !bufx || !bufy )
341 plabort(
"Could not allocate memory for map data" );
352 if ( object->nParts > 0 )
354 bufxraw =
object->padfX +
object->panPartStart[partnumber];
355 bufyraw =
object->padfY +
object->panPartStart[partnumber];
359 bufxraw =
object->padfX;
360 bufyraw =
object->padfY;
363 for ( i = 0; i < nVertices; i++ )
365 bufx[i] = (
PLFLT) bufxraw[i];
366 bufy[i] = (
PLFLT) bufyraw[i];
370 minsectlon =
object->dfXMin;
371 maxsectlon =
object->dfXMax;
372 minsectlat =
object->dfYMin;
373 maxsectlat =
object->dfYMax;
377 if ( partnumber == object->nParts - 1 || object->nParts == 0 )
382 SHPDestroyObject(
object );
388 if ( nVertices == 0 )
409 splitx = (
PLFLT **) malloc(
sizeof (
PLFLT* ) );
410 splity = (
PLFLT **) malloc(
sizeof (
PLFLT* ) );
412 splitsectionlengths = (
int *) malloc(
sizeof (
size_t ) );
413 if ( !splitx || !splity || !splitsectionlengths )
415 plabort(
"Could not allocate memory for longitudinally split map data" );
421 free( splitsectionlengths );
424 splitsectionlengths[0] = nVertices;
430 minsectlon =
MIN( minsectlon, bufx[0] );
431 maxsectlon =
MAX( minsectlon, bufx[0] );
432 minsectlat =
MIN( minsectlat, bufy[0] );
433 maxsectlat =
MAX( maxsectlat, bufy[0] );
436 rebaselon( &bufx[0], ( minx + maxx ) / 2.0 );
437 for ( i = 1; i < nVertices; i++ )
440 rebaselon( &bufx[i], ( minx + maxx ) / 2.0 );
443 if ( bufx[i - 1] - bufx[i] > 180. || bufx[i - 1] - bufx[i] < -180. )
447 if ( checkwrap( mapform, bufx[i], bufy[i] ) )
449 appendresult += appendfltptr( &splitx, nsplitsections, bufx + i );
450 appendresult += appendfltptr( &splity, nsplitsections, bufy + i );
451 appendresult += appendint( &splitsectionlengths, nsplitsections, nVertices - i );
452 splitsectionlengths[nsplitsections - 1] -= splitsectionlengths[nsplitsections] - 1;
459 appendresult += appendfltptr( &splitx, nsplitsections, bufx + i - 1 );
460 appendresult += appendfltptr( &splity, nsplitsections, bufy + i - 1 );
461 appendresult += appendint( &splitsectionlengths, nsplitsections, nVertices - i + 1 );
462 splitsectionlengths[nsplitsections - 1] -= splitsectionlengths[nsplitsections] - 2;
465 if ( appendresult > 0 )
467 plabort(
"Could not allocate memory for appending to longitudinally split map data" );
473 free( splitsectionlengths );
479 minsectlon =
MIN( minsectlon, bufx[i] );
480 maxsectlon =
MAX( minsectlon, bufx[i] );
481 minsectlat =
MIN( minsectlat, bufy[i] );
482 maxsectlat =
MAX( maxsectlat, bufy[i] );
486 if ( ( maxsectlat > miny ) && ( minsectlat < maxy )
487 && ( maxsectlon > minx ) && ( minsectlon < maxx ) )
491 for ( i = 0; i < nsplitsections; ++i )
496 if ( splitsectionlengths[i] > 2 )
498 if ( splitx[i][1] - splitx[i][2] > 180. )
499 splitx[i][1] -= 360.0;
500 else if ( splitx[i][1] - splitx[i][2] < -180. )
501 splitx[i][1] += 360.0;
504 if ( splitx[i][0] - splitx[i][1] > 180. )
505 splitx[i][0] -= 360.0;
506 else if ( splitx[i][0] - splitx[i][1] < -180. )
507 splitx[i][0] += 360.0;
509 if ( splitx[i][splitsectionlengths[i] - 2] - splitx[i][splitsectionlengths[i] - 1] > 180. )
510 splitx[i][splitsectionlengths[i] - 1] += 360.0;
511 else if ( splitx[i][splitsectionlengths[i] - 2] - splitx[i][splitsectionlengths[i] - 1] < -180. )
512 splitx[i][splitsectionlengths[i] - 1] -= 360.0;
516 lastsplitpointx = splitx[i][splitsectionlengths[i] - 1];
517 lastsplitpointy = splity[i][splitsectionlengths[i] - 1];
518 penultimatesplitpointx = splitx[i][splitsectionlengths[i] - 2];
519 penultimatesplitpointy = splity[i][splitsectionlengths[i] - 2];
522 drawmapdata( mapform, shapetype, splitsectionlengths[i], splitx[i], splity[i], dx, dy, just, text );
524 for ( j = 1; j < splitsectionlengths[i]; ++j )
526 if ( ( splitx[i][j] < 200.0 && splitx[i][j - 1] > 260.0 ) || ( splitx[i][j - 1] < 200.0 && splitx[i][j] > 260.0 ) )
527 plwarn(
"wrapping error" );
531 splitx[i][splitsectionlengths[i] - 1] = lastsplitpointx;
532 splity[i][splitsectionlengths[i] - 1] = lastsplitpointy;
533 splitx[i][splitsectionlengths[i] - 2] = penultimatesplitpointx;
534 splity[i][splitsectionlengths[i] - 2] = penultimatesplitpointy;
540 drawmapdata( mapform, shapetype, nVertices, bufx, bufy, dx, dy, just, text );
545 free( splitsectionlengths );
555 #endif //HAVE_SHAPELIB
603 drawmap( mapform, name, 0.0, 0.0, SHPT_ARC, 0.0, NULL, minx, maxx,
604 miny, maxy, NULL, 0 );
606 plwarn(
"plmap is a no-op because shapelib is not available." );
633 drawmap( mapform, name, 0.0, 0.0, SHPT_ARC, 0.0,
"", minx, maxx,
634 miny, maxy, plotentries, nplotentries );
636 plwarn(
"plmapline is a no-op because shapelib is not available." );
656 drawmap( mapform, name, 1.0, 0.0, SHPT_POINT, 0.5,
string, minx, maxx,
657 miny, maxy, plotentries, nplotentries );
659 plwarn(
"plmapstring is a no-op because shapelib is not available." );
679 drawmap( mapform, name, dx, dy, SHPT_POINT, just, text, minx, maxx,
680 miny, maxy, &plotentry, 1 );
682 plwarn(
"plmaptex is a no-op because shapelib is not available." );
700 drawmap( mapform, name, 0.0, 0.0, SHPT_POLYGON, 0.0, NULL, minx, maxx,
701 miny, maxy, plotentries, nplotentries );
703 plwarn(
"plmapfill is a no-op because shapelib is not available." );
746 PLFLT yy, xx, temp, x[2], y[2], dx, dy;
748 if ( minlong > maxlong )
754 if ( minlat > maxlat )
760 dx = ( maxlong - minlong ) /
NSEG;
761 dy = ( maxlat - minlat ) /
NSEG;
765 for ( yy = dlat * ceil( minlat / dlat ); yy <= maxlat; yy += dlat )
767 if ( mapform == NULL )
773 for ( xx = minlong; xx < maxlong; xx += dx )
778 ( *mapform )( 2, x, y );
786 for ( xx = dlong * ceil( minlong / dlong ); xx <= maxlong; xx += dlong )
788 if ( mapform == NULL )
794 for ( yy = minlat; yy < maxlat; yy += dy )
799 ( *mapform )( 2, x, y );
830 if ( strstr( message,
"Unable to open" ) == NULL )
831 fprintf( stderr,
"%s\n", message );
839 char *fs = NULL, *dn = NULL;
843 SASetupDefaultHooks( &sHooks );
844 sHooks.Error = CustomErrors;
850 #define SHPOpenLL( a, b, c ) SHPOpen( a, b )
851 #endif //HAVE_SAHOOKS
859 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
865 #if defined ( PLPLOT_LIB_ENV )
870 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
874 #endif // PLPLOT_LIB_ENV
878 if ( ( file = SHPOpenLL( fn,
"rb", &sHooks ) ) != NULL )
880 pldebug(
"OpenShapeFile",
"Found file %s in current directory.\n", fn );
887 #if defined ( PLPLOT_HOME_ENV )
892 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
896 #endif // PLPLOT_HOME_ENV/lib
900 #if defined ( DATA_DIR )
903 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
912 if ( ( file = SHPOpenLL( fs,
"rb", &sHooks ) ) != NULL )
917 pldebug(
"OpenShapeFile",
"File %s not found.\n", fn );
922 pldebug(
"OpenShapeFile",
"SHPOpen successfully opened two files with basename %s\n", fs );
926 #endif //ifdef HAVE_SHAPELIB
void plGetName(PLCHAR_VECTOR dir, PLCHAR_VECTOR subdir, PLCHAR_VECTOR filename, char **filespec)
void(* PLMAPFORM_callback)(PLINT n, PLFLT_NC_VECTOR x, PLFLT_NC_VECTOR y)
void mapform(PLINT n, PLFLT *x, PLFLT *y)
void plmapline(PLMAPFORM_callback mapform, PLCHAR_VECTOR name, PLFLT minx, PLFLT maxx, PLFLT miny, PLFLT maxy, PLINT_VECTOR plotentries, PLINT nplotentries)
const char * PLCHAR_VECTOR
void plmeridians(PLMAPFORM_callback mapform, PLFLT dlong, PLFLT dlat, PLFLT minlong, PLFLT maxlong, PLFLT minlat, PLFLT maxlat)
void plmaptex(PLMAPFORM_callback mapform, PLCHAR_VECTOR name, PLFLT dx, PLFLT dy, PLFLT just, PLCHAR_VECTOR text, PLFLT minx, PLFLT maxx, PLFLT miny, PLFLT maxy, PLINT plotentry)
void plabort(PLCHAR_VECTOR errormsg)
const PLINT * PLINT_VECTOR
void plmapfill(PLMAPFORM_callback mapform, PLCHAR_VECTOR name, PLFLT minx, PLFLT maxx, PLFLT miny, PLFLT maxy, PLINT_VECTOR plotentries, PLINT nplotentries)
int pdf_close(PDFstrm *pdfs)
void plwarn(PLCHAR_VECTOR errormsg)
void plmap(PLMAPFORM_callback mapform, PLCHAR_VECTOR name, PLFLT minx, PLFLT maxx, PLFLT miny, PLFLT maxy)
void plmapstring(PLMAPFORM_callback mapform, PLCHAR_VECTOR name, PLCHAR_VECTOR string, PLFLT minx, PLFLT maxx, PLFLT miny, PLFLT maxy, PLINT_VECTOR plotentries, PLINT nplotentries)
PDFstrm * plLibOpenPdfstrm(PLCHAR_VECTOR fn)