⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 esri_e00_import.cpp

📁 这是一个GPS相关的程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	CSG_Table				*pTable;
	struct info_Table	info;

	//-----------------------------------------------------
	while( (line = E00ReadNextLine(hReadPtr)) != NULL && strncmp(line, "EOI", 3) )
	{
		strncpy(info.Name, line, 32);
		info.Name[32]	= 0;
		p	= strchr(info.Name, ' ');
		if( p != NULL )
			*p	= 0;
		p	= strchr(info.Name, '.');
		if( p == 0 )
			p	= info.Name;
		else
			p++;
		s	= p;

		strncpy(info.AI	, line + 32,  2);	info.AI[2]	= 0;
		strncpy(tmp		, line + 34,  4);	tmp[ 4]		= 0;	info.uFields	= atoi(tmp);
		strncpy(tmp		, line + 38,  4);	tmp[ 4]		= 0;	info.nFields	= atoi(tmp);
		strncpy(tmp		, line + 42,  4);	tmp[ 4]		= 0;	info.ldr		= atoi(tmp);
		strncpy(tmp		, line + 46, 11);	tmp[11]		= 0;	info.nRecords	= atol(tmp);

		info.length	= 0;
		info.Field	= (struct info_Field *)malloc(info.nFields * sizeof(struct info_Field));

		//---------------------------------------------
		for(i=0; i<info.nFields; i++)
		{
			if( (line = E00ReadNextLine(hReadPtr)) != NULL )
			{
				sscanf(line, "%16s", info.Field[i].Name);
				info.Field[i].Size	= atoi(&line[16]);
				info.Field[i].Type	= atoi(&line[34]);
			}

			//---------------------------------------------
			switch( info.Field[i].Type )
			{
			case 60:	// float / double
				info.Field[i].Size = info.Field[i].Size == 4 ? 14 : 24;
				break;

			case 50:	// short / long
				info.Field[i].Size = info.Field[i].Size == 2 ?  6 : 11;
				break;

			case 40:	// float
				info.Field[i].Size = 14;
				break;

			case 10:
				info.Field[i].Size = 8;
				break;

			default:	// string
				break;
			}

			if( i < info.uFields )
			{
				info.length			+= info.Field[i].Size;
			}

			if( i == 0 )
			{
				info.Field[i].Position	= 0;
			}
			else
			{
				info.Field[i].Position	= info.Field[i-1].Position + info.Field[i-1].Size;
			}
		}

		//---------------------------------------------
		pTable	= NULL;

		if     ( !s.CmpNoCase(SG_T("aat")) && pAAT == NULL )
		{
			pTable	= pAAT	= info_Get_Table(info);
		}
		else if( !s.CmpNoCase(SG_T("pat")) && pPAT == NULL )
		{
			pTable	= pPAT	= info_Get_Table(info);
		}
	//	else if( !s.CmpNoCase("vat") )		// value table (grid)
	//	else if( !s.CmpNoCase("bnd") )		// coverage boundaries
	//	else if( !s.CmpNoCase("tic") )		// tick marks
	//	else if( !s.CmpNoCase("sta") )		// stats on grid
	//	else if( !s.CmpNoCase("lut") )		// look-up table
	//	else if( !s.CmpNoCase("acode") )	// arc attributes
	//	else if( !s.CmpNoCase("pcode") )	// polygon attributes
	//	else								// non graphic tables
		else	// come on, let's get every table we can get...
		{
			pTable	= info_Get_Table(info);
		}

		//-------------------------------------------------
		free(info.Field);

		if( pTable )
		{
			CSG_Table_Record	*pRecord;
			CSG_Shape			*pShape;
			CSG_Shapes			*pBND, *pTIC;

			if     ( !s.CmpNoCase(SG_T("bnd")) )	// coverage boundaries
			{
				pBND	= SG_Create_Shapes(SHAPE_TYPE_Polygon, SG_T("Boundary"));
				pBND->Get_Table().Add_Field("XMIN", TABLE_FIELDTYPE_Double);
				pBND->Get_Table().Add_Field("YMIN", TABLE_FIELDTYPE_Double);
				pBND->Get_Table().Add_Field("XMAX", TABLE_FIELDTYPE_Double);
				pBND->Get_Table().Add_Field("YMAX", TABLE_FIELDTYPE_Double);
				pRecord	= pTable->Get_Record(0);
				pShape	= pBND->Add_Shape();
				pShape->Get_Record()->Set_Value(0, pRecord->asDouble(0));
				pShape->Get_Record()->Set_Value(1, pRecord->asDouble(1));
				pShape->Get_Record()->Set_Value(2, pRecord->asDouble(2));
				pShape->Get_Record()->Set_Value(3, pRecord->asDouble(3));
				pShape->Add_Point(pRecord->asDouble(0), pRecord->asDouble(1));
				pShape->Add_Point(pRecord->asDouble(0), pRecord->asDouble(3));
				pShape->Add_Point(pRecord->asDouble(2), pRecord->asDouble(3));
				pShape->Add_Point(pRecord->asDouble(2), pRecord->asDouble(1));
				Parameters("BND")->Set_Value(pBND);
				delete(pTable);
			}
			else if( !s.CmpNoCase(SG_T("tic")) )	// tick marks
			{
				pTIC	= SG_Create_Shapes(SHAPE_TYPE_Point, SG_T("Tick Points"));
				pTIC->Get_Table().Add_Field("ID", TABLE_FIELDTYPE_Int);
				pTIC->Get_Table().Add_Field("X" , TABLE_FIELDTYPE_Double);
				pTIC->Get_Table().Add_Field("Y" , TABLE_FIELDTYPE_Double);
				for(i=0; i<pTable->Get_Record_Count(); i++)
				{
					pRecord	= pTable->Get_Record(i);
					pShape	= pTIC->Add_Shape();
					pShape->Get_Record()->Set_Value(0, pRecord->asInt   (0));
					pShape->Get_Record()->Set_Value(1, pRecord->asDouble(1));
					pShape->Get_Record()->Set_Value(2, pRecord->asDouble(2));
					pShape->Add_Point(pRecord->asDouble(1), pRecord->asDouble(2));
				}
				Parameters("TIC")->Set_Value(pTIC);
				delete(pTable);
			}
			else
			{
				Parameters("TABLE")->Set_Value(pTable);
			}
		}
	}

	//-----------------------------------------------------
	// 0 if none, 1 if AAT, 2 if PAT, 3 if both
	return( pPAT ? (pAAT ? 3 : 2) : (pAAT ? 1 : 0) );
}

//---------------------------------------------------------
CSG_Table * CESRI_E00_Import::info_Get_Table(struct info_Table info)
{
	char			*buffer_record, *buffer_item;
	int				iRecord, iField;
	CSG_Table			*pTable;
	CSG_Table_Record	*pRecord;

	//-----------------------------------------------------
	Process_Set_Text(CSG_String(info.Name));

	buffer_record	= (char *)malloc(info.length + 3);
	buffer_item		= (char *)malloc(info.length + 3);

	pTable			= SG_Create_Table();
	pTable->Set_Name(CSG_String(info.Name));

	//-----------------------------------------------------
	for(iField=0; iField<info.uFields; iField++)
	{
		switch( info.Field[iField].Type )
		{
		case 60:	// float / double
			pTable->Add_Field(info.Field[iField].Name, TABLE_FIELDTYPE_Double);
			break;

		case 50:	// short / long
			pTable->Add_Field(info.Field[iField].Name, TABLE_FIELDTYPE_Int);
			break;

		case 40:	// float
			pTable->Add_Field(info.Field[iField].Name, TABLE_FIELDTYPE_Double);
			break;

		case 10:	// short
			pTable->Add_Field(info.Field[iField].Name, TABLE_FIELDTYPE_Int);
			break;

		default:	// string
			pTable->Add_Field(info.Field[iField].Name, TABLE_FIELDTYPE_String);
			break;
		}
	}

	//-----------------------------------------------------
	for(iRecord=0; iRecord<info.nRecords && Set_Progress(iRecord, info.nRecords); iRecord++)
	{
		info_Get_Record(buffer_record, info.length);

		pRecord	= pTable->Add_Record();

		for(iField=0; iField<info.uFields; iField++)
		{
			strncpy(buffer_item, &buffer_record[info.Field[iField].Position], info.Field[iField].Size);
			buffer_item[info.Field[iField].Size] = 0;

			switch( pTable->Get_Field_Type(iField) )
			{
			default:
				pRecord->Set_Value(iField, atof(buffer_item));
				break;

			case TABLE_FIELDTYPE_Int:
				pRecord->Set_Value(iField, atoi(buffer_item));
				break;

			case TABLE_FIELDTYPE_String:
				pRecord->Set_Value(iField, CSG_String(buffer_item));
				break;
			}
		}
	}

	//-----------------------------------------------------
	free(buffer_record);
	free(buffer_item);

	return( pTable );
}

//---------------------------------------------------------
void CESRI_E00_Import::info_Skip_Table(struct info_Table info)
{
	char	*buffer_record;
	int		iRecord;

	buffer_record	= (char *)malloc(info.length + 3);

	for(iRecord=0; iRecord<info.nRecords; iRecord++)
	{
		info_Get_Record(buffer_record, info.length);
	}

	free(buffer_record);
}

//---------------------------------------------------------
void CESRI_E00_Import::info_Get_Record(char *buffer, int buffer_length)
{
	const char *line;

	char	*p;
	int		l;

	//-----------------------------------------------------
	p	= buffer;
	l	= 0;

	//-----------------------------------------------------
	if( (line = E00ReadNextLine(hReadPtr)) != NULL )
	{
		strncpy(buffer, line, buffer_length < 84 ? buffer_length : 84);

		while( l < buffer_length )
		{
			if( *p =='\r' || *p == '\n' || *p == 0 )
			{
				while( (l % 80 || p == buffer) && l < buffer_length )
				{
					l++;
					*p++	= ' ';
				}

				if( l == buffer_length )
				{
					break;
				}
				else if( (line = E00ReadNextLine(hReadPtr)) != NULL )
				{
					strncpy(p, line, buffer_length - l < 84 ? buffer_length - l : 84);

					if( *p =='\r' || *p == '\n' || *p == 0 )	// if empty line
					{
						l++;
						*p++	= ' ';
						*p		= 0;
					}
				}
			}
			else
			{
				l++;
				p++;
			}
		}

		*p	= 0;
	}
}


///////////////////////////////////////////////////////////
//														 //
//						Skips							 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CESRI_E00_Import::Assign_Attributes(CSG_Shapes *pShapes)
{
	int				iShape, iRecord, iField, oField, id;
	CSG_Table_Record	*pRec;
	CSG_Shape			*pShape;

	if( pShapes && pShapes->Get_Table().Get_Field_Count() > 0 && pPAT && pPAT->Get_Field_Count() > 2 )
	{
		Process_Set_Text(_TL("Assign attributes to shapes..."));

		oField	= pShapes->Get_Table().Get_Field_Count();

		for(iField=0; iField<pPAT->Get_Field_Count(); iField++)
		{
			pShapes->Get_Table().Add_Field(pPAT->Get_Field_Name(iField), pPAT->Get_Field_Type(iField));
		}

		for(iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++)
		{
			pShape	= pShapes->Get_Shape(iShape);
			id		= pShape->Get_Record()->asInt(0);

			for(iRecord=0; iRecord<pPAT->Get_Record_Count(); iRecord++)
			{
				pRec	= pPAT->Get_Record(iRecord);

				if( id == pRec->asInt(2) )
				{
					for(iField=0; iField<pPAT->Get_Field_Count(); iField++)
					{
						switch( pPAT->Get_Field_Type(iField) )
						{
						case TABLE_FIELDTYPE_String:
							pShape->Get_Record()->Set_Value(oField + iField, pRec->asString(iField));
							break;

						default:
							pShape->Get_Record()->Set_Value(oField + iField, pRec->asDouble(iField));
							break;
						}
					}

					break;
				}
			}

		}

		return( true );
	}

	return( false );
}


///////////////////////////////////////////////////////////
//														 //
//						Skips							 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CESRI_E00_Import::skip(char *end)
{
	const char	*line;

	int		l	= strlen(end);

	while( (line = E00ReadNextLine(hReadPtr)) != NULL && strncmp(line, end, l) );
}

//---------------------------------------------------------
void CESRI_E00_Import::skip_dat(void)
{
	const char	*line;

	int		i	= 0;
	
	while( (line = E00ReadNextLine(hReadPtr)) != NULL && i != -1 )
	{
		sscanf(line, "%d", &i);
	}
}

//---------------------------------------------------------
void CESRI_E00_Import::skip_msk(void)
{
	const char	*line;

	double	xmin, ymin, xmax, ymax, res, sk;
	long	xsize, ysize, nskip;

	if( (line = E00ReadNextLine(hReadPtr)) != NULL )
	{
		sscanf(line, "%lf %lf %lf", &xmin, &ymin, &xmax);

		if( (line = E00ReadNextLine(hReadPtr)) != NULL )
		{
			sscanf(line, "%lf %lf %ld %ld", &ymax, &res, &xsize, &ysize);

			sk		= ((ymax - ymin) / res) * ((xmax - xmin) / res) / 32.0;
			nskip	= (long)ceil(sk / 7.0);

			while( nskip-- )
			{
				E00ReadNextLine(hReadPtr);
			}
		}
	}
}

//---------------------------------------------------------
void CESRI_E00_Import::skip_arc(int prec)
{
	const char	*line;

	int		i, covnum, nPoints;

	while( (line = E00ReadNextLine(hReadPtr)) != NULL )
	{
		sscanf(line, "%d %*d %*d %*d %*d %*d %d", &covnum, &nPoints);

		if( covnum == -1 )
			break;

		if( prec == 0 )
			nPoints	= (nPoints + 1) / 2;	// number of coordinate lines

		for(i=0; i<nPoints; i++)
		{
			E00ReadNextLine(hReadPtr);
		}
	}
}

//---------------------------------------------------------
void CESRI_E00_Import::skip_lab(int prec)
{
	const char	*line;

	long	covid;

	while( (line = E00ReadNextLine(hReadPtr)) != NULL )
	{
		sscanf(line, "%ld", &covid);

		if( covid == -1 )
			break;

		E00ReadNextLine(hReadPtr);

		if( prec )	// two lines of coordinates in double precision
			E00ReadNextLine(hReadPtr);
	}
}

//---------------------------------------------------------
void CESRI_E00_Import::skip_pal(int prec)
{
	const char	*line;

	int		i, narcs;

	while( (line = E00ReadNextLine(hReadPtr)) != NULL )
	{
		sscanf(line, "%d", &narcs);

		if( prec )	// two lines of coordinates in double precision
			E00ReadNextLine(hReadPtr);

		if( narcs == -1 )
			break;

		for(i=(narcs+1)/2; i; i--)
			E00ReadNextLine(hReadPtr);
	}
}

//---------------------------------------------------------
void CESRI_E00_Import::skip_txt(int prec)
{
	const char	*line;

	int		i, n, nskip;

	nskip	= prec ? 7 : 5;

	while( (line = E00ReadNextLine(hReadPtr)) != NULL )
	{
		sscanf( line, "%d", &n);

		if( n == -1 )
			break;

		for(i=0; i<nskip; i++)
			E00ReadNextLine(hReadPtr);
	}
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -