423 |
|
|
424 |
{ |
{ |
425 |
char *pszFullname, *pszBasename; |
char *pszFullname, *pszBasename; |
426 |
SHPHandle psSHP; |
FILE *fpSHP, *fpSHX; |
|
|
|
|
uchar *pabyBuf; |
|
427 |
int i; |
int i; |
|
double dValue; |
|
428 |
|
|
429 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
430 |
/* Ensure the access string is one of the legal ones. We */ |
/* Ensure the access string is one of the legal ones. We */ |
447 |
bBigEndian = TRUE; |
bBigEndian = TRUE; |
448 |
|
|
449 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
|
/* Initialize the info structure. */ |
|
|
/* -------------------------------------------------------------------- */ |
|
|
psSHP = (SHPHandle) calloc(sizeof(SHPInfo),1); |
|
|
|
|
|
psSHP->bUpdated = FALSE; |
|
|
|
|
|
/* -------------------------------------------------------------------- */ |
|
450 |
/* Compute the base (layer) name. If there is any extension */ |
/* Compute the base (layer) name. If there is any extension */ |
451 |
/* on the passed in filename we will strip it off. */ |
/* on the passed in filename we will strip it off. */ |
452 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
466 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
467 |
pszFullname = (char *) malloc(strlen(pszBasename) + 5); |
pszFullname = (char *) malloc(strlen(pszBasename) + 5); |
468 |
sprintf( pszFullname, "%s.shp", pszBasename ); |
sprintf( pszFullname, "%s.shp", pszBasename ); |
469 |
psSHP->fpSHP = fopen(pszFullname, pszAccess ); |
fpSHP = fopen(pszFullname, pszAccess ); |
470 |
if( psSHP->fpSHP == NULL ) |
if( fpSHP == NULL ) |
471 |
{ |
{ |
472 |
sprintf( pszFullname, "%s.SHP", pszBasename ); |
sprintf( pszFullname, "%s.SHP", pszBasename ); |
473 |
psSHP->fpSHP = fopen(pszFullname, pszAccess ); |
fpSHP = fopen(pszFullname, pszAccess ); |
474 |
} |
} |
475 |
|
|
476 |
if( psSHP->fpSHP == NULL ) |
if( fpSHP == NULL ) |
477 |
{ |
{ |
478 |
#ifdef USE_CPL |
#ifdef USE_CPL |
479 |
CPLError( CE_Failure, CPLE_OpenFailed, |
CPLError( CE_Failure, CPLE_OpenFailed, |
480 |
"Unable to open %s.shp or %s.SHP.", |
"Unable to open %s.shp or %s.SHP.", |
481 |
pszBasename, pszBasename ); |
pszBasename, pszBasename ); |
482 |
#endif |
#endif |
|
free( psSHP ); |
|
483 |
free( pszBasename ); |
free( pszBasename ); |
484 |
free( pszFullname ); |
free( pszFullname ); |
485 |
return( NULL ); |
return( NULL ); |
486 |
} |
} |
487 |
|
|
488 |
sprintf( pszFullname, "%s.shx", pszBasename ); |
sprintf( pszFullname, "%s.shx", pszBasename ); |
489 |
psSHP->fpSHX = fopen(pszFullname, pszAccess ); |
fpSHX = fopen(pszFullname, pszAccess ); |
490 |
if( psSHP->fpSHX == NULL ) |
if( fpSHX == NULL ) |
491 |
{ |
{ |
492 |
sprintf( pszFullname, "%s.SHX", pszBasename ); |
sprintf( pszFullname, "%s.SHX", pszBasename ); |
493 |
psSHP->fpSHX = fopen(pszFullname, pszAccess ); |
fpSHX = fopen(pszFullname, pszAccess ); |
494 |
} |
} |
495 |
|
|
496 |
if( psSHP->fpSHX == NULL ) |
if( fpSHX == NULL ) |
497 |
{ |
{ |
498 |
#ifdef USE_CPL |
#ifdef USE_CPL |
499 |
CPLError( CE_Failure, CPLE_OpenFailed, |
CPLError( CE_Failure, CPLE_OpenFailed, |
500 |
"Unable to open %s.shx or %s.SHX.", |
"Unable to open %s.shx or %s.SHX.", |
501 |
pszBasename, pszBasename ); |
pszBasename, pszBasename ); |
502 |
#endif |
#endif |
503 |
fclose( psSHP->fpSHP ); |
fclose( fpSHP ); |
504 |
free( psSHP ); |
free( pszBasename ); |
505 |
|
free( pszFullname ); |
506 |
|
return( NULL ); |
507 |
|
} |
508 |
|
|
509 |
|
free( pszFullname ); |
510 |
|
free( pszBasename ); |
511 |
|
|
512 |
|
return SHPOpenEx( fpSHP, fpSHX ); |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
|
/************************************************************************/ |
517 |
|
/* SHPOpenW() */ |
518 |
|
/* */ |
519 |
|
/* Open the .shp and .shx files based on the basename of the */ |
520 |
|
/* files or either file name, for wide character filenames */ |
521 |
|
/************************************************************************/ |
522 |
|
|
523 |
|
#ifdef SHPAPI_HAS_WIDE |
524 |
|
|
525 |
|
SHPHandle SHPAPI_CALL |
526 |
|
SHPOpenW( const wchar_t * pszLayer, const wchar_t * pszAccess ) |
527 |
|
|
528 |
|
{ |
529 |
|
wchar_t *pszFullname, *pszBasename; |
530 |
|
FILE *fpSHP, *fpSHX; |
531 |
|
int i; |
532 |
|
|
533 |
|
/* -------------------------------------------------------------------- */ |
534 |
|
/* Ensure the access string is one of the legal ones. We */ |
535 |
|
/* ensure the result string indicates binary to avoid common */ |
536 |
|
/* problems on Windows. */ |
537 |
|
/* -------------------------------------------------------------------- */ |
538 |
|
if( wcscmp(pszAccess,L"rb+") == 0 || wcscmp(pszAccess,L"r+b") == 0 |
539 |
|
|| wcscmp(pszAccess,L"r+") == 0 ) |
540 |
|
pszAccess = L"r+b"; |
541 |
|
else |
542 |
|
pszAccess = L"rb"; |
543 |
|
|
544 |
|
/* -------------------------------------------------------------------- */ |
545 |
|
/* Compute the base (layer) name. If there is any extension */ |
546 |
|
/* on the passed in filename we will strip it off. */ |
547 |
|
/* -------------------------------------------------------------------- */ |
548 |
|
pszBasename = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszLayer)+5)); |
549 |
|
wcscpy( pszBasename, pszLayer ); |
550 |
|
for( i = wcslen(pszBasename)-1; |
551 |
|
i > 0 && pszBasename[i] != L'.' && pszBasename[i] != L'/' |
552 |
|
&& pszBasename[i] != L'\\'; |
553 |
|
i-- ) {} |
554 |
|
|
555 |
|
if( pszBasename[i] == L'.' ) |
556 |
|
pszBasename[i] = L'\0'; |
557 |
|
|
558 |
|
/* -------------------------------------------------------------------- */ |
559 |
|
/* Open the .shp and .shx files. Note that files pulled from */ |
560 |
|
/* a PC to Unix with upper case filenames won't work! */ |
561 |
|
/* -------------------------------------------------------------------- */ |
562 |
|
pszFullname = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszBasename) + 5)); |
563 |
|
swprintf( pszFullname, L"%s.shp", pszBasename ); |
564 |
|
fpSHP = _wfopen(pszFullname, pszAccess ); |
565 |
|
if( fpSHP == NULL ) |
566 |
|
{ |
567 |
|
swprintf( pszFullname, L"%s.SHP", pszBasename ); |
568 |
|
fpSHP = _wfopen(pszFullname, pszAccess ); |
569 |
|
} |
570 |
|
|
571 |
|
if( fpSHP == NULL ) |
572 |
|
{ |
573 |
|
#ifdef USE_CPL |
574 |
|
CPLError( CE_Failure, CPLE_OpenFailed, |
575 |
|
"Unable to open .shp file." ); |
576 |
|
#endif |
577 |
|
free( pszBasename ); |
578 |
|
free( pszFullname ); |
579 |
|
return( NULL ); |
580 |
|
} |
581 |
|
|
582 |
|
swprintf( pszFullname, L"%s.shx", pszBasename ); |
583 |
|
fpSHX = _wfopen(pszFullname, pszAccess ); |
584 |
|
if( fpSHX == NULL ) |
585 |
|
{ |
586 |
|
swprintf( pszFullname, L"%s.SHX", pszBasename ); |
587 |
|
fpSHX = _wfopen(pszFullname, pszAccess ); |
588 |
|
} |
589 |
|
|
590 |
|
if( fpSHX == NULL ) |
591 |
|
{ |
592 |
|
#ifdef USE_CPL |
593 |
|
CPLError( CE_Failure, CPLE_OpenFailed, |
594 |
|
"Unable to open .shx file." ); |
595 |
|
#endif |
596 |
|
fclose( fpSHP ); |
597 |
free( pszBasename ); |
free( pszBasename ); |
598 |
free( pszFullname ); |
free( pszFullname ); |
599 |
return( NULL ); |
return( NULL ); |
602 |
free( pszFullname ); |
free( pszFullname ); |
603 |
free( pszBasename ); |
free( pszBasename ); |
604 |
|
|
605 |
|
return SHPOpenEx( fpSHP, fpSHX ); |
606 |
|
} |
607 |
|
|
608 |
|
#endif |
609 |
|
|
610 |
|
/************************************************************************/ |
611 |
|
/* shpopen() */ |
612 |
|
/* */ |
613 |
|
/* Open the .shp and .shx files based on the basename of the */ |
614 |
|
/* files or either file name. */ |
615 |
|
/************************************************************************/ |
616 |
|
|
617 |
|
SHPHandle SHPAPI_CALL |
618 |
|
SHPOpenEx( FILE * fpSHP, FILE * fpSHX ) |
619 |
|
{ |
620 |
|
SHPHandle psSHP; |
621 |
|
uchar *pabyBuf; |
622 |
|
int i; |
623 |
|
double dValue; |
624 |
|
|
625 |
|
/* -------------------------------------------------------------------- */ |
626 |
|
/* Establish the byte order on this machine. */ |
627 |
|
/* -------------------------------------------------------------------- */ |
628 |
|
i = 1; |
629 |
|
if( *((uchar *) &i) == 1 ) |
630 |
|
bBigEndian = FALSE; |
631 |
|
else |
632 |
|
bBigEndian = TRUE; |
633 |
|
|
634 |
|
/* -------------------------------------------------------------------- */ |
635 |
|
/* Initialize the info structure. */ |
636 |
|
/* -------------------------------------------------------------------- */ |
637 |
|
psSHP = (SHPHandle) calloc(sizeof(SHPInfo),1); |
638 |
|
|
639 |
|
psSHP->fpSHP = fpSHP; |
640 |
|
psSHP->fpSHX = fpSHX; |
641 |
|
psSHP->bUpdated = FALSE; |
642 |
|
|
643 |
|
|
644 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
645 |
/* Read the file size from the SHP file. */ |
/* Read the file size from the SHP file. */ |
646 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
858 |
char *pszBasename, *pszFullname; |
char *pszBasename, *pszFullname; |
859 |
int i; |
int i; |
860 |
FILE *fpSHP, *fpSHX; |
FILE *fpSHP, *fpSHX; |
|
uchar abyHeader[100]; |
|
|
int32 i32; |
|
|
double dValue; |
|
|
|
|
|
/* -------------------------------------------------------------------- */ |
|
|
/* Establish the byte order on this system. */ |
|
|
/* -------------------------------------------------------------------- */ |
|
|
i = 1; |
|
|
if( *((uchar *) &i) == 1 ) |
|
|
bBigEndian = FALSE; |
|
|
else |
|
|
bBigEndian = TRUE; |
|
861 |
|
|
862 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
863 |
/* Compute the base (layer) name. If there is any extension */ |
/* Compute the base (layer) name. If there is any extension */ |
904 |
free( pszFullname ); |
free( pszFullname ); |
905 |
free( pszBasename ); |
free( pszBasename ); |
906 |
|
|
907 |
|
SHPCreateEx( fpSHP, fpSHX, nShapeType ); |
908 |
|
|
909 |
|
/* -------------------------------------------------------------------- */ |
910 |
|
/* Close the files, and then open them as regular existing files. */ |
911 |
|
/* -------------------------------------------------------------------- */ |
912 |
|
fclose( fpSHP ); |
913 |
|
fclose( fpSHX ); |
914 |
|
|
915 |
|
return( SHPOpen( pszLayer, "r+b" ) ); |
916 |
|
} |
917 |
|
|
918 |
|
#ifdef SHPAPI_HAS_WIDE |
919 |
|
|
920 |
|
/************************************************************************/ |
921 |
|
/* SHPCreate() */ |
922 |
|
/* */ |
923 |
|
/* Create a new shape file and return a handle to the open */ |
924 |
|
/* shape file with read/write access. */ |
925 |
|
/************************************************************************/ |
926 |
|
|
927 |
|
SHPHandle SHPAPI_CALL |
928 |
|
SHPCreateW( const wchar_t * pszLayer, int nShapeType ) |
929 |
|
|
930 |
|
{ |
931 |
|
wchar_t *pszBasename, *pszFullname; |
932 |
|
int i; |
933 |
|
FILE *fpSHP, *fpSHX; |
934 |
|
|
935 |
|
/* -------------------------------------------------------------------- */ |
936 |
|
/* Compute the base (layer) name. If there is any extension */ |
937 |
|
/* on the passed in filename we will strip it off. */ |
938 |
|
/* -------------------------------------------------------------------- */ |
939 |
|
pszBasename = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszLayer)+5)); |
940 |
|
wcscpy( pszBasename, pszLayer ); |
941 |
|
for( i = wcslen(pszBasename)-1; |
942 |
|
i > 0 && pszBasename[i] != L'.' && pszBasename[i] != L'/' |
943 |
|
&& pszBasename[i] != L'\\'; |
944 |
|
i-- ) {} |
945 |
|
|
946 |
|
if( pszBasename[i] == L'.' ) |
947 |
|
pszBasename[i] = L'\0'; |
948 |
|
|
949 |
|
/* -------------------------------------------------------------------- */ |
950 |
|
/* Open the two files so we can write their headers. */ |
951 |
|
/* -------------------------------------------------------------------- */ |
952 |
|
pszFullname = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszBasename) + 5)); |
953 |
|
swprintf( pszFullname, L"%s.shp", pszBasename ); |
954 |
|
fpSHP = _wfopen(pszFullname, L"wb" ); |
955 |
|
if( fpSHP == NULL ) |
956 |
|
{ |
957 |
|
#ifdef USE_CPL |
958 |
|
CPLError( CE_Failure, CPLE_AppDefined, |
959 |
|
"Failed to create file." ); |
960 |
|
#endif |
961 |
|
return( NULL ); |
962 |
|
} |
963 |
|
|
964 |
|
swprintf( pszFullname, L"%s.shx", pszBasename ); |
965 |
|
fpSHX = _wfopen(pszFullname, L"wb" ); |
966 |
|
if( fpSHX == NULL ) |
967 |
|
{ |
968 |
|
#ifdef USE_CPL |
969 |
|
CPLError( CE_Failure, CPLE_AppDefined, |
970 |
|
"Failed to create file." ); |
971 |
|
#endif |
972 |
|
return( NULL ); |
973 |
|
} |
974 |
|
|
975 |
|
free( pszFullname ); |
976 |
|
free( pszBasename ); |
977 |
|
|
978 |
|
SHPCreateEx( fpSHP, fpSHX, nShapeType ); |
979 |
|
|
980 |
|
/* -------------------------------------------------------------------- */ |
981 |
|
/* Close the files, and then open them as regular existing files. */ |
982 |
|
/* -------------------------------------------------------------------- */ |
983 |
|
fclose( fpSHP ); |
984 |
|
fclose( fpSHX ); |
985 |
|
|
986 |
|
return( SHPOpenW( pszLayer, L"r+b" ) ); |
987 |
|
} |
988 |
|
|
989 |
|
#endif |
990 |
|
|
991 |
|
/************************************************************************/ |
992 |
|
/* SHPCreateEx() */ |
993 |
|
/* */ |
994 |
|
/* Create a new shape file and return a handle to the open */ |
995 |
|
/* shape file with read/write access. */ |
996 |
|
/************************************************************************/ |
997 |
|
|
998 |
|
void SHPAPI_CALL |
999 |
|
SHPCreateEx( FILE * fpSHP, FILE * fpSHX, int nShapeType ) |
1000 |
|
|
1001 |
|
{ |
1002 |
|
int i; |
1003 |
|
uchar abyHeader[100]; |
1004 |
|
int32 i32; |
1005 |
|
double dValue; |
1006 |
|
|
1007 |
|
/* -------------------------------------------------------------------- */ |
1008 |
|
/* Establish the byte order on this system. */ |
1009 |
|
/* -------------------------------------------------------------------- */ |
1010 |
|
i = 1; |
1011 |
|
if( *((uchar *) &i) == 1 ) |
1012 |
|
bBigEndian = FALSE; |
1013 |
|
else |
1014 |
|
bBigEndian = TRUE; |
1015 |
|
|
1016 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
1017 |
/* Prepare header block for .shp file. */ |
/* Prepare header block for .shp file. */ |
1018 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
1049 |
CPLError( CE_Failure, CPLE_AppDefined, |
CPLError( CE_Failure, CPLE_AppDefined, |
1050 |
"Failed to write .shp header." ); |
"Failed to write .shp header." ); |
1051 |
#endif |
#endif |
1052 |
return NULL; |
return; |
1053 |
} |
} |
1054 |
|
|
1055 |
/* -------------------------------------------------------------------- */ |
/* -------------------------------------------------------------------- */ |
1065 |
CPLError( CE_Failure, CPLE_AppDefined, |
CPLError( CE_Failure, CPLE_AppDefined, |
1066 |
"Failed to write .shx header." ); |
"Failed to write .shx header." ); |
1067 |
#endif |
#endif |
1068 |
return NULL; |
return; |
1069 |
} |
} |
|
|
|
|
/* -------------------------------------------------------------------- */ |
|
|
/* Close the files, and then open them as regular existing files. */ |
|
|
/* -------------------------------------------------------------------- */ |
|
|
fclose( fpSHP ); |
|
|
fclose( fpSHX ); |
|
|
|
|
|
return( SHPOpen( pszLayer, "r+b" ) ); |
|
1070 |
} |
} |
1071 |
|
|
1072 |
|
|
1073 |
/************************************************************************/ |
/************************************************************************/ |
1074 |
/* _SHPSetBounds() */ |
/* _SHPSetBounds() */ |
1075 |
/* */ |
/* */ |