本文主要是介绍[GIS算法] DEM相关处理 - 窗口分析(以平均值为例)| 裁剪 - C语言实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include<stdio.h>
#include<stdlib.h>typedef struct{double x0,y0; //左下角点的坐标值int dx,dy; //栅格单元大小int ycount,xcount; //行列号double **M; //矩阵
}DEM;DEM* CreateDEM(int xcount,int ycount); //创建DEM
int InitDEM(DEM *pDEM); //初始化DEM
void PrintDEM(DEM dem); //输出DEM* SubDEM(DEM dem, int row, int col, int nrow, int ncol); //裁剪
DEM* WindowAnalysis_Mean(DEM dem, int size); //窗口分析/*测试数据
5 5
1 3 5 2 0
4 3 8 3 4
8 5 0 6 3
2 1 3 5 6
9 7 2 4 1
*/
int main() {DEM *pDEM, *pMean;int xcount,ycount;printf("输入行列数:\n>>> ");scanf("%d%d", &xcount, &ycount);pDEM = CreateDEM(xcount, ycount); //创建DEMInitDEM(pDEM); //初始化PrintDEM(*pDEM); //输出printf("\n");pMean = WindowAnalysis_Mean(*pDEM, 3); //3x3窗口分析求平均值PrintDEM(*pMean);return 0;
}DEM* CreateDEM(int xcount, int ycount) {DEM *p;int i;p = (DEM *)malloc(sizeof(DEM)); if (!p) exit(0);p->xcount=xcount;p->ycount=ycount;p->M = (double **)malloc(sizeof(double *)*xcount); if (!p->M) exit(0);for (i=0; i<xcount; i++) {p->M[i] = (double *)malloc(sizeof(double)*ycount);if (!p->M[i]) exit(0);}return p;
}
int InitDEM(DEM *pDEM) {int i,j;printf("以矩阵的形式输入DEM的值:\n");for (i=0; i<pDEM->xcount; i++) {for (j=0; j<pDEM->ycount; j++) {scanf("%lf", &pDEM->M[i][j]);}}return 1;
}
DEM* SubDEM(DEM dem, int row, int col, int nrow, int ncol) {DEM *sub;int i,j;if (row+nrow>dem.xcount || col+ncol>dem.ycount || row>dem.xcount || col>dem.ycount) return NULL;sub = CreateDEM(nrow, ncol);for (i=0; i<nrow; i++) {for (j=0; j<ncol; j++) {sub->M[i][j] = dem.M[i+row][j+col];}}return sub;
}
void PrintDEM(DEM dem) {int i,j;for (i=0; i<dem.xcount; i++) {for (j=0; j<dem.ycount; j++) {printf("%lf\t", dem.M[i][j]);}printf("\n");}
}
DEM* WindowAnalysis_Mean(DEM dem, int size) {// size:窗口大小// 例如:3X3的窗口 size=3DEM *mean;int i,j;int x,y;int step;int x_step, y_step;int flag;double sum;mean = CreateDEM(dem.xcount, dem.ycount);for (i=0; i<mean->xcount; i++) {for (j=0; j<mean->ycount; j++) {sum = 0; //窗口的总和step = size/2; //位移量flag = 0; //是否越界// 计算窗口的sumfor (x_step=-step; x_step<=step && !flag; x_step++) {for (y_step=-step; y_step<=step && !flag; y_step++) {x = i+x_step;y = j+y_step;if (x<0 || y<0 || x>=mean->xcount || y>=mean->ycount) { //越界flag = 1; //越界} else { //没有越界sum += dem.M[x][y];}}}//赋值if (flag) { //越界了mean->M[i][j] = dem.M[i][j];} else { //没有越界mean->M[i][j] = sum/(size*size);}}}return mean;
}
这篇关于[GIS算法] DEM相关处理 - 窗口分析(以平均值为例)| 裁剪 - C语言实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!