分类:
2008-11-13 11:29:52
根据网友的创意,将部分代码改善了一下 至于polygon等格式也将送到网上
#include
#include
#include
//#include
//#include "shapefil.h"
typedef unsigned char uchar;
typedef unsigned long ulong;
FILE* shpFile_Pointer;
using namespace std;
/*
* change the big order into little order
*/
void swap(int length, void * word)
{
uchar temp;
for(int i=0; i
temp = ((uchar*)word)[i];
((uchar*)word)[i] = ((uchar*)word)[length-i-1];
((uchar*)word)[length-i-1] = temp;
}
}
ulong toInt(ulong code)
{
ulong value;
value = ((((ulong)(code)&(ulong)0x000000ffUL)<<24)|\
(((ulong)(code)&(ulong)0x0000ff00UL)<<8)|\
(((ulong)(code)&(ulong)0x00ff0000UL)>>8)|\
(((ulong)(code)&(ulong)0xff000000UL)>>24));
return value;
}
void showPoint()
{
int RecordNumber;
int ContentLength;
int num = 0;
while(fread(&RecordNumber, sizeof(int), 1, shpFile_Pointer))
{
++num;
cout<<"Record "<
cout<<"Content length: "<
int sType;
double x;
double y;
fread(&sType, sizeof(int),1,shpFile_Pointer);
fread(&x, sizeof(double),1,shpFile_Pointer);
fread(&y, sizeof(double),1,shpFile_Pointer);
cout<<"("<
}
}
void showPolyLine()
{
int RecordNumber;
int ContentLength;
int num = 0;
while(fread(&RecordNumber, sizeof(int), 1, shpFile_Pointer))
{
++num;
cout<<"Record "<
cout<<"Content length: "<
int sType;
double Box[4]; //The Bounding Box for the PolyLine stored in the order Xmin, Ymin, Xmax,Ymax
int NumParts; //记录了当前线性目标所包含的子线段的个数
int NumPoints; //记录了当前当前线性目标得坐标点总数
int *parts; //记录了每个子线段的第一个坐标点在坐标数组points中的位置
fread(&sType, sizeof(int),1,shpFile_Pointer);
for(int i=0; i<4; ++i)
{
fread(Box+i,sizeof(double),1,shpFile_Pointer);
}
fread(&NumParts,sizeof(int),1,shpFile_Pointer); //记录当前子线段的个数
fread(&NumPoints,sizeof(int),1,shpFile_Pointer);//记录当前points的个数
cout<<"当前子线段的个数: "<
for(int i=0; i
fread(parts+i,sizeof(int),1,shpFile_Pointer);
}
int numOfCurrentLine;
for(int i=0; i
if(i!=(NumParts-1))
numOfCurrentLine = parts[i+1]-parts[i];
else
numOfCurrentLine = NumPoints-parts[i];
//cout<<"当前线段包含的点的个数: "<
double * pointsY = new double[numOfCurrentLine];
for(int j=0; j
fread(pointsX+j, sizeof(double),1,shpFile_Pointer);
fread(pointsY+j, sizeof(double),1,shpFile_Pointer);
cout<< pointsX[j]<< " "<
delete[] pointsX;
delete[] pointsY;
}
delete[] parts;
}
}
void switchType(int num)
{
switch(num)
{
case 0: cout<<"NULL Shape"<
case 1: cout<<"Point"<
case 3: cout<<"PolyLine"<
case 5: cout<<"Polygon"<
case 8:cout<<"MultiPoint"<
case 11:cout<<"PointZ"<
case 13:cout<<"PolyLineZ"<
case 15:cout<<"PolygonZ"<
case 18:cout<<"MultiPointZ"<
case 21:cout<<"PointM"<
case 23:cout<<"PolyLineM"<
case 25:cout<<"PolygonM"<
case 28:cout<<"MultiPointM"<
case 31:cout<<"MultiPatch"<
default:
break;
}
}
int main()
{
const char *path = "./roads.shp";
//SHPHandle hd;
//FILE* shpFile_Pointer;
if ((shpFile_Pointer = fopen(path,"rb"))==NULL)
{
std::cout<<"Reading fail"<
else
{
std::cout<<"Read success"<
int FileCode;
int Unused;
int FileLength;
int Version;
int ShapeType;
double Xmin;
double Ymin;
double Xmax;
double Ymax;
double Zmin;
double Zmax;
double Mmin;
double Mmax;
fread(&FileCode, sizeof(int), 1, shpFile_Pointer);
FileCode = toInt(FileCode);
cout<<"File Code: "<
{
fread(&Unused, sizeof(int), 1, shpFile_Pointer);
}
fread(&FileLength, sizeof(int), 1, shpFile_Pointer);
FileLength = toInt(FileLength);
cout<<"File Length: "<
fread(&Version, sizeof(int), 1, shpFile_Pointer);
cout<<"Version: "<
fread(&ShapeType, sizeof(int), 1, shpFile_Pointer);
/*cout<<"Shape Type ";
switchType(ShapeType);*/
fread(&Xmin, sizeof(double), 1, shpFile_Pointer);
fread(&Ymin, sizeof(double), 1, shpFile_Pointer);
fread(&Xmax, sizeof(double), 1, shpFile_Pointer);
fread(&Ymax, sizeof(double), 1, shpFile_Pointer);
fread(&Zmin, sizeof(double), 1, shpFile_Pointer);
fread(&Zmax, sizeof(double), 1, shpFile_Pointer);
cout <<"X range: "<< Xmin <<" "<
fread(&Mmax, sizeof(double), 1, shpFile_Pointer);
cout <<"M range: "<< Mmin <<" "<
/*int RecordNumber;
int ContentLength;
int num = 0;
while(fread(&RecordNumber, sizeof(int), 1, shpFile_Pointer))
{
++num;
cout<<"Record "<
cout<<"Content length: "<
double x;
double y;
fread(&sType, sizeof(int),1,shpFile_Pointer);
fread(&x, sizeof(double),1,shpFile_Pointer);
fread(&y, sizeof(double),1,shpFile_Pointer);
cout<<"("<
switchType(sType);
}*/
cout<<"Shape Type ";
switchType(ShapeType);
//showPoint();
fclose(shpFile_Pointer);
return 0;
}