⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shputils.c

📁 ESRI SHAPE文件读/写源代码。SHAPE是GIS中的重要文件格式
💻 C
📖 第 1 页 / 共 3 页
字号:
    }/* -------------------------------------------------------------------- *//*      Print out the file bounds.                                      *//* -------------------------------------------------------------------- */    iRecord = DBFGetRecordCount( hDBF );    SHPGetInfo( hSHP, NULL, NULL, adfBoundsMin, adfBoundsMax );    printf( "Input Bounds:  (%lg,%lg) - (%lg,%lg)   Entities: %d   DBF: %d\n",	    adfBoundsMin[0], adfBoundsMin[1],            adfBoundsMax[0], adfBoundsMax[1],            nEntities, iRecord );	        if (strcmp(outfile,"") == 0)    {    	ti = DBFGetFieldCount( hDBF );	showitems();	exit(0);    }         if (iclip) check_theme_bnd();        jRecord = DBFGetRecordCount( hDBFappend );    SHPGetInfo( hSHPappend, NULL, NULL, adfBoundsMin, adfBoundsMax );    if (nEntitiesAppend == 0)        puts("New Output File\n");    else        printf( "Append Bounds: (%lg,%lg)-(%lg,%lg)   Entities: %d  DBF: %d\n",                adfBoundsMin[0], adfBoundsMin[1],                adfBoundsMax[0], adfBoundsMax[1],                nEntitiesAppend, jRecord );    /* -------------------------------------------------------------------- *//*	Find matching fields in the append file or add new items.       *//* -------------------------------------------------------------------- */    mergefields();/* -------------------------------------------------------------------- *//*	Find selection field if needed.                                 *//* -------------------------------------------------------------------- */    if (iselect)    findselect();/* -------------------------------------------------------------------- *//*  Read all the records 						*//* -------------------------------------------------------------------- */    jRecord = DBFGetRecordCount( hDBFappend );    for( iRecord = 0; iRecord < nEntities; iRecord++)  /** DBFGetRecordCount(hDBF) **/    {/* -------------------------------------------------------------------- *//*      SELECT for values if needed. (Can the record be skipped.)       *//* -------------------------------------------------------------------- */        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() == 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 UNIT 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 );    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=0;        {      	    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 == 1  ||  newdbf == 1)	            {	                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=1; 	            }	        }	    }	}		if (pt[i] == -1  && found == 0)  /* 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) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -