📄 shpapi.cpp
字号:
// shpapi.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "shpgeo.h"
#include <stdarg.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
SHPHandle old_SHP, new_SHP;
int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart, j;
double *padVertices, adBounds[4];
const char *pszPlus;
SHPObject *psCShape;
FILE *ifp = NULL;
int idfld, nflds;
char kv[257] = "";
char idfldName[120] = "";
char fldName[120] = "";
char shpFileName[120] = "";
char dbfFileName[120] = "";
char prjFileName[120] = "";
char parg[80];
double apeture[4];
int inarg, outarg;
/* for testing only
char *in_args[] = { "init=nad83:1002", "units=us-ft" };
char *out_args[] = { "proj=utm", "zone=16", "units=m" };
*/
/* static char *parms[] = {
"proj=poly",
"ellps=clrk66",
"lon_0=90W",
"no_defs"
};
PJ *ref = NULL;
ref = pj_init(sizeof(parms)/sizeof(char *), parms);
*/
char *in_args[16];
char *out_args[16];
int in_argc = 0 , out_argc = 0, outf_arg;
char *arglst;
PJ *orig_prj, *new_prj;
va_list myargs, moargs;
strcpy( prjFileName, "E:\\地图\\中国地图(shp)\\边界.shp" );
old_SHP = SHPOpen( prjFileName, "rb" );
if( old_SHP == NULL )
{
}
outf_arg = 2;
inarg = 0;
outarg = 0;
/* if shapefile has a prj component then use that
else try for a file then read args as list */
/* ifp = fopen( asFileName ( prjFileName, "prj" ),"r");
i = 0;
if ( ifp )
{
while( fscanf( ifp, "%s", parg) != EOF )
{
in_args[i] = (char *)malloc ( strlen(parg));
strcpy ( in_args[i], parg);
i++;
}
in_argc = i;
fclose (ifp);
}
i = 0;
ifp = fopen( "E:\\地图\\中国地图(shp投影)\\城镇.prj","r");
if ( ifp )
{
while( fscanf( ifp, "%s", parg) != EOF )
{
out_args[i] = (char *)malloc ( strlen(parg));
strcpy ( out_args[i], parg);
i++;
}
out_argc = i;
fclose (ifp);
}
*/
in_argc = out_argc = 1;
in_args[0] = (char *)malloc ( 256);
out_args[0] = (char *)malloc ( 256);
strcpy(in_args[0], "+proj=longlat +ellps=clrk80 no_defs ");
strcpy(out_args[0], "+proj=merc +lat_ts=0 +lon_0=0 +k=1.000000 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m");
/* static char *parms[] = {
"proj=poly",
"ellps=clrk66",
"lon_0=90W",
"no_defs"
};
PJ *ref = NULL;
ref = pj_init(sizeof(parms)/sizeof(char *), parms);
*/
orig_prj = SHPSetProjectionEx(in_args[0]);
new_prj = SHPSetProjectionEx (out_args[0] );
// orig_prj = SHPSetProjection ( in_argc, in_args );
// new_prj = SHPSetProjection ( out_argc, out_args );
// orig_prj = SHPSetProjection ( in_argc, in_args );
// new_prj = SHPSetProjection ( out_argc, out_args );
if ( !(( (!in_argc) || orig_prj) && ( (!out_argc) || new_prj) ))
{
fprintf (stderr, "unable to process projection, exiting...\n");
}
SHPGetInfo( old_SHP, &nEntities, &nShapeType, NULL, NULL);
new_SHP = SHPCreate ( "D:\\adc.shp", nShapeType );
if( new_SHP == NULL)
{
printf( "Unable to create new files:\n");
}
for( i = 0; i < nEntities; i++ )
{
int j;
psCShape = SHPReadObject ( old_SHP, i );
double x = psCShape->padfX[0];
double y = psCShape->padfY[0];
SHPProject (psCShape, orig_prj, new_prj );
x = psCShape->padfX[0];
y = psCShape->padfY[0];
SHPWriteObject ( new_SHP, -1, psCShape );
SHPDestroyObject ( psCShape );
}
SHPFreeProjection ( orig_prj );
SHPFreeProjection ( new_prj );
/* store projection params into prj file */
ifp = fopen( "D:\\abc.prj","w");
if ( ifp ) {
if ( out_argc == 0 )
{
fprintf( ifp, "proj=geographic\n" );
}
else
{
for ( i = 0; i < out_argc; i++ )
fprintf( ifp, "%s\n", out_args[i]);
}
fclose (ifp);
}
SHPClose( old_SHP );
SHPClose( new_SHP );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -