DBRegion DBObjData::Extent(DBObjRecord *record) { DBRegion extent; DBCoordinate coord; DBFloat delta; DBObjTable *items; DBObjTableField *field; if (record == (DBObjRecord *) NULL) return (Extent()); switch (Type()) { case DBTypeVectorPoint: delta = pow((double) 10.0, (double) Precision()); items = TablesPTR->Item(DBrNItems); field = items->Field(DBrNCoord); coord = field->Coordinate(record); extent.Expand(coord + delta); extent.Expand(coord - delta); return (extent); case DBTypeVectorLine: case DBTypeVectorPolygon: items = TablesPTR->Item(DBrNItems); field = items->Field(DBrNRegion); return (field->Region(record)); case DBTypeGridDiscrete: case DBTypeGridContinuous: return (Extent()); case DBTypeNetwork: { DBInt cellID, cellNum; DBObjRecord *cellRec; DBNetworkIF *netIF = new DBNetworkIF(this); cellRec = netIF->MouthCell(record); cellNum = netIF->CellBasinCells(cellRec) + cellRec->RowID(); for (cellID = cellRec->RowID(); cellID < cellNum; ++cellID) { cellRec = netIF->Cell(cellID); extent.Expand(netIF->Center(cellRec) + (netIF->CellSize() / 2.0)); extent.Expand(netIF->Center(cellRec) - (netIF->CellSize() / 2.0)); } delete netIF; } return (extent); case DBTypeTable: default: return (extent); } }
int DBObjTable::Read(FILE *file, int swap) { DBInt id; DBDate date; DBCoordinate coord; DBRegion region; DBPosition pos; DBUShort row, col; DBObjRecord *record; DBObjTableField *field; if (DBObjectLIST<DBObjRecord>::Read(file, swap) != DBSuccess) return (DBFault); if (FieldPTR->Read(file, swap) != DBSuccess) return (DBFault); for (id = 0; id < FieldPTR->ItemNum(); ++id) if (FieldPTR->ReadItem(file, id, swap) == DBFault) return (DBFault); RecordLengthVAR = 0; for (field = FieldPTR->First(); field != (DBObjTableField *) NULL; field = FieldPTR->Next()) RecordLengthVAR = RecordLengthVAR > field->StartByte() + field->Length() ? RecordLengthVAR : field->StartByte() + field->Length(); for (id = 0; id < ItemNum(); ++id) { if (ReadItem(file, id, swap) == DBFault) return (DBFault); record = Item(id); if (swap == false) continue; for (field = FieldPTR->First(); field != (DBObjTableField *) NULL; field = FieldPTR->Next()) switch (field->Type()) { case DBTableFieldInt: case DBTableFieldFloat: case DBTableFieldTableRec: case DBTableFieldDataRec: switch (field->Length()) { case 2: DBByteOrderSwapHalfWord((char *) record->Data() + field->StartByte()); break; case 4: DBByteOrderSwapWord((char *) record->Data() + field->StartByte()); break; case 8: DBByteOrderSwapLongWord((char *) record->Data() + field->StartByte()); break; } break; case DBTableFieldDate: date = field->Date(record); date.Swap(); field->Date(record, date); break; case DBTableFieldCoord: coord = field->Coordinate(record); coord.Swap(); field->Coordinate(record, coord); break; case DBTableFieldRegion: region = field->Region(record); region.Swap(); field->Region(record, region); break; case DBTableFieldPosition: pos = field->Position(record); switch (field->Length()) { case sizeof(DBPosition): pos.Swap(); break; default: row = pos.Row; col = pos.Col; DBByteOrderSwapHalfWord(&row); DBByteOrderSwapHalfWord(&col); pos.Row = row; pos.Col = col; break; } field->Position(record, pos); break; default: break; } } if (MethodPTR->Read(file, swap) != DBSuccess) return (DBFault); for (id = 0; id < MethodPTR->ItemNum(); ++id) if (MethodPTR->ReadItem(file, id, swap) == DBFault) return (DBFault); return (DBSuccess); }
int DBImportARCLine (DBObjData *vecData,const char *arcCov) { FILE *inFile; DBInt arcNum, vertex, swap = DBByteOrder (DBByteOrderLITTLE), floatCov; char fileName [DBDataFileNameLen], objName [DBStringLength]; short infoHeader [50]; DBARCRecord arcRecord; DBObjTable *lines; DBObjTable *nodes; DBObjectLIST<DBObjRecord> *data; DBObjRecord *lineRec, *nodeRec, *dataRec; DBRegion dataExtent = vecData->Extent (), itemExtent; DBObjTableField *fromNodeFLD; DBObjTableField *toNodeFLD; DBObjTableField *leftPolyFLD; DBObjTableField *rightPolyFLD; DBObjTableField *vertexesFLD; DBObjTableField *vertexNumFLD; DBObjTableField *extentFLD; DBObjTableField *coordFLD; DBObjTableField *linkNumFLD; DBCoordinate *vertexes, nodeCoord; DBFloat4 floatVAR [2]; switch (vecData->Type ()) { case DBTypeVectorLine: lines = vecData->Table (DBrNItems); break; case DBTypeVectorPolygon: lines = vecData->Table (DBrNContours); break; default: CMmsgPrint (CMmsgAppError, "Invalide Vector Data Type in: %s %d",__FILE__,__LINE__); return (DBFault); } fromNodeFLD = lines->Field (DBrNFromNode); toNodeFLD = lines->Field (DBrNToNode); leftPolyFLD = lines->Field (DBrNLeftPoly); rightPolyFLD= lines->Field (DBrNRightPoly); vertexesFLD = lines->Field (DBrNVertexes); vertexNumFLD= lines->Field (DBrNVertexNum); extentFLD = lines->Field (DBrNRegion); nodes = vecData->Table (DBrNNodes); coordFLD = nodes->Field (DBrNCoord); linkNumFLD = nodes->Field (DBrNLinkNum); data = vecData->Arrays (); sprintf (fileName,"%s/arc",arcCov); if (access (fileName,R_OK) == DBFault) sprintf (fileName,"%s/arc.adf",arcCov); if ((inFile = fopen (fileName,"r")) == NULL) { CMmsgPrint (CMmsgSysError, "File Opening Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (fread (infoHeader,sizeof (short),50,inFile) != 50) { CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } arcNum = 0; for (lineRec = lines->First ();arcRecord.Read (inFile,swap) != DBFault;lineRec = lines->Next ()) { if (lineRec == NULL) { sprintf (objName,"Line: %5d",arcRecord.ID () + 1); if ((lineRec = lines->Add (objName)) == (DBObjRecord *) NULL) return (DBFault); } else DBPause ((++arcNum * 100) / lines->ItemNum ()); floatCov = arcRecord.RecordLength () - 12 == arcRecord.NumOfPnts () * (DBInt) sizeof (float) ? true : false; while (arcRecord.FromNode () > nodes->ItemNum ()) { sprintf (objName,"Node: %5d",nodes->ItemNum () + 1); nodes->Add (objName); if ((nodeRec = nodes->Item ()) == NULL) return (DBFault); linkNumFLD->Int (nodeRec,0); } if ((nodeRec = nodes->Item (arcRecord.FromNode () - 1)) == (DBObjRecord *) NULL) { CMmsgPrint (CMmsgAppError, "Node Not Found in: %s %d",__FILE__,__LINE__); return (DBFault); } if (floatCov) { if (fread (floatVAR,sizeof (floatVAR),1,inFile) != 1) { CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (swap) { DBByteOrderSwapWord (floatVAR); DBByteOrderSwapWord (floatVAR + 1); } nodeCoord.X = (DBFloat) floatVAR [0]; nodeCoord.Y = (DBFloat) floatVAR [1]; } else { if (fread (&nodeCoord,sizeof (DBCoordinate),1,inFile) != 1) { CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (swap) nodeCoord.Swap (); } coordFLD->Coordinate (nodeRec,nodeCoord); fromNodeFLD->Record (lineRec,nodeRec); if (leftPolyFLD != (DBObjTableField *) NULL) leftPolyFLD->Record (lineRec,arcRecord.LeftPoly ()); if (rightPolyFLD != (DBObjTableField *) NULL) rightPolyFLD->Record (lineRec,arcRecord.RightPoly ()); itemExtent.LowerLeft = nodeCoord; itemExtent.UpperRight = nodeCoord; linkNumFLD->Int (nodeRec,linkNumFLD->Int (nodeRec) + 1); if (arcRecord.NumOfPnts () > 2) { if ((dataRec = data->Item (lineRec->RowID ())) == (DBObjRecord *) NULL) { if ((dataRec = new DBObjRecord ("LineData",0,sizeof (DBFloat))) == (DBObjRecord *) NULL) return (DBFault); data->Add (dataRec); } dataRec->Realloc ((arcRecord.NumOfPnts () - 2) * sizeof (DBCoordinate)); if ((vertexes = (DBCoordinate *) dataRec->Data ()) == NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (floatCov) for (vertex = 0;vertex < arcRecord.NumOfPnts () - 2;++vertex) { if (fread (floatVAR,sizeof (floatVAR),1,inFile) != 1) { CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (swap) { DBByteOrderSwapWord (floatVAR); DBByteOrderSwapWord (floatVAR + 1); } vertexes [vertex].X = (DBFloat) floatVAR [0]; vertexes [vertex].Y = (DBFloat) floatVAR [1]; dataExtent.Expand (vertexes [vertex]); itemExtent.Expand (vertexes [vertex]); } else for (vertex = 0;vertex < arcRecord.NumOfPnts () - 2;++vertex) { if (fread (vertexes + vertex,sizeof (DBCoordinate),1,inFile) != 1) { CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (swap) vertexes [vertex].Swap (); dataExtent.Expand (vertexes [vertex]); itemExtent.Expand (vertexes [vertex]); } vertexesFLD->Record (lineRec,dataRec); } else vertexesFLD->Record (lineRec,(DBObjRecord *) NULL); vertexNumFLD->Int (lineRec,arcRecord.NumOfPnts () - 2); while (arcRecord.ToNode () > nodes->ItemNum ()) { sprintf (objName,"Node: %5d",nodes->ItemNum () + 1); nodes->Add (objName); if ((nodeRec = nodes->Item ()) == NULL) return (DBFault); linkNumFLD->Int (nodeRec,0); } if ((nodeRec = nodes->Item (arcRecord.ToNode () - 1)) == (DBObjRecord *) NULL) { CMmsgPrint (CMmsgAppError, "Node Not Found in: %s %d",__FILE__,__LINE__); return (DBFault); } if (floatCov) { if (fread (floatVAR,sizeof (floatVAR),1,inFile) != 1) { CMmsgPrint (CMmsgAppError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (swap) { DBByteOrderSwapWord (floatVAR); DBByteOrderSwapWord (floatVAR + 1); } nodeCoord.X = (DBFloat) floatVAR [0]; nodeCoord.Y = (DBFloat) floatVAR [1]; } else { if (fread (&nodeCoord,sizeof (DBCoordinate),1,inFile) != 1) { CMmsgPrint (CMmsgAppError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (swap) nodeCoord.Swap (); } itemExtent.Expand (nodeCoord); coordFLD->Coordinate (nodeRec,nodeCoord); linkNumFLD->Int (nodeRec,linkNumFLD->Int (nodeRec) + 1); toNodeFLD->Record (lineRec,nodeRec); extentFLD->Region (lineRec,itemExtent); } fclose (inFile); vertexesFLD->RecordProp ((DBInt) sizeof (DBFloat)); vecData->Extent (dataExtent); vecData->Projection (DBMathGuessProjection (dataExtent)); return (DBSuccess); }
int DBImportARCPoly (DBObjData *vecData,const char *arcCov) { DBInt polyNum = 0, line, lineNum, vertexNum, polyDir; DBObjTable *items = vecData->Table (DBrNItems); DBObjTable *lines = vecData->Table (DBrNContours); DBObjTableField *firstLineFLD = items->Field (DBrNFirstLine); DBObjTableField *lineNumFLD = items->Field (DBrNLineNum); DBObjTableField *polyVertexNumFLD = items->Field (DBrNVertexNum); DBObjTableField *polyExtentFLD = items->Field (DBrNRegion); DBObjTableField *leftPolyFLD = lines->Field (DBrNLeftPoly); DBObjTableField *rightPolyFLD = lines->Field (DBrNRightPoly); DBObjTableField *nextLineFLD = lines->Field (DBrNNextLine); DBObjTableField *prevLineFLD = lines->Field (DBrNPrevLine); DBObjTableField *fromNodeFLD = lines->Field (DBrNFromNode); DBObjTableField *toNodeFLD = lines->Field (DBrNToNode); DBObjTableField *lineVertextNumFLD = lines->Field (DBrNVertexNum); DBObjTableField *lineExtentFLD = lines->Field (DBrNRegion); DBObjRecord *polyRec, *lineRec, *firstNodeRec, *nodeRec, **lineRecArray; DBRegion extent, initRegion; if (DBImportARCLine (vecData,arcCov) == DBFault) return (DBFault); items->Delete (0); for (polyRec = items->First ();polyRec != (DBObjRecord *) NULL;polyRec = items->Next ()) lineNumFLD->Int (polyRec,0); for (lineRec = lines->First ();lineRec != (DBObjRecord *) NULL;lineRec = lines->Next ()) { rightPolyFLD->Record (lineRec,items->Item ((DBInt) (rightPolyFLD->Record(lineRec) - (DBObjRecord *) NULL))); leftPolyFLD->Record (lineRec,items->Item ((DBInt) (leftPolyFLD->Record (lineRec) - (DBObjRecord *) NULL))); nextLineFLD->Record (lineRec,(DBObjRecord *) NULL); prevLineFLD->Record (lineRec,(DBObjRecord *) NULL); } if ((lineRecArray = (DBObjRecord **) calloc (lines->ItemNum (),sizeof (DBObjRecord *))) == (DBObjRecord **) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); return (DBFault); } for (polyRec = items->First ();polyRec != (DBObjRecord *) NULL;polyRec = items->Next ()) { DBPause ((++polyNum * 100) / items->ItemNum ()); lineNum = vertexNum = 0; extent = initRegion; for (lineRec = lines->First ();lineRec != (DBObjRecord *) NULL;lineRec = lines->Next ()) if ((rightPolyFLD->Record (lineRec) == polyRec) || (leftPolyFLD->Record (lineRec) == polyRec)) { lineRecArray [lineNum++] = lineRec; vertexNum += lineVertextNumFLD->Int (lineRec); extent.Expand (lineExtentFLD->Region (lineRec)); } lineNumFLD->Int (polyRec,lineNum); polyExtentFLD->Region (polyRec,extent); firstLineFLD->Record (polyRec,lineRecArray [0]); firstNodeRec = (rightPolyFLD->Record (lineRecArray [0]) == polyRec) ? fromNodeFLD->Record (lineRecArray [0]) : toNodeFLD->Record(lineRecArray [0]); for (lineNum = 1;lineNum < lineNumFLD->Int (polyRec);++lineNum) { polyDir = (rightPolyFLD->Record (lineRecArray [lineNum - 1]) == polyRec); nodeRec = polyDir ? toNodeFLD->Record (lineRecArray [lineNum - 1]) : fromNodeFLD->Record(lineRecArray [lineNum - 1]); for (line = lineNum;line < lineNumFLD->Int (polyRec); ++line) if ((nodeRec == fromNodeFLD->Record (lineRecArray [line])) || (nodeRec == toNodeFLD->Record (lineRecArray [line]))) break; if (line < lineNumFLD->Int (polyRec)) { lineRec = lineRecArray [lineNum]; lineRecArray [lineNum] = lineRecArray [line]; lineRecArray [line] = lineRec; } else { vertexNum += 1; if (nodeRec != firstNodeRec) vertexNum += 1; } } polyDir = (rightPolyFLD->Record (lineRecArray [lineNum - 1]) == polyRec); nodeRec = polyDir ? toNodeFLD->Record (lineRecArray [lineNum - 1]) : fromNodeFLD->Record(lineRecArray [lineNum - 1]); polyVertexNumFLD->Int (polyRec,vertexNum + lineNum + 1 + (nodeRec != firstNodeRec ? 1 : 0)); for (lineNum = 1;lineNum < lineNumFLD->Int (polyRec);++lineNum) { if (rightPolyFLD->Record (lineRecArray [lineNum - 1]) == polyRec) nextLineFLD->Record (lineRecArray [lineNum - 1],lineRecArray [lineNum]); else prevLineFLD->Record (lineRecArray [lineNum - 1],lineRecArray [lineNum]); } if (rightPolyFLD->Record (lineRecArray [lineNum - 1]) == polyRec) nextLineFLD->Record (lineRecArray [lineNum - 1],lineRecArray [0]); else prevLineFLD->Record (lineRecArray [lineNum - 1],lineRecArray [0]); } free (lineRecArray); return (DBSuccess); }