📄 shapes_io.cpp
字号:
///////////////////////////////////////////////////////////
// //
// SAGA //
// //
// System for Automated Geoscientific Analyses //
// //
// Application Programming Interface //
// //
// Library: SAGA_API //
// //
//-------------------------------------------------------//
// //
// shapes_io.cpp //
// //
// Copyright (C) 2005 by Olaf Conrad //
// //
//-------------------------------------------------------//
// //
// This file is part of 'SAGA - System for Automated //
// Geoscientific Analyses'. //
// //
// This library is free software; you can redistribute //
// it and/or modify it under the terms of the GNU Lesser //
// General Public License as published by the Free //
// Software Foundation, version 2.1 of the License. //
// //
// This library is distributed in the hope that it will //
// be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A //
// PARTICULAR PURPOSE. See the GNU Lesser General Public //
// License for more details. //
// //
// You should have received a copy of the GNU Lesser //
// General Public License along with this program; if //
// not, write to the Free Software Foundation, Inc., //
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, //
// USA. //
// //
//-------------------------------------------------------//
// //
// contact: Olaf Conrad //
// Institute of Geography //
// University of Goettingen //
// Goldschmidtstr. 5 //
// 37077 Goettingen //
// Germany //
// //
// e-mail: oconrad@saga-gis.org //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#include "shapes.h"
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Shapes::_Load_ESRI(const SG_Char *File_Name)
{
bool bError;
char buf_Header[100];
int Type_File, Type_Shape,
FileCode, FileLength, Version,
RecordNumber, ContentLength,
iShape, iPart, nParts, iPoint, nPoints,
buf_nParts, *buf_nPoints;
TSG_Point dPoint;
TSG_Rect dRect;
CSG_String fName;
CSG_File Stream;
CSG_Shape *pShape;
//-----------------------------------------------------
// Load Attributes...
fName = SG_File_Make_Path(NULL, File_Name, SG_T("dbf"));
if( !m_Table._Create(fName, SG_T('\t')) || m_Table.Get_Record_Count() == 0 )
{
return( false );
}
//-----------------------------------------------------
// Open Shapes File...
SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), LNG("[MSG] Load shapes"), File_Name), true);
fName = SG_File_Make_Path(NULL, File_Name, SG_T("shp"));
if( !Stream.Open(fName, SG_FILE_R, true) )
{
SG_UI_Msg_Add(LNG("[MSG] failed"), false);
SG_UI_Msg_Add_Error(LNG("[ERR] Shape file could not be opened."));
return( false );
}
//-----------------------------------------------------
// Load File-Header (100-Bytes)...
Stream.Read(buf_Header, sizeof(char), 100);
FileCode = SG_Mem_Get_Int (buf_Header + 0, true ); // Byte 0 -> File Code 9994 Integer Big...
// ... // Byte 4-20 -> Unused 0 Integer Big...
FileLength = SG_Mem_Get_Int (buf_Header + 24, true ); // Byte 24 -> File Length File Length Integer Big...
Version = SG_Mem_Get_Int (buf_Header + 28, false); // Byte 28 -> Version 1000 Integer Little...
Type_File = SG_Mem_Get_Int (buf_Header + 32, false); // Byte 32 -> Shape m_Type Shape m_Type Integer Little...
dRect.xMin = SG_Mem_Get_Double (buf_Header + 36, false); // Byte 36 -> Bounding Box Xmin Double Little...
dRect.yMin = SG_Mem_Get_Double (buf_Header + 44, false); // Byte 44 -> Bounding Box Ymin Double Little...
dRect.xMax = SG_Mem_Get_Double (buf_Header + 52, false); // Byte 52 -> Bounding Box Xmax Double Little...
dRect.yMax = SG_Mem_Get_Double (buf_Header + 60, false); // Byte 60 -> Bounding Box Ymax Double Little...
// ... // Byte 68* -> Bounding Box Zmin Double Little...
// ... // Byte 76* -> Bounding Box Zmax Double Little...
// ... // Byte 84* -> Bounding Box Mmin Double Little...
// ... // Byte 92* -> Bounding Box Mmax Double Little...
m_Extent.Assign(dRect);
switch( Type_File )
{
case 1: m_Type = SHAPE_TYPE_Point; break;
case 8: m_Type = SHAPE_TYPE_Points; break;
case 3: m_Type = SHAPE_TYPE_Line; break;
case 5: m_Type = SHAPE_TYPE_Polygon; break;
default: m_Type = SHAPE_TYPE_Undefined; break; // unsupported...
}
if( Stream.is_EOF() || FileCode != 9994 || Version != 1000 || m_Type == SHAPE_TYPE_Undefined )
{
SG_UI_Msg_Add(LNG("[MSG] failed"), false);
SG_UI_Msg_Add_Error(LNG("[ERR] Shape file header is invalid."));
return( false );
}
//-------------------------------------------------
// Load Shapes...
m_nShapes = m_Table.Get_Record_Count();
m_Shapes = (CSG_Shape **)SG_Malloc(m_nShapes * sizeof(CSG_Shape *));
buf_nParts = 0;
buf_nPoints = NULL;
for(iShape=0, bError=false; iShape<m_nShapes && SG_UI_Process_Set_Progress(iShape, m_nShapes); iShape++)
{
switch( m_Type )
{
default:
case SHAPE_TYPE_Point: m_Shapes[iShape] = pShape = new CSG_Shape_Point (this, m_Table.Get_Record(iShape)); break;
case SHAPE_TYPE_Points: m_Shapes[iShape] = pShape = new CSG_Shape_Points (this, m_Table.Get_Record(iShape)); break;
case SHAPE_TYPE_Line: m_Shapes[iShape] = pShape = new CSG_Shape_Line (this, m_Table.Get_Record(iShape)); break;
case SHAPE_TYPE_Polygon: m_Shapes[iShape] = pShape = new CSG_Shape_Polygon (this, m_Table.Get_Record(iShape)); break;
}
RecordNumber = Stream.Read_Int(true);
ContentLength = Stream.Read_Int(true);
Stream.Read(&Type_Shape, sizeof(int));
if( Type_Shape != Type_File || Stream.is_EOF() )
{
bError = true;
}
else
{
switch( Type_Shape )
{
//---------------------------------------------
case 1: // Point...
Stream.Read(&dPoint , sizeof(TSG_Point));
pShape->Add_Point(dPoint.x, dPoint.y);
break;
//---------------------------------------------
case 8: // Multipoint...
Stream.Read(&dRect , sizeof(TSG_Rect));
Stream.Read(&nPoints, sizeof(int));
for(iPoint=0; iPoint<nPoints; iPoint++)
{
Stream.Read(&dPoint, sizeof(TSG_Point));
pShape->Add_Point(dPoint.x, dPoint.y);
}
break;
//---------------------------------------------
case 3: case 5: // Line, Polygon...
Stream.Read(&dRect , sizeof(TSG_Rect));
Stream.Read(&nParts , sizeof(int));
Stream.Read(&nPoints, sizeof(int));
//-----------------------------------------
if( buf_nParts <= nParts )
{
buf_nParts = nParts + 1;
buf_nPoints = (int *)SG_Realloc(buf_nPoints, buf_nParts * sizeof(int));
}
for(iPart=0; iPart<nParts; iPart++)
{
Stream.Read(buf_nPoints + iPart, sizeof(int));
}
buf_nPoints[nParts] = nPoints;
//-----------------------------------------
for(iPoint=0, iPart=0; iPoint<nPoints; iPoint++)
{
if( iPoint >= buf_nPoints[iPart + 1] && iPart < nParts - 1 )
{
iPart++;
}
Stream.Read(&dPoint, sizeof(TSG_Point));
pShape->Add_Point(dPoint.x, dPoint.y, iPart);
}
break;
}
}
}
//-----------------------------------------------------
// Clean up...
if( buf_nPoints )
{
SG_Free(buf_nPoints);
}
if( bError )
{
SG_UI_Msg_Add(LNG("[MSG] failed"), false);
SG_UI_Msg_Add_Error(LNG("[ERR] Shape file is corrupted."));
}
else
{
SG_UI_Msg_Add(LNG("[MSG] okay"), false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -