ArcSDE C APIデータ読み出し




#include "stdafx.h"
#include "windows.h"
#include "sdetype.h"
#include "sdeerno.h"
#include "assert.h"
#include <iostream>
#pragma comment(lib, "sde.lib")


using namespace std;


SE_LAYERINFO FindLayerInfo(const char* szName, SE_LAYERINFO *layerinfo_list, long count);

int main(int argc, char* argv[])
{
	const char* szServer = "127.0.0.1";
	const char* szInstance = "5151";
	const char* szDatabase = "sde";
	const char* szUser = "sde";
	const char* szPassword = "sde";

	const char* szName = "SDE.cities";
	LONG rc;
	long lyrcount = 0;
	SE_ERROR pseError;
	SE_CONNECTION seConnection;
	SE_STREAM seStreamCursor=NULL;
	SE_COORDREF coordref=NULL;
	SE_LAYERINFO layerinfo = NULL;
	SE_LAYERINFO *layerinfo_list = NULL;
	SE_SQL_CONSTRUCT *sql=NULL;
	SE_COLUMNINFO* pColumnList = NULL;
	SHORT m_num_columns = 1; //     
	CHAR **m_columns = new CHAR*[m_num_columns]; //       
	m_columns[0] = "SHAPE";

	DWORD ts = GetTickCount();
	DWORD ts_con = GetTickCount();
	rc = SE_connection_create(szServer, szInstance, szDatabase, szUser, szPassword, &pseError, &seConnection);
	assert(rc==SE_SUCCESS);
	DWORD te_con = GetTickCount();
	cout<<"Connect Time"<<((DWORD)(te_con-ts_con))<<endl;

	rc = SE_layer_get_info_list(seConnection, &layerinfo_list, &lyrcount);
	assert(rc==SE_SUCCESS);

	layerinfo = FindLayerInfo(szName, layerinfo_list, lyrcount);
	assert(layerinfo!=NULL);

	rc = SE_stream_create(seConnection, &seStreamCursor);
	assert(rc==SE_SUCCESS);

	rc = SE_coordref_create (&coordref);
	assert(rc==SE_SUCCESS);

	rc = SE_layerinfo_get_coordref (layerinfo, coordref);
	assert(rc==SE_SUCCESS);

	CHAR table[SE_MAX_TABLE_LEN];
	CHAR column[SE_MAX_COLUMN_LEN];
	rc = SE_layerinfo_get_spatial_column(layerinfo,table,column);
	assert(rc == SE_SUCCESS);

	rc = SE_sql_construct_alloc(1, &sql);
	assert(rc == SE_SUCCESS);
	strcpy(sql->tables[0], table);
	sql->where = strdup("");
	rc = SE_stream_query(seStreamCursor, 1, (const CHAR **)m_columns, sql);
	assert(rc == SE_SUCCESS);
	rc = SE_stream_execute(seStreamCursor);
	assert(rc==SE_SUCCESS);
	free(sql->where);
	SE_sql_construct_free(sql);

	LONG wktLen = 0;
	LONG wkbLen = 0;
	LONG binarySize = 0;
	SE_SHAPE pShape = NULL;
	char* pWKB = NULL;
	char* pWKT = NULL;
	while(SE_stream_fetch(seStreamCursor)==SE_SUCCESS)
	{
		rc = SE_shape_create(coordref, &pShape);
		rc = SE_stream_get_shape(seStreamCursor, 1, pShape);
		if(rc==SE_SUCCESS)
		{
			rc = SE_shape_get_text_size(pShape, &wktLen);
			if(rc==SE_SUCCESS)
			{
				pWKT = new char[wktLen];
				memset(pWKT, 0, wktLen);
				SE_shape_as_text(pShape, wkbLen, pWKT);
				printf("\r%s",pWKT);
				delete pWKT;
			}
			SE_shape_free(pShape);
		}
	}
	
	delete[] m_columns;
	
	SE_layer_free_info_list(lyrcount, layerinfo_list);
	SE_coordref_free(coordref);
	SE_stream_free(seStreamCursor);
	SE_connection_free(seConnection);

	return 0;
}

SE_LAYERINFO FindLayerInfo(const char* szName, SE_LAYERINFO *layerinfo_list, long count)
{
	CHAR name[SE_MAX_COLUMN_LEN];
	CHAR cachedColumnName[SE_MAX_COLUMN_LEN];

	for(long i=0; i<count; i++)
	{
		SE_layerinfo_get_spatial_column(layerinfo_list[i], name, cachedColumnName);
		if(stricmp(name, szName)==0)
		{
			return layerinfo_list[i];	
		}
	}
	return NULL;
}