1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
/******************************************************************************
* $Id: vfkfeature.cpp 25702 2013-03-07 17:17:54Z martinl $
*
* Project: VFK Reader - Feature definition
* Purpose: Implements IVFKFeature/VFKFeature class.
* Author: Martin Landa, landa.martin gmail.com
*
******************************************************************************
* Copyright (c) 2009-2010, 2012-2013, Martin Landa <landa.martin gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
****************************************************************************/
#include "vfkreader.h"
#include "vfkreaderp.h"
#include "cpl_conv.h"
#include "cpl_error.h"
/*!
\brief IVFKFeature constructor
\param poDataBlock pointer to VFKDataBlock instance
*/
IVFKFeature::IVFKFeature(IVFKDataBlock *poDataBlock)
{
CPLAssert(NULL != poDataBlock);
m_poDataBlock = poDataBlock;
m_nFID = -1;
m_nGeometryType = poDataBlock->GetGeometryType();
m_bGeometry = FALSE;
m_bValid = FALSE;
m_paGeom = NULL;
}
/*!
\brief IVFKFeature destructor
*/
IVFKFeature::~IVFKFeature()
{
if (m_paGeom)
delete m_paGeom;
m_poDataBlock = NULL;
}
/*!
\brief Set feature geometry type
*/
void IVFKFeature::SetGeometryType(OGRwkbGeometryType nGeomType)
{
m_nGeometryType = nGeomType;
}
/*!
\brief Set feature id
FID: 0 for next, -1 for same
\param nFID feature id
*/
void IVFKFeature::SetFID(long nFID)
{
if (m_nFID > 0) {
m_nFID = nFID;
}
else {
m_nFID = m_poDataBlock->GetFeatureCount() + 1;
}
}
/*!
\brief Set feature geometry
Also checks if given geometry is valid
\param poGeom pointer to OGRGeometry
\return TRUE on valid feature or otherwise FALSE
*/
bool IVFKFeature::SetGeometry(OGRGeometry *poGeom)
{
m_bGeometry = TRUE;
delete m_paGeom;
m_paGeom = NULL;
m_bValid = TRUE;
if (!poGeom) {
return m_bValid;
}
/* check empty geometries */
if (m_nGeometryType == wkbNone && poGeom->IsEmpty()) {
CPLDebug("OGR-VFK", "%s: empty geometry fid = %ld",
m_poDataBlock->GetName(), m_nFID);
m_bValid = FALSE;
}
/* check coordinates */
if (m_nGeometryType == wkbPoint) {
double x, y;
x = ((OGRPoint *) poGeom)->getX();
y = ((OGRPoint *) poGeom)->getY();
if (x > -430000 || x < -910000 ||
y > -930000 || y < -1230000) {
CPLDebug("OGR-VFK", "%s: invalid point fid = %ld",
m_poDataBlock->GetName(), m_nFID);
m_bValid = FALSE;
}
}
/* check degenerated linestrings */
if (m_nGeometryType == wkbLineString &&
((OGRLineString *) poGeom)->getNumPoints() < 2) {
CPLDebug("OGR-VFK", "%s: invalid linestring fid = %ld",
m_poDataBlock->GetName(), m_nFID);
m_bValid = FALSE;
}
/* check degenerated polygons */
if (m_nGeometryType == wkbPolygon) {
OGRLinearRing *poRing;
poRing = ((OGRPolygon *) poGeom)->getExteriorRing();
if (!poRing || poRing->getNumPoints() < 3) {
CPLDebug("OGR-VFK", "%s: invalid polygon fid = %ld",
m_poDataBlock->GetName(), m_nFID);
m_bValid = FALSE;
}
}
if (m_bValid)
m_paGeom = (OGRGeometry *) poGeom->clone(); /* make copy */
return m_bValid;
}
/*!
\brief Get feature geometry
\return pointer to OGRGeometry or NULL on error
*/
OGRGeometry *IVFKFeature::GetGeometry()
{
if (m_nGeometryType != wkbNone && !m_bGeometry)
LoadGeometry();
return m_paGeom;
}
/*!
\brief Load geometry
\return TRUE on success or FALSE on failure
*/
bool IVFKFeature::LoadGeometry()
{
const char *pszName;
CPLString osSQL;
if (m_bGeometry)
return TRUE;
pszName = m_poDataBlock->GetName();
if (EQUAL (pszName, "SOBR") ||
EQUAL (pszName, "OBBP") ||
EQUAL (pszName, "SPOL") ||
EQUAL (pszName, "OB") ||
EQUAL (pszName, "OP") ||
EQUAL (pszName, "OBPEJ")) {
/* -> wkbPoint */
return LoadGeometryPoint();
}
else if (EQUAL (pszName, "SBP")) {
/* -> wkbLineString */
return LoadGeometryLineStringSBP();
}
else if (EQUAL (pszName, "HP") ||
EQUAL (pszName, "DPM")) {
/* -> wkbLineString */
return LoadGeometryLineStringHP();
}
else if (EQUAL (pszName, "PAR") ||
EQUAL (pszName, "BUD")) {
/* -> wkbPolygon */
return LoadGeometryPolygon();
}
return FALSE;
}
/*!
\brief VFKFeature constructor
\param poDataBlock pointer to VFKDataBlock instance
*/
VFKFeature::VFKFeature(IVFKDataBlock *poDataBlock, long iFID) : IVFKFeature(poDataBlock)
{
m_nFID = iFID;
m_propertyList.assign(poDataBlock->GetPropertyCount(), VFKProperty());
CPLAssert(size_t (poDataBlock->GetPropertyCount()) == m_propertyList.size());
}
/*!
\brief Set feature properties
\param pszLine pointer to line containing feature definition
\return TRUE on success or FALSE on failure
*/
bool VFKFeature::SetProperties(const char *pszLine)
{
unsigned int iIndex, nLength;
const char *poChar, *poProp;
char* pszProp;
bool inString;
std::vector<CPLString> oPropList;
pszProp = NULL;
for (poChar = pszLine; *poChar != '\0' && *poChar != ';'; poChar++)
/* skip data block name */
;
if (poChar == '\0')
return FALSE; /* nothing to read */
poChar++; /* skip ';' after data block name*/
/* read properties into the list */
poProp = poChar;
iIndex = nLength = 0;
inString = FALSE;
while(*poChar != '\0') {
if (*poChar == '"' &&
(*(poChar-1) == ';' || *(poChar+1) == ';' || *(poChar+1) == '\0')) {
poChar++; /* skip '"' */
inString = inString ? FALSE : TRUE;
if (inString) {
poProp = poChar;
if (*poChar == '"') {
poChar++;
inString = FALSE;
}
}
if (*poChar == '\0')
break;
}
if (*poChar == ';' && !inString) {
pszProp = (char *) CPLRealloc(pszProp, nLength + 1);
if (nLength > 0)
strncpy(pszProp, poProp, nLength);
pszProp[nLength] = '\0';
oPropList.push_back(pszProp);
iIndex++;
poProp = ++poChar;
nLength = 0;
}
else {
poChar++;
nLength++;
}
}
/* append last property */
if (inString) {
nLength--; /* ignore '"' */
}
pszProp = (char *) CPLRealloc(pszProp, nLength + 1);
if (nLength > 0)
strncpy(pszProp, poProp, nLength);
pszProp[nLength] = '\0';
oPropList.push_back(pszProp);
/* set properties from the list */
if (oPropList.size() != (size_t) m_poDataBlock->GetPropertyCount()) {
/* try to read also invalid records */
CPLDebug("OGR-VFK", "%s: invalid number of properties %d should be %d",
m_poDataBlock->GetName(),
(int) oPropList.size(), m_poDataBlock->GetPropertyCount());
return FALSE;
}
iIndex = 0;
for (std::vector<CPLString>::iterator ip = oPropList.begin();
ip != oPropList.end(); ++ip) {
SetProperty(iIndex++, (*ip).c_str());
}
/* set fid
if (EQUAL(m_poDataBlock->GetName(), "SBP")) {
GUIntBig id;
const VFKProperty *poVfkProperty;
poVfkProperty = GetProperty("PORADOVE_CISLO_BODU");
if (poVfkProperty)
{
id = strtoul(poVfkProperty->GetValueS(), NULL, 0);
if (id == 1)
SetFID(0);
else
SetFID(-1);
}
}
else {
SetFID(0);
}
*/
CPLFree(pszProp);
return TRUE;
}
/*!
\brief Set feature property
\param iIndex property index
\param pszValue property value
\return TRUE on success
\return FALSE on failure
*/
bool VFKFeature::SetProperty(int iIndex, const char *pszValue)
{
if (iIndex < 0 || iIndex >= m_poDataBlock->GetPropertyCount() ||
size_t(iIndex) >= m_propertyList.size())
return FALSE;
if (strlen(pszValue) < 1)
m_propertyList[iIndex] = VFKProperty();
else {
OGRFieldType fType;
const char *pszEncoding;
char *pszValueEnc;
fType = m_poDataBlock->GetProperty(iIndex)->GetType();
switch (fType) {
case OFTInteger:
m_propertyList[iIndex] = VFKProperty(atoi(pszValue));
break;
case OFTReal:
m_propertyList[iIndex] = VFKProperty(CPLAtof(pszValue));
break;
default:
pszEncoding = m_poDataBlock->GetProperty(iIndex)->GetEncoding();
if (pszEncoding) {
pszValueEnc = CPLRecode(pszValue, pszEncoding,
CPL_ENC_UTF8);
m_propertyList[iIndex] = VFKProperty(pszValueEnc);
CPLFree(pszValueEnc);
}
else {
m_propertyList[iIndex] = VFKProperty(pszValue);
}
break;
}
}
return TRUE;
}
/*!
\brief Get property value by index
\param iIndex property index
\return property value
\return NULL on error
*/
const VFKProperty *VFKFeature::GetProperty(int iIndex) const
{
if (iIndex < 0 || iIndex >= m_poDataBlock->GetPropertyCount() ||
size_t(iIndex) >= m_propertyList.size())
return NULL;
const VFKProperty* poProperty = &m_propertyList[iIndex];
return poProperty;
}
/*!
\brief Get property value by name
\param pszName property name
\return property value
\return NULL on error
*/
const VFKProperty *VFKFeature::GetProperty(const char *pszName) const
{
return GetProperty(m_poDataBlock->GetPropertyIndex(pszName));
}
/*!
\brief Load geometry (point layers)
\todo Really needed?
\return TRUE on success
\return FALSE on failure
*/
bool VFKFeature::LoadGeometryPoint()
{
double x, y;
int i_idxX, i_idxY;
i_idxY = m_poDataBlock->GetPropertyIndex("SOURADNICE_Y");
i_idxX = m_poDataBlock->GetPropertyIndex("SOURADNICE_X");
if (i_idxY < 0 || i_idxX < 0)
return FALSE;
x = -1.0 * GetProperty(i_idxY)->GetValueD();
y = -1.0 * GetProperty(i_idxX)->GetValueD();
OGRPoint pt(x, y);
SetGeometry(&pt);
return TRUE;
}
/*!
\brief Load geometry (linestring SBP layer)
\todo Really needed?
\return TRUE on success or FALSE on failure
*/
bool VFKFeature::LoadGeometryLineStringSBP()
{
int id, idxId, idxBp_Id, idxPCB, ipcb;
VFKDataBlock *poDataBlockPoints;
VFKFeature *poPoint, *poLine;
OGRLineString OGRLine;
poDataBlockPoints = (VFKDataBlock *) m_poDataBlock->GetReader()->GetDataBlock("SOBR");
if (!poDataBlockPoints)
return FALSE;
idxId = poDataBlockPoints->GetPropertyIndex("ID");
idxBp_Id = m_poDataBlock->GetPropertyIndex("BP_ID");
idxPCB = m_poDataBlock->GetPropertyIndex("PORADOVE_CISLO_BODU");
if (idxId < 0 || idxBp_Id < 0 || idxPCB < 0)
return false;
poLine = this;
while (TRUE)
{
id = poLine->GetProperty(idxBp_Id)->GetValueI();
ipcb = poLine->GetProperty(idxPCB)->GetValueI();
if (OGRLine.getNumPoints() > 0 && ipcb == 1)
{
m_poDataBlock->GetPreviousFeature(); /* push back */
break;
}
poPoint = poDataBlockPoints->GetFeature(idxId, id);
if (!poPoint)
{
continue;
}
OGRPoint *pt = (OGRPoint *) poPoint->GetGeometry();
OGRLine.addPoint(pt);
poLine = (VFKFeature *) m_poDataBlock->GetNextFeature();
if (!poLine)
break;
};
OGRLine.setCoordinateDimension(2); /* force 2D */
SetGeometry(&OGRLine);
/* reset reading */
poDataBlockPoints->ResetReading();
return TRUE;
}
/*!
\brief Load geometry (linestring HP/DPM layer)
\todo Really needed?
\return TRUE on success or FALSE on failure
*/
bool VFKFeature::LoadGeometryLineStringHP()
{
int id, idxId, idxHp_Id;
VFKDataBlock *poDataBlockLines;
VFKFeature *poLine;
poDataBlockLines = (VFKDataBlock *) m_poDataBlock->GetReader()->GetDataBlock("SBP");
if (!poDataBlockLines)
return FALSE;
idxId = m_poDataBlock->GetPropertyIndex("ID");
idxHp_Id = poDataBlockLines->GetPropertyIndex("HP_ID");
if (idxId < 0 || idxHp_Id < 0)
return FALSE;
id = GetProperty(idxId)->GetValueI();
poLine = poDataBlockLines->GetFeature(idxHp_Id, id);
if (!poLine || !poLine->GetGeometry())
return FALSE;
SetGeometry(poLine->GetGeometry());
poDataBlockLines->ResetReading();
return TRUE;
}
/*!
\brief Load geometry (polygon BUD/PAR layers)
\todo Implement (really needed?)
\return TRUE on success or FALSE on failure
*/
bool VFKFeature::LoadGeometryPolygon()
{
return FALSE;
}
OGRErr VFKFeature::LoadProperties(OGRFeature *poFeature)
{
for (int iField = 0; iField < m_poDataBlock->GetPropertyCount(); iField++) {
if (GetProperty(iField)->IsNull())
continue;
OGRFieldType fType = poFeature->GetDefnRef()->GetFieldDefn(iField)->GetType();
if (fType == OFTInteger)
poFeature->SetField(iField,
GetProperty(iField)->GetValueI());
else if (fType == OFTReal)
poFeature->SetField(iField,
GetProperty(iField)->GetValueD());
else
poFeature->SetField(iField,
GetProperty(iField)->GetValueS());
}
return OGRERR_NONE;
}
|