📄 importodbc.cpp
字号:
{
fprintf(importDB.m_pLogFile, "%i: Invalid value for %s\r\n",
m_nRecord, m_aColumn[i].m_sNameAttr);
return FALSE;
}
}
// Longitude
else if (wFlag == LON_COORD)
{
pY = &pAttr->GetCoord()->y;
if (!BDProjection()->StringAsLatLong(m_aColumn[i].m_sText, &dLon, CProjection::longitude | CProjection::import))
{
fprintf(importDB.m_pLogFile, "%i: Invalid value for %s\r\n",
m_nRecord, m_aColumn[i].m_sNameAttr);
return FALSE;
}
}
else if (wFlag == IMP_COORD)
{
// Allow empty coordinate pairs
if (CString(m_aColumn[i].m_sText).IsEmpty())
{
pAttr->GetCoord()->SetNull();
}
else if (!BDProjection()->StringAsCoord(m_aColumn[i].m_sText, pAttr->GetCoord()) &&
!m_aColumn[i].m_bShapeFile)
{
fprintf(importDB.m_pLogFile, "%i: Invalid value for %s\r\n",
m_nRecord, m_aColumn[i].m_sNameAttr);
};
}
}
};
};
// Assumes there is only one lat/long per column
if (pX != NULL && pY != NULL)
{
BDProjection()->LatLonToTransMercator(dLat,dLon,pX, pY);
};
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
BOOL CImportTable::LoadShapefile(CImportDB &importDB, CMapLayer& maplayer)
{
BOOL bOK = TRUE;
for (int i = 0; i < m_aColumn.GetSize(); i++)
{
if ((m_aColumn[i].m_sNameImport == SHAPEFILE_POINTS ||
m_aColumn[i].m_sNameImport == SHAPEFILE_POLYLINES) &&
m_aColumn[i].m_sNameAttr != IMPORT_SKIP)
{
importDB.m_pDlgProgress->SetText(BDString(IDS_IMPORTSHAPEFILE) + "...");
CShapeFile shapefile;
if (!shapefile.ImportShapeFile(&maplayer, m_sShapeFile))
{
fprintf(importDB.m_pLogFile, BDString(IDS_ERRORSHAPEFILE) + ": %s\r\n", m_sShapeFile);
bOK = FALSE;
}
// Ask if shapefile is protected
if (bOK && m_aColumn[i].m_sNameImport == SHAPEFILE_POLYLINES && !m_bAutoCreate)
{
CDlgProtect dlg;
if (dlg.DoModal() == IDOK && dlg.IsProtected())
{
for (int i = 0; i < maplayer.GetSize(); i++)
{
CLongLines* pLongLines = (CLongLines*)maplayer.GetAt(i)->GetMapObject();
pLongLines->SetProtected(dlg.GetMessage());
}
}
}
}
}
return bOK;
}
///////////////////////////////////////////////////////////////////////////////
//
// Update the map lines at the end as otherwise Access gets overwhelmed!
//
int CImportTable::DetermineShapefile(CAttrArray& aAttr, CMapLayer* pMapLayer)
{
int nRet = 0;
CCoord coordNull;
coordNull.SetNull();
// Search for the column containing the shapefile
for (int i = 0; i < m_aColumn.GetSize(); i++)
{
// Determine the attribute corresponding to the column
if (m_aColumn[i].m_bShapeFile && m_aColumn[i].m_bAttr)
{
// Too many records in dbf file
if (m_nRecord > pMapLayer->GetSize()) return -1;
CAttribute* pAttr = NULL;
int iAttr = GetAttr(aAttr, m_aColumn[i].m_sNameAttr);
ASSERT(iAttr != Undefined);
if (iAttr == Undefined) return FALSE;
for (int j = 0; j < aAttr.GetSize(); j++)
{
if (aAttr.GetAt(j)->GetAttrId() == LOWORD(iAttr))
{
pAttr = aAttr.GetAt(j);
}
}
// Copy the data
if (pAttr != NULL)
{
if (m_aColumn[i].m_sNameImport == SHAPEFILE_POINTS)
{
CMapLayerObj* pMapLayerObj = pMapLayer->GetAt(m_nRecord-1);
CCoord* pCoord = (CCoord*)pMapLayerObj->GetMapObject();
pAttr->SetCoord(*pCoord);
nRet = SHPPoint;
}
else if (m_aColumn[i].m_sNameImport == SHAPEFILE_POLYLINES)
{
nRet = SHPPolygon;
CLongLines* pMapLines = (CLongLines*)pMapLayer->GetAt(m_nRecord-1)->GetMapObject();
MergeShapefile(aAttr, pMapLines);
m_lAttrIdShapefile = pAttr->GetAttrId();
// Pointer transfered so set to null
pMapLayer->GetAt(m_nRecord-1)->SetMapObject(NULL);
}
}
}
}
return nRet;
}
///////////////////////////////////////////////////////////////////////////////
//
// As Access seems to run out of memory and disk space when continually updating
// longbinary objects, maplines will be stored in memory and written to database
// at end.
//
int CImportTable::MergeShapefile(CAttrArray& aAttr, CLongLines* pMapLines)
{
CAttribute* pAttrE = NULL;
int nRet = Add;
// Determine if the attributes are already stored
BOOL bFound = FALSE;
for (int i = 0; i < m_aAttrShapefile.GetSize(); i++)
{
CAttrArray& aAttrE = m_aAttrShapefile.GetAt(i).m_aAttr;
if (aAttrE.m_lFeature == aAttr.m_lFeature &&
aAttrE.m_lDate == aAttr.m_lDate)
{
// Check primary key
for (int j = 0; j < aAttrE.GetSize(); j++)
{
if (aAttrE[j]->IsPrimaryKey() &&
aAttrE[j] != aAttr[j]) break;
}
if (j == aAttrE.GetSize())
{
bFound = TRUE;
break;
}
}
}
// If not found then add to the list
if (!bFound)
{
CPolylineAttr polylineattr;
polylineattr.m_aAttr = aAttr;
m_aAttrShapefile.Add(polylineattr);
} else
{
nRet = Update;
}
// Retrieve the existing stored maplines
CPolylineAttr& aAttrE = m_aAttrShapefile[i];
CMapLayerObj* pMapLayerObj = new CMapLayerObj;
pMapLayerObj->SetMapObject(pMapLines);
pMapLayerObj->SetDataType(BDMAPLINES);
aAttrE.m_maplayer.Add(pMapLayerObj);
// Tidy up
return nRet;
}
///////////////////////////////////////////////////////////////////////////////
//
// Write the buffered shapefile and attributes back to the database
//
BOOL CImportTable::WriteShapefile(CImportDB& importDB)
{
int nImported = 0;
BOOL bOK = TRUE;
CString sProgress;
BDProgressRange(0, m_aAttrShapefile.GetSize());
for (int i = 0; i < m_aAttrShapefile.GetSize() && bOK; i++)
{
// Progress indicator
sProgress.Format(BDString(IDS_WRITING) + " %i",i+1);
BDProgressText(sProgress);
BDProgressPos(i);
CMapLayer& maplayer = m_aAttrShapefile[i].m_maplayer;
CLongLines maplines;
// Determine total size of maplines
int nSize = 0;
for (int j = 0; j < maplayer.GetSize(); j++)
{
ASSERT(maplayer.GetAt(j)->GetDataType() == BDMAPLINES);
CLongLines* pMapLines = (CLongLines*)maplayer.GetAt(j)->GetMapObject();
nSize += pMapLines->GetSize();
}
maplines.SetSize(nSize);
// Copy the data
nSize = 0;
for (j = 0; j < maplayer.GetSize(); j++)
{
CLongLines* pMapLines = (CLongLines*)maplayer.GetAt(j)->GetMapObject();
// Transfer protection status
maplines.SetProtected(pMapLines->GetProtected());
for (int k = 0; k < pMapLines->GetSize(); k++)
{
maplines.SetAt(k+nSize, pMapLines->GetAt(k));
}
nSize += pMapLines->GetSize();
}
// Determine the attribute to store the maplines
CAttrArray& aAttr = m_aAttrShapefile[i].m_aAttr;
CAttribute* pAttr = NULL;
for (j = 0; j < aAttr.GetSize(); j++)
{
if (aAttr[j]->GetAttrId() == m_lAttrIdShapefile)
{
pAttr = aAttr.GetAt(j);
break;
};
}
// Convert to long binary
CLongBinary longbinary;
maplines.GetLongBinary(longbinary);
pAttr->SetLongBinary(&longbinary);
longbinary.m_hData = NULL; // prevents deletion of memory
try
{
if (BDAttribute(BDHandle(), &aAttr, BDADD))
{
importDB.m_nImported++;
}
}
catch (CDBException* pEx)
{
pEx->Delete();
CString sError;
BDGetError(BDHandle(), sError);
fprintf(importDB.m_pLogFile, "%i: %s\r\n", i+1,(LPCSTR)sError);
}
catch (CException* pEx)
{
char szError[128];
pEx->GetErrorMessage(szError, sizeof(szError));
fprintf(importDB.m_pLogFile, "%i: %s\r\n", i+1,(LPCSTR)szError);
// Prevent from committing changes,
importDB.m_nImported = 0;
bOK = FALSE;
pEx->Delete();
}
// Must call BDEnd as using a different address for aAttr
BDEnd(BDHandle());
// Release memory as one goes along
maplayer.RemoveAll();
aAttr.RemoveAllX();
}
return bOK;
}
///////////////////////////////////////////////////////////////////////////////
void CImportDB::Write(LPCSTR sFileName)
{
FILE* pFile = fopen(sFileName, "wb");
if (pFile != NULL)
{
for (int i = 0; i < GetSize(); i++)
{
GetAt(i).Write(pFile);
}
fprintf(pFile,"end\n");
fclose(pFile);
}
}
///////////////////////////////////////////////////////////////////////////////
void CImportTable::Write(FILE* pFile)
{
fprintf(pFile, "table=%s,%s,%i,%s,%s,%s,%s\nshapefile=%s\n",
(LPCSTR)m_sTableImport,m_sFType, m_bImport,m_sFeature,m_sParentFeature,
m_sGrandParentFeature, m_sGreatGrandParentFeature,m_sShapeFile);
for (int i = 0; i < m_aColumn.GetSize(); i++)
{
m_aColumn[i].Write(pFile);
}
fprintf(pFile,"end\n");
}
///////////////////////////////////////////////////////////////////////////////
void CImportColumn::Write(FILE* pFile)
{
fprintf(pFile,"column=\"%s\",\"%s\",%i\n", (LPCSTR)m_sNameImport, (LPCSTR)m_sNameAttr,
m_bShapeFile);
}
///////////////////////////////////////////////////////////////////////////////
BOOL CImportDB::Read(LPCSTR sFileName)
{
int nRet = 0;
RemoveAll();
FILE* pFile = fopen(sFileName, "r");
if (pFile != NULL)
{
CImportTable table;
while ((nRet = table.Read(pFile)) == TRUE)
{
Add(table);
}
fclose(pFile);
}
return nRet != -1;
}
///////////////////////////////////////////////////////////////////////////////
BOOL CImportTable::Read(FILE* pFile)
{
int nRet = 0;
*this = CImportTable(); // Reset
CImportColumn column;
CString s = BDNextItem(pFile);
if (s == "table")
{
m_sTableImport = BDNextItem(pFile);
m_sFType = BDNextItem(pFile);
s = BDNextItem(pFile);
sscanf(s,"%d", &m_bImport);
m_sFeature = BDNextItem(pFile);
m_sParentFeature = BDNextItem(pFile);
m_sGrandParentFeature = BDNextItem(pFile);
m_sGreatGrandParentFeature = BDNextItem(pFile);
m_sShapeFile = BDNextStr(pFile);
while ((nRet = column.Read(pFile)) == TRUE)
{
m_aColumn.Add(column);
}
if (nRet == -1) return -1;
} else
{
if (s != "end") return -1;
return FALSE;
}
return TRUE;
};
///////////////////////////////////////////////////////////////////////////////
BOOL CImportColumn::Read(FILE* pFile)
{
*this = CImportColumn(); // Reset
CString s = BDNextItem(pFile);
if (s == "column")
{
m_sNameImport = BDNextItem(pFile);
m_sNameAttr = BDNextItem(pFile);
m_bShapeFile = BDNextInt(pFile,FALSE);
} else
{
if (s != "end") return -1;
return FALSE;
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
int CImportDB::Find(LPCSTR sTable)
{
for (int j = 0; j < GetSize(); j++)
{
if (GetAt(j).m_sTableImport == sTable) break;
}
// If not found then add
if (j == GetSize()) return -1;
else return j;
}
///////////////////////////////////////////////////////////////////////////////
//
// Searches through the attributes for one with the given name. Returns the
// index or -1 on error
//
int CImportTable::GetAttr(CAttrArray& aAttr, CString sAttr)
{
WORD wFlag = 0;
// Test for x,y coordinate suffix
if (sAttr.Right(strlen(X_COORDS)) == X_COORDS)
{
sAttr = sAttr.Left(sAttr.GetLength()-strlen(X_COORDS));
wFlag = X_COORD;
};
if (sAttr.Right(strlen(Y_COORDS)) == Y_COORDS)
{
sAttr = sAttr.Left(sAttr.GetLength()-strlen(Y_COORDS));
wFlag = Y_COORD;
}
if (sAttr.Right(strlen(LAT_COORDS)) == LAT_COORDS)
{
sAttr = sAttr.Left(sAttr.GetLength()-strlen(LAT_COORDS));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -