int main( int argc, char ** argv ) { SHPHandle hSHP; int nShapeType, nEntities, i, iPart, bValidate = 0,nInvalidCount=0; int bHeaderOnly = 0; const char *pszPlus; double adfMinBound[4], adfMaxBound[4]; if( argc > 1 && strcmp(argv[1],"-validate") == 0 ) { bValidate = 1; argv++; argc--; } if( argc > 1 && strcmp(argv[1],"-ho") == 0 ) { bHeaderOnly = 1; argv++; argc--; } /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ if( argc != 2 ) { printf( "shpdump [-validate] [-ho] shp_file\n" ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Open the passed shapefile. */ /* -------------------------------------------------------------------- */ hSHP = SHPOpen( argv[1], "rb" ); if( hSHP == NULL ) { printf( "Unable to open:%s\n", argv[1] ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Print out the file bounds. */ /* -------------------------------------------------------------------- */ SHPGetInfo( hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound ); printf( "Shapefile Type: %s # of Shapes: %d\n\n", SHPTypeName( nShapeType ), nEntities ); printf( "File Bounds: (%.15g,%.15g,%.15g,%.15g)\n" " to (%.15g,%.15g,%.15g,%.15g)\n", adfMinBound[0], adfMinBound[1], adfMinBound[2], adfMinBound[3], adfMaxBound[0], adfMaxBound[1], adfMaxBound[2], adfMaxBound[3] ); /* -------------------------------------------------------------------- */ /* Skim over the list of shapes, printing all the vertices. */ /* -------------------------------------------------------------------- */ for( i = 0; i < nEntities && !bHeaderOnly; i++ ) { int j; SHPObject *psShape; psShape = SHPReadObject( hSHP, i ); if( psShape == NULL ) { fprintf( stderr, "Unable to read shape %d, terminating object reading.\n", i ); break; } if( psShape->bMeasureIsUsed ) printf( "\nShape:%d (%s) nVertices=%d, nParts=%d\n" " Bounds:(%.15g,%.15g, %.15g, %.15g)\n" " to (%.15g,%.15g, %.15g, %.15g)\n", i, SHPTypeName(psShape->nSHPType), psShape->nVertices, psShape->nParts, psShape->dfXMin, psShape->dfYMin, psShape->dfZMin, psShape->dfMMin, psShape->dfXMax, psShape->dfYMax, psShape->dfZMax, psShape->dfMMax ); else printf( "\nShape:%d (%s) nVertices=%d, nParts=%d\n" " Bounds:(%.15g,%.15g, %.15g)\n" " to (%.15g,%.15g, %.15g)\n", i, SHPTypeName(psShape->nSHPType), psShape->nVertices, psShape->nParts, psShape->dfXMin, psShape->dfYMin, psShape->dfZMin, psShape->dfXMax, psShape->dfYMax, psShape->dfZMax ); if( psShape->nParts > 0 && psShape->panPartStart[0] != 0 ) { fprintf( stderr, "panPartStart[0] = %d, not zero as expected.\n", psShape->panPartStart[0] ); } for( j = 0, iPart = 1; j < psShape->nVertices; j++ ) { const char *pszPartType = ""; if( j == 0 && psShape->nParts > 0 ) pszPartType = SHPPartTypeName( psShape->panPartType[0] ); if( iPart < psShape->nParts && psShape->panPartStart[iPart] == j ) { pszPartType = SHPPartTypeName( psShape->panPartType[iPart] ); iPart++; pszPlus = "+"; } else pszPlus = " "; if( psShape->bMeasureIsUsed ) printf(" %s (%.15g,%.15g, %.15g, %.15g) %s \n", pszPlus, psShape->padfX[j], psShape->padfY[j], psShape->padfZ[j], psShape->padfM[j], pszPartType ); else printf(" %s (%.15g,%.15g, %.15g) %s \n", pszPlus, psShape->padfX[j], psShape->padfY[j], psShape->padfZ[j], pszPartType ); } if( bValidate ) { int nAltered = SHPRewindObject( hSHP, psShape ); if( nAltered > 0 ) { printf( " %d rings wound in the wrong direction.\n", nAltered ); nInvalidCount++; } } SHPDestroyObject( psShape ); } SHPClose( hSHP ); if( bValidate ) { printf( "%d object has invalid ring orderings.\n", nInvalidCount ); } #ifdef USE_DBMALLOC malloc_dump(2); #endif exit( 0 ); }
int main(int argc, char *argv[]) { int opt; size_t i; const char *argv0 = argv[0]; struct rt_wdb *fd_out; struct bu_vls vls_in = BU_VLS_INIT_ZERO; struct bu_vls vls_out = BU_VLS_INIT_ZERO; int opt_debug = 0; int opt_verbose = 0; /* shapelib vars */ SHPHandle shapefile; size_t shp_num_invalid = 0; int shp_num_entities = 0; int shp_type = 0; /* intentionally double for scan */ double shp_min[4] = HINIT_ZERO; double shp_max[4] = HINIT_ZERO; /* geometry */ point2d_t *verts = NULL; size_t num_verts = 0; if (argc < 2) { usage(argv0); bu_exit(1, NULL); } while ((opt = bu_getopt(argc, argv, "dxv")) != -1) { switch (opt) { case 'd': opt_debug = 1; break; case 'x': sscanf(bu_optarg, "%x", (unsigned int *) &RTG.debug); bu_printb("librt RT_G_DEBUG", RT_G_DEBUG, DEBUG_FORMAT); bu_log("\n"); break; case 'v': opt_verbose++; break; default: usage(argv0); bu_exit(1, NULL); break; } } argv += bu_optind; argc -= bu_optind; if (opt_verbose) bu_log("Verbose output enabled.\n"); if (opt_debug) bu_log("Debugging output enabled.\n"); /* validate input/output file specifiers */ if (argc < 1) { usage(argv0); bu_exit(1, "ERROR: Missing input and output file names\n"); } bu_vls_strcat(&vls_in, argv[0]); if (argc < 2) { bu_vls_printf(&vls_out, "%s.g", argv[0]); } else { bu_vls_strcat(&vls_out, argv[1]); } if (opt_verbose) { bu_log("Reading from [%s]\n", bu_vls_addr(&vls_in)); bu_log("Writing to [%s]\n\n", bu_vls_addr(&vls_out)); } /* initialize single threaded resource */ rt_init_resource(&rt_uniresource, 0, NULL); /* open the input */ shapefile = SHPOpen(bu_vls_addr(&vls_in), "rb"); if (!shapefile) { bu_log("ERROR: Unable to open shapefile [%s]\n", bu_vls_addr(&vls_in)); bu_vls_free(&vls_in); bu_vls_free(&vls_out); bu_exit(4, NULL); } /* print shapefile details */ if (opt_verbose) { SHPGetInfo(shapefile, &shp_num_entities, &shp_type, shp_min, shp_max); bu_log("Shapefile Type: %s\n", SHPTypeName(shp_type)); bu_log("# of Shapes: %d\n\n", shp_num_entities); bu_log("File Bounds: (%12.3f,%12.3f, %.3g, %.3g)\n" " to (%12.3f,%12.3f, %.3g, %.3g)\n", shp_min[0], shp_min[1], shp_min[2], shp_min[3], shp_max[0], shp_max[1], shp_max[2], shp_max[3]); } /* open the .g for writing */ if ((fd_out = wdb_fopen(bu_vls_addr(&vls_out))) == NULL) { bu_log("ERROR: Unable to open shapefile [%s]\n", bu_vls_addr(&vls_out)); bu_vls_free(&vls_in); bu_vls_free(&vls_out); perror(argv0); bu_exit(5, NULL); } /* iterate over all entities */ for (i=0; i < (size_t)shp_num_entities; i++) { SHPObject *object; int shp_part; size_t j; object = SHPReadObject(shapefile, i); if (!object) { if (opt_debug) bu_log("Shape %zu of %zu is missing, skipping.\n", i+1, (size_t)shp_num_entities); continue; } /* validate the object */ if (opt_debug) { int shp_altered = SHPRewindObject(shapefile, object); if (shp_altered > 0) { bu_log("WARNING: Shape %zu of %zu has [%d] bad loop orientations.\n", i+1, (size_t)shp_num_entities, shp_altered); shp_num_invalid++; } } /* print detail header */ if (opt_verbose) { if (object->bMeasureIsUsed) { bu_log("\nShape:%zu (%s) nVertices=%d, nParts=%d\n" " Bounds:(%12.3f,%12.3f, %g, %g)\n" " to (%12.3f,%12.3f, %g, %g)\n", i+1, SHPTypeName(object->nSHPType), object->nVertices, object->nParts, object->dfXMin, object->dfYMin, object->dfZMin, object->dfMMin, object->dfXMax, object->dfYMax, object->dfZMax, object->dfMMax); } else { bu_log("\nShape:%zu (%s) nVertices=%d, nParts=%d\n" " Bounds:(%12.3f,%12.3f, %g)\n" " to (%12.3f,%12.3f, %g)\n", i+1, SHPTypeName(object->nSHPType), object->nVertices, object->nParts, object->dfXMin, object->dfYMin, object->dfZMin, object->dfXMax, object->dfYMax, object->dfZMax); } if (object->nParts > 0 && object->panPartStart[0] != 0) { if (opt_debug) bu_log("Shape %zu of %zu: panPartStart[0] = %d, not zero as expected.\n", i+1, (size_t)shp_num_entities, object->panPartStart[0]); continue; } } num_verts = 0; verts = (point2d_t *)bu_calloc((size_t)object->nVertices, sizeof(point2d_t), "alloc point array"); for (j = 0, shp_part = 1; j < (size_t)object->nVertices; j++) { if (shp_part < object->nParts && j == (size_t)object->panPartStart[shp_part]) { shp_part++; bu_log("Shape %zu of %zu: End of Loop\n", i+1, (size_t)shp_num_entities); make_shape(fd_out, opt_verbose, opt_debug, i, num_verts, verts); /* reset for next loop */ memset(verts, 0, sizeof(point2d_t) * object->nVertices); num_verts = 0; } bu_log("%zu/%zu:%zu/%zu\t\t", i+1, (size_t)shp_num_entities, j+1, (size_t)object->nVertices); bu_log("(%12.4f, %12.4f, %12.4f, %g)\n", object->padfX[j], object->padfY[j], object->padfZ[j], object->padfM[j]); V2SET(verts[num_verts], object->padfX[j], object->padfY[j]); num_verts++; } bu_log("Shape %zu of %zu: End of Loop\n", i+1, (size_t)shp_num_entities); make_shape(fd_out, opt_verbose, opt_debug, i, num_verts, verts); bu_free(verts, "free point array"); verts = NULL; num_verts = 0; SHPDestroyObject(object); object = NULL; } if (opt_verbose) { if (shp_num_invalid > 0) { bu_log("WARNING: %zu of %zu shape(s) had bad loop orientations.\n", shp_num_invalid, (size_t)shp_num_entities); } bu_log("\nDone.\n"); } /* close up our files */ SHPClose(shapefile); wdb_close(fd_out); /* free up allocated resources */ bu_vls_free(&vls_in); bu_vls_free(&vls_out); return 0; }
int main( int argc, char ** argv ) { SHPHandle hSHP, hSHPOut; int nShapeType, nEntities, i, nInvalidCount=0; double adfMinBound[4], adfMaxBound[4]; /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ if( argc != 3 ) { printf( "shprewind in_shp_file out_shp_file\n" ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Open the passed shapefile. */ /* -------------------------------------------------------------------- */ hSHP = SHPOpen( argv[1], "rb" ); if( hSHP == NULL ) { printf( "Unable to open:%s\n", argv[1] ); exit( 1 ); } SHPGetInfo( hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound ); /* -------------------------------------------------------------------- */ /* Create output shapefile. */ /* -------------------------------------------------------------------- */ hSHPOut = SHPCreate( argv[2], nShapeType ); if( hSHPOut == NULL ) { printf( "Unable to create:%s\n", argv[2] ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Skim over the list of shapes, printing all the vertices. */ /* -------------------------------------------------------------------- */ for( i = 0; i < nEntities; i++ ) { SHPObject *psShape; psShape = SHPReadObject( hSHP, i ); if( SHPRewindObject( hSHP, psShape ) ) nInvalidCount++; SHPWriteObject( hSHPOut, -1, psShape ); SHPDestroyObject( psShape ); } SHPClose( hSHP ); SHPClose( hSHPOut ); printf( "%d objects rewound.\n", nInvalidCount ); exit( 0 ); }
int main( int argc, char ** argv ) { SHPHandle hSHP; int nShapeType, nEntities, i, iPart, bValidate = 0,nInvalidCount=0; int bHeaderOnly = 0; double adfMinBound[4], adfMaxBound[4]; if( argc > 1 && strcmp(argv[1],"-validate") == 0 ) { bValidate = 1; argv++; argc--; } if( argc > 1 && strcmp(argv[1],"-ho") == 0 ) { bHeaderOnly = 1; argv++; argc--; } /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ if( argc != 2 ) { printf( "shpdump [-validate] [-ho] shp_file\n" ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Open the passed shapefile. */ /* -------------------------------------------------------------------- */ hSHP = SHPOpen( argv[1], "rb" ); if( hSHP == NULL ) { printf( "Unable to open:%s\n", argv[1] ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Print out the file bounds. */ /* -------------------------------------------------------------------- */ SHPGetInfo( hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound ); printf( "{\n" ); printf( " \"type\": \"%s\",\n", SHPTypeName( nShapeType ) ); printf( " \"shape_count\": %d,\n", nEntities ); printf( " \"bounding_box\": [%12.3f,%12.3f,%g,%g,%12.3f,%12.3f,%g,%g],\n", adfMinBound[0], adfMinBound[1], adfMinBound[2], adfMinBound[3], adfMaxBound[0], adfMaxBound[1], adfMaxBound[2], adfMaxBound[3] ); printf( " \"shapes\": [\n" ); int isFirst = 1; /* -------------------------------------------------------------------- */ /* Skim over the list of shapes, printing all the vertices. */ /* -------------------------------------------------------------------- */ for( i = 0; i < nEntities && !bHeaderOnly; i++ ) { int j; SHPObject *psShape; psShape = SHPReadObject( hSHP, i ); if( psShape == NULL ) { fprintf( stderr, "Unable to read shape %d, terminating object reading.\n", i ); break; } if (isFirst!=1) { printf( ",\n" ); } isFirst = 0; printf( " {\n" ); printf( " \"index\": %d,\n", i ); printf( " \"type\": \"%s\",\n", SHPTypeName(psShape->nSHPType) ); printf( " \"vertex_count\": %d,\n", psShape->nVertices ); printf( " \"part_count\": %d,\n", psShape->nParts ); if( psShape->bMeasureIsUsed ) { printf( " \"measure_is_used\": true,\n" ); printf( " \"bounding_box\": [%12.3f,%12.3f, %g, %g, %12.3f,%12.3f, %g, %g],\n", psShape->dfXMin, psShape->dfYMin, psShape->dfZMin, psShape->dfMMin, psShape->dfXMax, psShape->dfYMax, psShape->dfZMax, psShape->dfMMax ); } else { printf( " \"measure_is_used\": false,\n" ); printf( " \"bounding_box\": [%12.3f,%12.3f, %g, %12.3f,%12.3f, %g],\n", psShape->dfXMin, psShape->dfYMin, psShape->dfZMin, psShape->dfXMax, psShape->dfYMax, psShape->dfZMax ); } if( psShape->nParts > 0 && psShape->panPartStart[0] != 0 ) { fprintf( stderr, "panPartStart[0] = %d, not zero as expected.\n", psShape->panPartStart[0] ); } printf( " \"parts\": [\n"); int isFirstVertex = 1; for( j = 0, iPart = 0; j < psShape->nVertices; j++ ) { const char *pszPartType = ""; if( iPart < psShape->nParts && psShape->panPartStart[iPart] == j ) { pszPartType = SHPPartTypeName( psShape->panPartType[iPart] ); iPart++; if (j != 0) { printf( "\n ]\n" ); printf( "\n },\n" ); } printf( " {\n" ); printf( " \"type\": \"%s\",\n", pszPartType ); printf( " \"vertices\": [\n" ); isFirstVertex = 1; } if (isFirstVertex!=1) { printf( ",\n" ); } isFirstVertex = 0; if( psShape->bMeasureIsUsed ) printf(" { \"x\":%f, \"y\":%f, \"z\":%g, \"m\":%g }", psShape->padfX[j], psShape->padfY[j], psShape->padfZ[j], psShape->padfM[j]); else printf(" { \"x\":%f, \"y\":%f, \"z\":%g }", psShape->padfX[j], psShape->padfY[j], psShape->padfZ[j]); } printf( " ]\n"); printf( " }\n"); printf( " ]\n"); if( bValidate ) { int nAltered = SHPRewindObject( hSHP, psShape ); if( nAltered > 0 ) { printf( " %d rings wound in the wrong direction.\n", nAltered ); nInvalidCount++; } } printf( " }" ); SHPDestroyObject( psShape ); } printf( "\n ]\n" ); printf( "}\n" ); SHPClose( hSHP ); if( bValidate ) { printf( "%d object has invalid ring orderings.\n", nInvalidCount ); } #ifdef USE_DBMALLOC malloc_dump(2); #endif exit( 0 ); }
void load_shapes( const std::string & filename_, IShapeLoadCallback & callback ) /* int load_shapes( const std::string & filename_, std::vector< ptr< Shape > > & shapes // std::vector< agg::path_storage > &shapes ) */ //int load_shapes( const char *filename, agg::path_storage &path) { // assert( shapes.empty() ); const char *filename = filename_.c_str(); SHPHandle hSHP; int nShapeType, nEntities, i, iPart, bValidate = 0,nInvalidCount=0; const char *pszPlus; double adfMinBound[4], adfMaxBound[4]; bValidate = 1; /* if( argc > 1 && strcmp(argv[1],"-validate") == 0 ) { bValidate = 1; argv++; argc--; } */ /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ /* if( argc != 2 ) { printf( "shpdump [-validate] shp_file\n" ); exit( 1 ); } */ /* -------------------------------------------------------------------- */ /* Open the passed shapefile. */ /* -------------------------------------------------------------------- */ // const char *filename = "world_shape/world_adm0.shp"; /// const char *filename = "world_adm0.shp"; // fprintf( stdout, "trying to open '%s'\n", filename ); hSHP = SHPOpen( /*argv[1]*/ filename, "rb" ); if( hSHP == NULL ) { printf( "Unable to open:%s\n", filename ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Print out the file bounds. */ /* -------------------------------------------------------------------- */ SHPGetInfo( hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound ); #if 0 printf( "Shapefile Type: %s # of Shapes: %d\n\n", SHPTypeName( nShapeType ), nEntities ); printf( "File Bounds: (%12.3f,%12.3f,%g,%g)\n" " to (%12.3f,%12.3f,%g,%g)\n", adfMinBound[0], adfMinBound[1], adfMinBound[2], adfMinBound[3], adfMaxBound[0], adfMaxBound[1], adfMaxBound[2], adfMaxBound[3] ); #endif /* ok - rather than have an array of paths for each shape we include the meta data we should just keep the paths */ /* -------------------------------------------------------------------- */ /* Skim over the list of shapes, printing all the vertices. */ /* -------------------------------------------------------------------- */ for( i = 0; i < nEntities; i++ ) { agg::path_storage path; // ptr< Shape> shape = new Shape; // shapes.push_back( agg::path_storage() ); int j; SHPObject *psShape; psShape = SHPReadObject( hSHP, i ); #if 0 printf( "\nShape:%d (%s) nVertices=%d, nParts=%d\n" " Bounds:(%12.3f,%12.3f, %g, %g)\n" " to (%12.3f,%12.3f, %g, %g)\n", i, SHPTypeName(psShape->nSHPType), psShape->nVertices, psShape->nParts, psShape->dfXMin, psShape->dfYMin, psShape->dfZMin, psShape->dfMMin, psShape->dfXMax, psShape->dfYMax, psShape->dfZMax, psShape->dfMMax ); printf( "\n"); #endif for( j = 0, iPart = 1; j < psShape->nVertices; j++ ) { assert( iPart <= psShape->nParts); if( j == 0 ) { // definitions in shapefil.h assert( psShape->panPartType[ 0] == SHPP_RING ); // printf( "//// type %s\n", SHPPartTypeName( psShape->panPartType[0] )); //printf( "* move_to (1) %12.3f %12.3f\n", psShape->padfX[j], psShape->padfY[j]); path.move_to( psShape->padfX[j], psShape->padfY[j]); } // last item in a poly is close - gets last item ( note --- we dont want to add it again ) else if( j == psShape->nVertices - 1 ) { // printf( "* close poly (1) (ignore %12.3f %12.3f)\n", psShape->padfX[j], psShape->padfY[j]); path.close_polygon(); } else if( ( iPart < psShape->nParts && psShape->panPartStart[ iPart] - 1 == j )) { // printf( "* close poly (2) (ignore %12.3f %12.3f)\n", psShape->padfX[j], psShape->padfY[j]); path.close_polygon(); } else if( ( iPart < psShape->nParts && psShape->panPartStart[ iPart] == j )) { // only point where we increment iPart iPart++; //printf( "* move_to (2) %12.3f %12.3f\n", psShape->padfX[j], psShape->padfY[j]); path.move_to( psShape->padfX[j], psShape->padfY[j]); } else { // printf( "* line_to %12.3f %12.3f\n", psShape->padfX[j], psShape->padfY[j]); path.line_to( psShape->padfX[j], psShape->padfY[j]); } // the values seem to indicate that they are closed ... /* printf("%d %s (%12.3f,%12.3f, %g, %g) %s \n", iPart, pszPlus, psShape->padfX[j], psShape->padfY[j], psShape->padfZ[j], psShape->padfM[j], pszPartType ); */ } if( bValidate ) { int nAltered = SHPRewindObject( hSHP, psShape ); if( nAltered > 0 ) { printf( " %d rings wound in the wrong direction.\n", nAltered ); nInvalidCount++; exit( 0); } } SHPDestroyObject( psShape ); callback.add_shape( i, path ); // shapes.push_back( shape); } SHPClose( hSHP ); if( bValidate && nInvalidCount ) { printf( "%d object has invalid ring orderings.\n", nInvalidCount ); } #ifdef USE_DBMALLOC malloc_dump(2); #endif // exit( 0 ); }