ms_bitarray msSearchDiskTree(char *filename, rectObj aoi, int debug) { SHPTreeHandle disktree; ms_bitarray status=NULL; disktree = msSHPDiskTreeOpen (filename, debug); if(!disktree) { /* only set this error IF debugging is turned on, gets annoying otherwise */ if(debug) msSetError(MS_NOTFOUND, "Unable to open spatial index for %s. In most cases you can safely ignore this message, otherwise check file names and permissions.", "msSearchDiskTree()", filename); return(NULL); } status = msAllocBitArray(disktree->nShapes); if(!status) { msSetError(MS_MEMERR, NULL, "msSearchDiskTree()"); msSHPDiskTreeClose( disktree ); return(NULL); } searchDiskTreeNode(disktree, aoi, status); msSHPDiskTreeClose( disktree ); return(status); }
char *msSearchDiskTree(const TCHAR *filename, rectObj aoi, int debug) { SHPTreeHandle disktree; char *status=NULL; disktree = msSHPDiskTreeOpen (filename, debug); if(!disktree) { #if MAPSHAPEERROR // only set this error IF debugging is turned on, gets annoying otherwise if(debug) msSetError(MS_IOERR, "Unable to open spatial index for %ls. In most cases you can safely ignore this message, otherwise check file names and permissions.", "msSearchDiskTree()", filename); #endif return(NULL); } status = msAllocBitArray(disktree->nShapes); if(!status) { #if MAPSHAPEERROR msSetError(MS_MEMERR, NULL, "msSearchDiskTree()"); #endif msSHPDiskTreeClose( disktree ); return(NULL); } searchDiskTreeNode(disktree, aoi, status); msSHPDiskTreeClose( disktree ); return(status); }
int msWriteTree(treeObj *tree, char *filename, int B_order) { char signature[3] = "SQT"; char version = 1; char reserved[3] = {0,0,0}; SHPTreeHandle disktree; int i; char mtBigEndian; char pabyBuf[32]; char *pszBasename, *pszFullname; disktree = (SHPTreeHandle) malloc(sizeof(SHPTreeInfo)); MS_CHECK_ALLOC(disktree, sizeof(SHPTreeInfo), MS_FALSE); /* -------------------------------------------------------------------- */ /* Compute the base (layer) name. If there is any extension */ /* on the passed in filename we will strip it off. */ /* -------------------------------------------------------------------- */ pszBasename = (char *) msSmallMalloc(strlen(filename)+5); strcpy( pszBasename, filename ); for( i = strlen(pszBasename)-1; i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/' && pszBasename[i] != '\\'; i-- ) {} if( pszBasename[i] == '.' ) pszBasename[i] = '\0'; /* -------------------------------------------------------------------- */ /* Open the .shp and .shx files. Note that files pulled from */ /* a PC to Unix with upper case filenames won't work! */ /* -------------------------------------------------------------------- */ pszFullname = (char *) msSmallMalloc(strlen(pszBasename) + 5); sprintf( pszFullname, "%s%s", pszBasename, MS_INDEX_EXTENSION); disktree->fp = fopen(pszFullname, "wb"); msFree(pszBasename); /* not needed */ msFree(pszFullname); if(!disktree->fp) { msFree(disktree); msSetError(MS_IOERR, NULL, "msWriteTree()"); return(MS_FALSE); } /* for efficiency, trim the tree */ msTreeTrim(tree); /* -------------------------------------------------------------------- */ /* Establish the byte order on this machine. */ /* -------------------------------------------------------------------- */ i = 1; if( *((uchar *) &i) == 1 ) mtBigEndian = MS_FALSE; else mtBigEndian = MS_TRUE; if( !(mtBigEndian ^ ( B_order == MS_LSB_ORDER || B_order == MS_NEW_LSB_ORDER )) ) disktree->needswap = 1; else disktree->needswap = 0; if( B_order == MS_NATIVE_ORDER ) disktree->needswap = 0; /* write the header */ if ( B_order > 0 ) { memcpy( pabyBuf, &signature, 3 ); memcpy (&disktree->signature, &signature, 3); pabyBuf[3] = B_order; memcpy( pabyBuf+4, &version, 1); memcpy( pabyBuf+5, &reserved, 3); memcpy( &disktree->version, &version, 1); memcpy( &disktree->flags, &reserved, 3); fwrite( pabyBuf, 8, 1, disktree->fp ); } memcpy( pabyBuf, &tree->numshapes, 4 ); if( disktree->needswap ) SwapWord( 4, pabyBuf ); memcpy( pabyBuf+4, &tree->maxdepth, 4 ); if( disktree->needswap ) SwapWord( 4, pabyBuf+4 ); i = fwrite( pabyBuf, 8, 1, disktree->fp ); if( !i ) { fprintf (stderr, "unable to write to index file ... exiting \n"); msSHPDiskTreeClose( disktree ); return (MS_FALSE); } writeTreeNode(disktree, tree->root); msSHPDiskTreeClose( disktree ); return(MS_TRUE); }
int main( int argc, char ** argv ) { SHPTreeHandle qix; int i, j; rectObj rect; int pos; ms_bitarray bitmap = NULL; char mBigEndian; treeNodeObj *node = NULL; /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ if( argc <= 1 ) { printf( "shptreetst shapefile {minx miny maxx maxy}\n" ); exit( 1 ); } i = 1; if( *((unsigned char *) &i) == 1 ) mBigEndian = 0; else mBigEndian = 1; qix = msSHPDiskTreeOpen (AddFileSuffix(argv[1],".qix"), 0 /* no debug*/); if( qix == NULL ) { printf("unable to open index file %s \n", argv[1]); exit(-1); } printf ("This %s %s index supports a shapefile with %d shapes, %d depth \n", (qix->version ? "new": "old"), (qix->LSB_order? "LSB": "MSB"), (int) qix->nShapes, (int) qix->nDepth); /* -------------------------------------------------------------------- */ /* Skim over the list of shapes, printing all the vertices. */ /* -------------------------------------------------------------------- */ pos = ftell (qix->fp); j = 0; while( pos && j < 20) { j ++; /* fprintf (stderr,"box %d, at %d pos \n", j, (int) ftell(qix)); */ node = readTreeNode (qix); if (node ) { fprintf (stdout,"shapes %d, node %d, %f,%f,%f,%f \n",(int) node->numshapes,node->numsubnodes,node->rect.minx, node->rect.miny, node->rect.maxx, node->rect.maxy); } else { pos = 0; } } printf ("read entire file now at quad box rec %d file pos %ld\n", j, ftell (qix->fp)); j = qix->nShapes; msSHPDiskTreeClose (qix); if( argc >= 5 ) { rect.minx = atof (argv[2]); rect.miny = atof (argv[3]); rect.maxx = atof (argv[4]); rect.maxy = atof (argv[5]); } else { printf ("using last read box as a search \n"); rect.minx = node->rect.minx; rect.miny = node->rect.miny; rect.maxx = node->rect.maxx; rect.maxy = node->rect.maxy; } bitmap = msSearchDiskTree( argv[1], rect, 0 /* no debug*/ ); if ( bitmap ) { printf ("result of rectangle search was \n"); for ( i=0; i<j; i++) { if ( msGetBit(bitmap,i) ) { printf(" %d,",i); } } } printf("\n"); return(0); }
int main( int argc, char ** argv ) { SHPHandle hSHP; DBFHandle hDBF; SHPTreeHandle qix; int i; char *myfile = NULL; treeNodeObj *node; #ifdef MAPSERVER shapeObj shape; lineObj line[3]; pointObj pts[6]; #else SHPObject *shape; double X[6], Y[6]; #endif int result; char mBigEndian; int this_rec, factor; /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ if( argc <= 2 ) { printf( "shptreevis shapefile new_shapefile \n" ); exit( 1 ); } i = 1; if( *((unsigned char *) &i) == 1 ) mBigEndian = 0; else mBigEndian = 1; qix = msSHPDiskTreeOpen (AddFileSuffix(argv[1],".qix"), 0 /* no debug*/); if( qix == NULL ) { printf("unable to open index file %s \n", argv[1]); exit(-1); } /* -------------------------------------------------------------------- */ /* Open the passed shapefile. */ /* -------------------------------------------------------------------- */ myfile = AddFileSuffix(argv[2],".shp"); #ifdef MAPSERVER hSHP = msSHPCreate ( myfile, SHPT_POLYGON ); hDBF = msDBFCreate ( AddFileSuffix(argv[2],".dbf") ); #else hSHP = SHPCreate ( myfile, SHPT_POLYGON ); hDBF = DBFCreate ( AddFileSuffix(argv[2],".dbf") ); #endif if ( (!hSHP) || (!hDBF) ) { printf ("create error for %s ... exiting \n", myfile); exit (-1); } /* add fields to dbf */ #ifdef MAPSERVER msDBFAddField ( hDBF, "ITEMS", FTInteger, 15,0 ); msDBFAddField ( hDBF, "SUBNODES", FTInteger, 15,0 ); msDBFAddField ( hDBF, "FACTOR", FTInteger, 15,0 ); #else DBFAddField ( hDBF, "ITEMS", FTInteger, 15,0 ); DBFAddField ( hDBF, "SUBNODES", FTInteger, 15,0 ); DBFAddField ( hDBF, "FACTOR", FTInteger, 15,0 ); #endif #ifndef MAPSERVER SHPClose ( hSHP ); hSHP = SHPOpen ( myfile, "r+b" ); DBFClose (hDBF); hDBF = DBFOpen ( myfile, "r+b"); #endif printf ("This %s %s index supports a shapefile with %d shapes, %d depth \n", (qix->version ? "new": "old"), (qix->LSB_order? "LSB": "MSB"), (int) qix->nShapes, (int) qix->nDepth); /* -------------------------------------------------------------------- */ /* Skim over the list of shapes, printing all the vertices. */ /* -------------------------------------------------------------------- */ while( 1 ) { node = readTreeNode (qix); if (node ) { this_rec = hDBF->nRecords; #ifdef MAPSERVER msDBFWriteIntegerAttribute( hDBF, this_rec, 0, node->numshapes); msDBFWriteIntegerAttribute( hDBF, this_rec, 1, node->numsubnodes); #else DBFWriteIntegerAttribute( hDBF, this_rec, 0, node->numshapes); DBFWriteIntegerAttribute( hDBF, this_rec, 1, node->numsubnodes); #endif factor = node->numshapes + node->numsubnodes; #ifdef MAPSERVER shape.numlines = 1; shape.type = SHPT_POLYGON; pts[0].x = node->rect.minx; pts[0].y = node->rect.miny; pts[1].x = node->rect.maxx; pts[1].y = node->rect.miny; pts[2].x = node->rect.maxx; pts[2].y = node->rect.maxy; pts[3].x = node->rect.minx; pts[3].y = node->rect.maxy; pts[4].x = node->rect.minx; pts[4].y = node->rect.miny; line[0].numpoints = 5; line[0].point = &pts[0]; shape.line = &line[0]; shape.bounds = node->rect; result = msSHPWriteShape ( hSHP, &shape ); if ( result < 0 ) { printf ("unable to write shape \n"); exit (0); } #else X[0] = node->rect.minx; X[1] = node->rect.maxx; X[2] = node->rect.maxx; X[3] = node->rect.minx; X[4] = node->rect.minx; Y[0] = node->rect.miny; Y[1] = node->rect.miny; Y[2] = node->rect.maxy; Y[3] = node->rect.maxy; Y[4] = node->rect.miny; shape = SHPCreateSimpleObject( SHPT_POLYGON, 5, X, Y, NULL); SHPWriteObject(hSHP, -1, shape); SHPDestroyObject ( shape ); #endif } else break; } #ifdef MAPSERVER msSHPClose( hSHP ); msDBFClose( hDBF ); #else SHPClose( hSHP ); DBFClose( hDBF ); #endif msSHPDiskTreeClose (qix); return(0); }