📄 shputils.c
字号:
/* -------------------------------------------------------------------- */ if (iselect) if (selectrec() == 0) goto SKIP_RECORD; /** SKIP RECORD **//* -------------------------------------------------------------------- *//* Read a Shape record *//* -------------------------------------------------------------------- */ psCShape = SHPReadObject( hSHP, iRecord );/* -------------------------------------------------------------------- *//* Clip coordinates of shapes if needed. *//* -------------------------------------------------------------------- */ if (iclip) if (clip_boundary() == 0) goto SKIP_RECORD; /** SKIP RECORD **//* -------------------------------------------------------------------- *//* Read a DBF record and copy each field. *//* -------------------------------------------------------------------- */ for( i = 0; i < DBFGetFieldCount(hDBF); i++ ) {/* -------------------------------------------------------------------- *//* Store the record according to the type and formatting *//* information implicit in the DBF field description. *//* -------------------------------------------------------------------- */ if (pt[i] > -1) /* if the current field exists in output file */ { switch( DBFGetFieldInfo( hDBF, i, NULL, &iWidth, &iDecimals ) ) { case FTString: DBFWriteStringAttribute(hDBFappend, jRecord, pt[i], (DBFReadStringAttribute( hDBF, iRecord, i )) ); break; case FTInteger: DBFWriteIntegerAttribute(hDBFappend, jRecord, pt[i], (DBFReadIntegerAttribute( hDBF, iRecord, i )) ); break; case FTDouble: DBFWriteDoubleAttribute(hDBFappend, jRecord, pt[i], (DBFReadDoubleAttribute( hDBF, iRecord, i )) ); break; } } } jRecord++;/* -------------------------------------------------------------------- *//* Change FACTOR and SHIFT coordinates of shapes if needed. *//* -------------------------------------------------------------------- */ if (iunit) { for( j = 0; j < psCShape->nVertices; j++ ) { psCShape->padfX[j] = psCShape->padfX[j] * factor + xshift; psCShape->padfY[j] = psCShape->padfY[j] * factor + yshift; } } /* -------------------------------------------------------------------- *//* Write the Shape record after recomputing current extents. *//* -------------------------------------------------------------------- */ SHPComputeExtents( psCShape ); SHPWriteObject( hSHPappend, -1, psCShape ); SKIP_RECORD: SHPDestroyObject( psCShape ); j=0; }/* -------------------------------------------------------------------- *//* Print out the # of Entities and the file bounds. *//* -------------------------------------------------------------------- */ jRecord = DBFGetRecordCount( hDBFappend ); SHPGetInfo( hSHPappend, &nEntitiesAppend, &nShapeTypeAppend, adfBoundsMin, adfBoundsMax ); printf( "Output Bounds: (%lg,%lg) - (%lg,%lg) Entities: %d DBF: %d\n\n", adfBoundsMin[0], adfBoundsMin[1], adfBoundsMax[0], adfBoundsMax[1], nEntitiesAppend, jRecord );/* -------------------------------------------------------------------- *//* Close the both shapefiles. *//* -------------------------------------------------------------------- */ SHPClose( hSHP ); SHPClose( hSHPappend ); DBFClose( hDBF ); DBFClose( hDBFappend ); if (nEntitiesAppend == 0) { puts("Remove the output files."); setext(outfile, "dbf"); remove(outfile); setext(outfile, "shp"); remove(outfile); setext(outfile, "shx"); remove(outfile); } return( 0 );}/************************************************************************//* openfiles() *//************************************************************************/void openfiles() {/* -------------------------------------------------------------------- *//* Open the DBF file. *//* -------------------------------------------------------------------- */ setext(infile, "dbf"); hDBF = DBFOpen( infile, "rb" ); if( hDBF == NULL ) { printf( "ERROR: Unable to open the input DBF:%s\n", infile ); exit( 1 ); }/* -------------------------------------------------------------------- *//* Open the append DBF file. *//* -------------------------------------------------------------------- */ if (strcmp(outfile,"")) { setext(outfile, "dbf"); hDBFappend = DBFOpen( outfile, "rb+" ); newdbf=0; if( hDBFappend == NULL ) { newdbf=1; hDBFappend = DBFCreate( outfile ); if( hDBFappend == NULL ) { printf( "ERROR: Unable to open the append DBF:%s\n", outfile ); exit( 1 ); } } }/* -------------------------------------------------------------------- *//* Open the passed shapefile. *//* -------------------------------------------------------------------- */ setext(infile, "shp"); hSHP = SHPOpen( infile, "rb" ); if( hSHP == NULL ) { printf( "ERROR: Unable to open the input shape file:%s\n", infile ); exit( 1 ); } SHPGetInfo( hSHP, &nEntities, &nShapeType, NULL, NULL );/* -------------------------------------------------------------------- *//* Open the passed append shapefile. *//* -------------------------------------------------------------------- */ if (strcmp(outfile,"")) { setext(outfile, "shp"); hSHPappend = SHPOpen( outfile, "rb+" ); if( hSHPappend == NULL ) { hSHPappend = SHPCreate( outfile, nShapeType ); if( hSHPappend == NULL ) { printf( "ERROR: Unable to open the append shape file:%s\n", outfile ); exit( 1 ); } } SHPGetInfo( hSHPappend, &nEntitiesAppend, &nShapeTypeAppend, NULL, NULL ); if (nShapeType != nShapeTypeAppend) { puts( "ERROR: Input and Append shape files are of different types."); exit( 1 ); } }}/* -------------------------------------------------------------------- *//* Change the extension. If there is any extension on the *//* filename, strip it off and add the new extension *//* -------------------------------------------------------------------- */void setext(char *pt, char *ext){int i; for( i = strlen(pt)-1; i > 0 && pt[i] != '.' && pt[i] != '/' && pt[i] != '\\'; i-- ) {} if( pt[i] == '.' ) pt[i] = '\0'; strcat(pt,"."); strcat(pt,ext);}/* -------------------------------------------------------------------- *//* Find matching fields in the append file. *//* Output file must have zero records to add any new fields. *//* -------------------------------------------------------------------- */void mergefields(){ int i,j; ti = DBFGetFieldCount( hDBF ); tj = DBFGetFieldCount( hDBFappend ); /* Create a pointer array for the max # of fields in the output file */ pt = (int *) malloc( (ti+tj+1) * sizeof(int) ); for( i = 0; i < ti; i++ ) { pt[i]= -1; /* Initial pt values to -1 */ } /* DBF must be empty before adding items */ jRecord = DBFGetRecordCount( hDBFappend ); for( i = 0; i < ti; i++ ) { iType = DBFGetFieldInfo( hDBF, i, iszTitle, &iWidth, &iDecimals ); found=FALSE; { for( j = 0; j < tj; j++ ) /* Search all field names for a match */ { jType = DBFGetFieldInfo( hDBFappend, j, jszTitle, &jWidth, &jDecimals ); if (iType == jType && (strcmp(iszTitle, jszTitle) == 0) ) { if (found || newdbf) { if (i == j) pt[i]=j; printf("Warning: Duplicate field name found (%s)\n",iszTitle); /* Duplicate field name (Try to guess the correct field by position) */ } else { pt[i]=j; found=TRUE; } } } } if (pt[i] == -1 && (! found) ) /* Try to force into an existing field */ { /* Ignore the field name, width, and decimal places */ jType = DBFGetFieldInfo( hDBFappend, j, jszTitle, &jWidth, &jDecimals ); if (iType == jType) { pt[i]=i; found=1; } } if ( (! found) && jRecord == 0) /* Add missing field to the append table */ { /* The output DBF must be is empty */ pt[i]=tj; tj++; if( DBFAddField( hDBFappend, iszTitle, iType, iWidth, iDecimals ) == -1 ) { printf( "Warning: DBFAddField(%s, TYPE:%d, WIDTH:%d DEC:%d, ITEM#:%d of %d) failed.\n", iszTitle, iType, iWidth, iDecimals, (i+1), (ti+1) ); pt[i]=-1; } } }}void findselect(){ /* Find the select field name */ iselectitem = -1; for( i = 0; i < ti && iselectitem < 0; i++ ) { iType = DBFGetFieldInfo( hDBF, i, iszTitle, &iWidth, &iDecimals ); if (strncasecmp2(iszTitle, selectitem, 0) == 0) iselectitem = i; } if (iselectitem == -1) { printf("Warning: Item not found for selection (%s)\n",selectitem); iselect = FALSE; iall = FALSE; showitems(); printf("Continued... (Selecting entire file)\n"); } /* Extract all of the select values (by field type) */ }void showitems(){char stmp[40],slow[40],shigh[40];double dtmp,dlow,dhigh,dsum,mean;long int itmp,ilow,ihigh,isum;long int maxrec;char *pt; printf("Available Items: (%d)",ti); maxrec = DBFGetRecordCount(hDBF); if (maxrec > 5000 && ! iall) { maxrec=5000; printf(" ** ESTIMATED RANGES (MEAN) For more records use \"All\""); } else { printf(" RANGES (MEAN)"); } for( i = 0; i < ti; i++ ) { switch( DBFGetFieldInfo( hDBF, i, iszTitle, &iWidth, &iDecimals ) ) { case FTString: strcpy(slow, "~"); strcpy(shigh,"\0"); printf("\n String %3d %-16s",iWidth,iszTitle); for( iRecord = 0; iRecord < maxrec; iRecord++ ) { strncpy(stmp,DBFReadStringAttribute( hDBF, iRecord, i ),39); if (strcmp(stmp,"!!") > 0) { if (strncasecmp2(stmp,slow,0) < 0) strncpy(slow, stmp,39); if (strncasecmp2(stmp,shigh,0) > 0) strncpy(shigh,stmp,39); } } pt=slow+strlen(slow)-1; while(*pt == ' ') { *pt='\0'; pt--; } pt=shigh+strlen(shigh)-1; while(*pt == ' ') { *pt='\0'; pt--; } if (strncasecmp2(slow,shigh,0) < 0) printf("%s to %s",slow,shigh); else if (strncasecmp2(slow,shigh,0) == 0) printf("= %s",slow); else printf("No Values"); break; case FTInteger: printf("\n Integer %3d %-16s",iWidth,iszTitle); ilow = 1999999999; ihigh= -1999999999; isum = 0; for( iRecord = 0; iRecord < maxrec; iRecord++ ) { itmp = DBFReadIntegerAttribute( hDBF, iRecord, i ); if (ilow > itmp) ilow = itmp; if (ihigh < itmp) ihigh = itmp; isum = isum + itmp; } mean=isum/maxrec; if (ilow < ihigh) printf("%d to %d \t(%.1f)",ilow,ihigh,mean); else if (ilow == ihigh) printf("= %d",ilow); else printf("No Values"); break; case FTDouble: printf("\n Real %3d,%d %-16s",iWidth,iDecimals,iszTitle); dlow = 999999999999999.0; dhigh= -999999999999999.0; dsum = 0; for( iRecord = 0; iRecord < maxrec; iRecord++ ) { dtmp = DBFReadDoubleAttribute( hDBF, iRecord, i ); if (dlow > dtmp) dlow = dtmp; if (dhigh < dtmp) dhigh = dtmp; dsum = dsum + dtmp; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -