/* ==================================================================== Read all SHP files in source directory and save to directory '.view' in dest directory. ==================================================================== */ int shp_all_to_bmp( void ) { char path[MAXPATHLEN]; int length; DIR *dir = 0; PG_Shp *shp; struct dirent *dirent = 0; /* open directory */ if ( ( dir = opendir( source_path ) ) == 0 ) { fprintf( stderr, "%s: can't open directory\n", source_path ); return 0; } while ( ( dirent = readdir( dir ) ) != 0 ) { if ( dirent->d_name[0] == '.' ) continue; if ( !strncmp( "tacally.shp", strlower( dirent->d_name ), 11 ) ) continue; if ( !strncmp( "tacgerm.shp", strlower( dirent->d_name ), 11 ) ) continue; if ( !strncmp( "a_", strlower( dirent->d_name ), 2 ) ) continue; length = strlen( dirent->d_name ); if ( (dirent->d_name[length - 1] != 'P' || dirent->d_name[length - 2] != 'H' || dirent->d_name[length - 3] != 'S') && (dirent->d_name[length - 1] != 'p' || dirent->d_name[length - 2] != 'h' || dirent->d_name[length - 3] != 's') ) continue; printf( "%s...\n", dirent->d_name ); if ( ( shp = shp_load( dirent->d_name ) ) == 0 ) continue; snprintf( path, MAXPATHLEN, "%s/.view/%s.bmp", dest_path, dirent->d_name ); SDL_SaveBMP( shp->surf, path ); shp_free( &shp ); } closedir( dir ); return 1; }
int main(int argc, char **argv) { SHP *shp; GEOMETRY *geo; DBUG_ENTER("main"); DBUG_PROCESS(argv[0]); DBUG_PUSH("d:t"); if(argc != 2) { printf("usage %s <shapefile.shp>\n", argv[0]); DBUG_RETURN(-1); } if(!(shp = shp_init(0))) { printf("Couldn't init\n"); DBUG_RETURN(-2); } if(shp_open(shp, argv[1], 'r') < 0) { printf("Couldn't open\n"); DBUG_RETURN(-3); } shp_dump(shp); while((geo = shp_read_next(shp))) { geometry_dump(geo, 1); geometry_free(geo); } shp_close(shp); shp_free(shp); DBUG_RETURN(0); }
/* ==================================================================== Load a SHP file to a PG_Shp struct. Since SHP files are only found in source path (=original PG data), for simplicity this function only gets the file name and the source_path is always prepended for opening the file. ==================================================================== */ PG_Shp *shp_load( const char *fname ) { int i; FILE *file = 0; char path[MAXPATHLEN]; int dummy; int width = 0, height = 0; int old_pos, pos, pal_pos; PG_Shp *shp = 0; SDL_PixelFormat *pf = SDL_GetVideoSurface()->format; Uint32 ckey = MAPRGB( CKEY_RED, CKEY_GREEN, CKEY_BLUE ); /* transparent color key */ Icon_Header header; RGB_Entry pal[256]; RGB_Entry *actual_pal = 0; int icon_maxw = 60, icon_maxh = 50; snprintf( path, MAXPATHLEN, "%s/%s", source_path, fname ); if ( ( file = fopen_ic( path, "r" ) ) == NULL ) { printf("Could not open file %s\n",path); return NULL; } /* magic */ fread( &dummy, 4, 1, file ); /* shp struct */ shp = calloc( 1, sizeof( PG_Shp ) ); /* icon count */ fread( &shp->count, 4, 1, file ); shp->count = SDL_SwapLE32(shp->count); if ( shp->count == 0 ) { fprintf( stderr, "%s: no icons found\n", path ); goto failure; } /* create surface (measure size first) */ for ( i = 0; i < shp->count; i++ ) { /* read file position of actual data and palette */ fread( &pos, 4, 1, file ); pos = SDL_SwapLE32(pos); fread( &dummy, 4, 1, file ); old_pos = ftell( file ); /* read header */ fseek( file, pos, SEEK_SET ); shp_read_icon_header( file, &header ); /* XXX if icon is too large, ignore it and replace with an empty * icon of maximum size; use hardcoded limit which is basically okay * as we convert PG data and can assume the icons have size of map * tile at maximum. */ if ( header.width > icon_maxw || header.height > icon_maxh ) { fprintf( stderr, "Icon %d in %s is too large (%dx%d), replacing " "with empty icon\n", i, fname, header.width,header.height ); header.width = icon_maxw; header.height = icon_maxh; header.valid = 0; } if ( header.width > width ) width = header.width; height += header.height; fseek( file, old_pos, SEEK_SET ); } shp->surf = SDL_CreateRGBSurface( SDL_SWSURFACE, width, height, pf->BitsPerPixel, pf->Rmask, pf->Gmask, pf->Bmask, pf->Amask ); if ( shp->surf == 0 ) { fprintf( stderr, "error creating surface: %s\n", SDL_GetError() ); goto failure; } SDL_FillRect( shp->surf, 0, ckey ); /* read icons */ shp->offsets = calloc( shp->count, sizeof( int ) ); shp->headers = calloc( shp->count, sizeof( Icon_Header ) ); fseek( file, 8, SEEK_SET ); for ( i = 0; i < shp->count; i++ ) { /* read position of data and palette */ pos = pal_pos = 0; fread( &pos, 4, 1, file ); pos = SDL_SwapLE32(pos); fread( &pal_pos, 4, 1, file ); pal_pos = SDL_SwapLE32(pal_pos); old_pos = ftell( file ); /* read palette */ if ( !use_def_pal && pal_pos > 0 ) { fseek( file, pal_pos, SEEK_SET ); shp_read_palette( file, pal ); actual_pal = pal; } else actual_pal = def_pal; /* read header */ fseek( file, pos, SEEK_SET ); shp_read_icon_header( file, &header ); /* see comment in measure loop above; have empty icon if too large */ if ( header.width > icon_maxw || header.height > icon_maxh ) { /* error message already given in measure loop */ header.width = icon_maxw; header.height = icon_maxh; header.valid = 0; } if ( header.valid ) shp_read_icon( file, shp->surf, shp->offsets[i], actual_pal, &header ); if ( i < shp->count - 1 ) shp->offsets[i + 1] = shp->offsets[i] + header.height; memcpy( &shp->headers[i], &header, sizeof( Icon_Header ) ); fseek( file, old_pos, SEEK_SET ); } fclose( file ); return shp; failure: if ( file ) fclose( file ); if ( shp ) shp_free( &shp ); return 0; }