Linux C处理Arcmap导出的DEM栅格文件data.asc

  • 关于栅格数据文件

    读者可用自己的

    作者在百度云网盘分享了一个

    dataDEM.asc

    http://pan.baidu.com/s/1o61C7W2

    Linux C处理Arcmap导出的DEM栅格文件data.asc
    Linux C处理Arcmap导出的DEM栅格文件data.asc
  • /**

     *【定义栅格数据文件头结构】

     */

    struct GridFileHead{

    int ncols;

    int nrows;

    double xllcorner;

    double yllcorner;

    double cellsize;

    double NODATA_value;

    };

    struct GridFileHead GridHead;

    Linux C处理Arcmap导出的DEM栅格文件data.asc
  • /**

     *【读文件头】

     */

    void readGridHead(char* lpFileName){

    int hFile;

    char szBuffer[256]={0};

    int nBytesRead;

    char* szTemp;

    hFile = open(lpFileName,O_RDONLY);

    nBytesRead = read(hFile,szBuffer,sizeof(szBuffer));

    //ncols

    szTemp = strstr(szBuffer,szncols);

    GridHead.ncols = atoi(szTemp+strlen(szncols));

    //nrows

    szTemp = strstr(szBuffer,sznrows);

    GridHead.nrows = atoi(szTemp+strlen(sznrows));

    //xllcorner

    szTemp = strstr(szBuffer,szxllcorner);

    GridHead.xllcorner = atof(szTemp+strlen(szxllcorner));

    //yllcorner

    szTemp = strstr(szBuffer,szyllcorner);

    GridHead.yllcorner = atof(szTemp+strlen(szyllcorner));

    //cellsize

    szTemp = strstr(szBuffer,szcellsize);

    GridHead.cellsize = atof(szTemp+strlen(szcellsize));

    //NODATA_value

    szTemp = strstr(szBuffer,szNODATA_value);

    GridHead.NODATA_value = atof(szTemp+strlen(szNODATA_value));

    close(hFile);

    }

    Linux C处理Arcmap导出的DEM栅格文件data.asc
  • /**

     *【读栅格数据】

     */

    void readGridFileToDoubleArray2D(char* lpFileName){

    int hFile;

    char szBuffer[180]={0};

    struct stat st;

    char *lpFileMap,hFileMap;

    char *lpOffset;

    char *lpSplit;

    char split=0x20;

    double *gridInt=0;

    hFile = open(lpFileName,O_RDONLY);

    fstat(hFile,&st);

    lpFileMap=mmap(NULL,st.st_size,PROT_READ,MAP_PRIVATE,hFile,0);

    if(!index(lpFileMap,0x20)){

    split=9;

    }

    lpOffset = strstr(lpFileMap,szNODATA_value);

    lpOffset = index(lpOffset,0x0A);

    lpOffset++;

    int i,j;

    for(i=0; i<GridHead.nrows; i++){

    for(j=0;j<GridHead.ncols-1;j++){

    arrDouble2D[i][j]=atof(lpOffset);

    lpSplit=index(lpOffset,split);

    lpOffset=lpSplit+1;

    }

    arrDouble2D[i][GridHead.ncols-1]=atof(lpOffset);

    lpSplit=index(lpOffset,0x0A);

    lpOffset++;

    }

    munmap(lpFileMap,st.st_size);

    close(hFile);

    }

    Linux C处理Arcmap导出的DEM栅格文件data.asc
  • /**

     *【写二维数组到文件】

     */

    void writeDoubleArray2DtoGridFile(char* lpFileName, double **arrDouble2D){

    char szFileName[255]={0};

    char szBuffer[81920]={0};

    char szTmp[64]={0};

    memset(szFileName,0,sizeof(szFileName));

    strcat(szFileName,”_out.asc”);

    int hOut;

    hOut = open(szFileName,O_RDWR|O_CREAT);

    vsprf(szBuffer,”ncols %d\r\nnrows %d\r\nxllcorner %f\r\nyllcorner %f\r\ncellsize %f\r\nNODATA_value %f\r\n”,

    GridHead.ncols,

    GridHead.nrows,

    GridHead.xllcorner,

    GridHead.yllcorner,

    GridHead.cellsize,

    GridHead.NODATA_value

    );

    write(hOut,szBuffer,strlen(szBuffer));

    memset(szBuffer,0,sizeof(szBuffer));

    int i,j;

    for(i=0;i<GridHead.nrows;i++){

    for(j=0;j<GridHead.ncols;j++){

    memset(szTmp,0,sizeof(szTmp));

    vsprf(szTmp,”%f “,arrDouble2D[i][j]);

    strcat(szBuffer,szTmp);

    }

    strcat(szBuffer,”\r\n”);

    write(hOut,szBuffer,strlen(szBuffer));

    memset(szBuffer,0,sizeof(szBuffer));

    }

    close(hOut);

    }

    Linux C处理Arcmap导出的DEM栅格文件data.asc
  • /**

     *【格式化输出的子程序】

     *【在上面第5步中用到】

     */

    int vsprf(char *str,char *format,…){

    va_list ap;

    int count;

    va_start(ap,format);

    count=vsprintf(str,format,ap);

    va_end(ap);

    return count;

    }

    Linux C处理Arcmap导出的DEM栅格文件data.asc
  • 【编写主程序】

    #include <stdio.h>

    #include <stdlib.h>

    #include <stdarg.h>

    #include <fcntl.h>

    #include <unistd.h>

    #include <string.h>

    #include <sys/types.h>

    #include <sys/stat.h>

    #include <sys/mman.h>

    #include <math.h>

    /**

     *

     */

    struct GridFileHead{

    int ncols;

    int nrows;

    double xllcorner;

    double yllcorner;

    double cellsize;

    double NODATA_value;

    };

    struct GridFileHead GridHead;

    /**

     *

     */

    char *szncols = “ncols”;

    char *sznrows = “nrows”;

    char *szxllcorner = “xllcorner”;

    char *szyllcorner = “yllcorner”;

    char *szcellsize = “cellsize”;

    char *szNODATA_value = “NODATA_value”;

    double **arrDouble2D=0;

    /**

     *

     */

    void main(int argc, char *argv[]) {

    /**

    *read grid file head

    */

    char *strFileInput=argv[1];

    readGridHead(strFileInput);

    /**

    *allocate memory for array

    */

    arrDouble2D=(int **)calloc(GridHead.nrows,sizeof(double *));

    int i;

    for(i=0;i<GridHead.nrows;i++){

    *(arrDouble2D+i)=(int *)calloc(GridHead.ncols, sizeof(double));

    }

    readGridFileToDoubleArray2D(strFileInput);

    /**

    *

    */

    writeDoubleArray2DtoGridFile(“dataout.asc”,arrDouble2D);

    /**

    *free memory

    */

    for(i=0;i<GridHead.nrows;i++){

    free(*(arrDouble2D+i));

    }

    free(arrDouble2D);

    }

    Linux C处理Arcmap导出的DEM栅格文件data.asc
    Linux C处理Arcmap导出的DEM栅格文件data.asc
  • 8

    【测试运行,结果如下】

    Linux C处理Arcmap导出的DEM栅格文件data.asc
    END
  • 转载自:https://jingyan.baidu.com/article/0f5fb099f75f546d8334ea92.html

    You may also like...