Exemple #1
0
DBObjRecord *DBVectorIF::Item (DBCoordinate coord) const

	{
	DBObjRecord *record, *retRecord = (DBObjRecord *) NULL;

	switch (DataPTR->Type ())
		{
		case DBTypeVectorPoint:
			{
			DBFloat dist, minDist = DBHugeVal;
			DBObjTableField *coordFLD  = ItemTable->Field (DBrNCoord);
			DBMathDistanceFunction distFunc =  DBMathGetDistanceFunction (DataPTR);
			
			for (record = FirstItem ();record != (DBObjRecord *) NULL;record = NextItem ())
				{
				dist = DBMathCoordinateDistance (distFunc,coord,coordFLD->Coordinate (record));
				if (dist < minDist) { minDist = dist; retRecord = record; }
				}
			} break;
		case DBTypeVectorLine:		break;
		case DBTypeVectorPolygon:	break;
		default: CMmsgPrint (CMmsgAppError, "Unknown Vector Data Type in: %s %d",__FILE__,__LINE__); break;
		}
	return (retRecord);
	}
Exemple #2
0
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);
    }
}
Exemple #3
0
		DBInt LoadPoints (FILE *file,DBObjTable *points,DBRegion *extent)
			{
			DBInt swap = DBByteOrder (DBByteOrderLITTLE), pointNum = 0;;
			DBObjTableField *coordField = points->Field (DBrNCoord);
			DBObjRecord *record;
			DBCoordinate coord;
			
			if (coordField == (DBObjTableField *) NULL)
				{
				CMmsgPrint (CMmsgAppError, "Corrupt Point Data Block in: %s %d",__FILE__,__LINE__);
				return (DBFault);
				}
			
			for (record = points->First ();record != (DBObjRecord *) NULL;record = (DBObjRecord *) points->Next ())
				{
				if (Read (file,swap) == DBFault) return (DBFault);
				DBPause ((++pointNum * 100)/ points->ItemNum ());
				coord.X = CoordX;
				coord.Y = CoordY;
				extent->Expand (coord);
				coordField->Coordinate (record,coord);
				}
			return (DBSuccess);
			}
Exemple #4
0
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);
}
Exemple #5
0
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);
	}
Exemple #6
0
static void _RGISToolsNetBasinMouthCBK (Widget widget,RGISWorkspace *workspace,XmAnyCallbackStruct *callData)

	{
	DBDataset *dataset = UIDataset ();
	DBObjData *netData = dataset->Data (), *pntData;
	DBNetworkIF *netIF;

	widget = widget; callData = callData;

	if (netData == (DBObjData *) NULL)
		{ CMmsgPrint (CMmsgAppError, "Null Data in: %s %d",__FILE__,__LINE__); return; }
	netIF = new DBNetworkIF (netData);

	if (UIDataHeaderForm (pntData = new DBObjData ("",DBTypeVectorPoint)))
		{
		char symName [DBStringLength];
		DBInt basinID, order;
		DBCoordinate coord;
		DBObjTable *items 	= pntData->Table (DBrNItems);
		DBObjTable *symbols	= pntData->Table (DBrNSymbols);
		DBObjTableField *coordField = items->Field (DBrNCoord);
		DBObjTableField *symbolFLD	 = items->Field (DBrNSymbol);
		DBObjTableField *orderFLD 	= new DBObjTableField (DBrNOrder,DBTableFieldInt,"%3d",sizeof (DBByte));
		DBObjTableField *subbasinLengthFLD = new DBObjTableField (DBrNSubbasinLength,DBTableFieldFloat,"%10.1f",sizeof (float));
		DBObjTableField *subbasinAreaFLD = new DBObjTableField (DBrNSubbasinArea,DBTableFieldFloat,"%10.1f",sizeof (float));
		DBObjTableField *foregroundFLD = symbols->Field (DBrNForeground);
		DBObjTableField *backgroundFLD = symbols->Field (DBrNBackground);
		DBObjTableField *styleFLD = symbols->Field (DBrNStyle);
		DBObjRecord *pntRec, *symRec, *cellRec, *basinRec;
		DBRegion dataExtent;

		items->AddField (orderFLD);
		items->AddField (subbasinLengthFLD);
		items->AddField (subbasinAreaFLD);

		cellRec = netIF->Cell ((DBInt) 0);
		for (order = 0;order <= netIF->CellOrder (cellRec);++order)
			{
			sprintf (symName,"Strahler Order:%2d",order);
			symRec = symbols->Add (symName);
			styleFLD->Int (symRec,0);
			foregroundFLD->Int (symRec,1);
			backgroundFLD->Int (symRec,0);
			}

		UIPauseDialogOpen ((char *) "Creating Basin Mouth");
		for (basinID = 0;basinID < netIF->BasinNum ();++basinID)
			{
			basinRec = netIF->Basin (basinID);
			if (UIPause (basinID * 100 / netIF->BasinNum ())) goto Stop;

			symRec = symbols->Item (netIF->CellOrder (cellRec));
			cellRec = netIF->MouthCell (basinRec);
			pntRec = items->Add (basinRec->Name ());
			coord = netIF->Center (cellRec);
			coordField->Coordinate (pntRec,coord);
			symbolFLD->Record (pntRec,symRec);
			orderFLD->Int (pntRec,netIF->CellOrder (cellRec));
			subbasinLengthFLD->Float (pntRec,netIF->CellBasinLength (cellRec));
			subbasinAreaFLD->Float (pntRec,netIF->CellBasinArea (cellRec));
			dataExtent.Expand (coord);
			}
Stop:
		UIPauseDialogClose ();
		pntData->Extent (dataExtent);
		workspace->CurrentData  (pntData);
		}
	else	delete pntData;
	delete netIF;
	}
Exemple #7
0
void RGISToolsConvertToPointCBK (Widget widget,RGISWorkspace *workspace,XmAnyCallbackStruct *callData)

	{
	char *nText, *xText, *yText, *selection;
	int allowOk;
	static int convert;
	DBDataset *dataset = UIDataset ();
	DBObjData *dbData  = dataset->Data ();
	DBObjTable *itemTable;
	static Widget tableSelect = (Widget) NULL;
	static Widget dShell = (Widget) NULL, mainForm;
	static Widget nameTextF, xCoordTextF, yCoordTextF;
	XmString string;

	if (tableSelect == (Widget) NULL)	tableSelect = UISelectionCreate ((char *) "Table Selection");
	selection = UISelectObject (tableSelect,(DBObjectLIST<DBObject> *) dbData->Tables ());
	if (selection == (char *) NULL) return;
	if ((itemTable = dbData->Table (selection)) == (DBObjTable *) NULL)
		{ CMmsgPrint (CMmsgAppError, "Invalid Table in: %s %d",__FILE__,__LINE__); return; }

	_RGISToolsConvertToPointFields = itemTable->Fields ();
	if (dShell == (Widget) NULL)
		{
		Widget button;

		dShell = UIDialogForm ((char *) "Convert Table To Point",false);
		mainForm = UIDialogFormGetMainForm (dShell);

		string = XmStringCreate ((char *) "Select",UICharSetBold);
		button = XtVaCreateManagedWidget ("RGISToolsConvertToPointNameButton",xmPushButtonWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_FORM,
								XmNtopOffset,				10,
								XmNrightAttachment,		XmATTACH_FORM,
								XmNrightOffset,			10,
								XmNmarginHeight,			5,
								XmNtraversalOn,			False,
								XmNlabelString,			string,
								XmNuserData,				DBTableFieldIsString,
								NULL);
		XmStringFree (string);
		nameTextF = XtVaCreateManagedWidget ("RGISToolsConvertToPointNameTextF",xmTextFieldWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_OPPOSITE_WIDGET,
								XmNtopWidget,				button,
								XmNrightAttachment,		XmATTACH_WIDGET,
								XmNrightWidget,			button,
								XmNrightOffset,			10,
								XmNbottomAttachment,		XmATTACH_OPPOSITE_WIDGET,
								XmNbottomWidget,			button,
								XmNmaxLength,				DBStringLength,
								XmNcolumns,					DBStringLength / 2,
								NULL);
		XtAddCallback (button,XmNactivateCallback,(XtCallbackProc) _RGISToolsConvertToPointSelectCBK,nameTextF);
		string = XmStringCreate ((char *) "Name Field:",UICharSetBold);
		XtVaCreateManagedWidget ("RGISToolsConvertToPointNameLabel",xmLabelWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_OPPOSITE_WIDGET,
								XmNtopWidget,				button,
								XmNleftAttachment,		XmATTACH_FORM,
								XmNleftOffset,				10,
								XmNrightAttachment,		XmATTACH_WIDGET,
								XmNrightWidget,			nameTextF,
								XmNrightOffset,			10,
								XmNbottomAttachment,		XmATTACH_OPPOSITE_WIDGET,
								XmNbottomWidget,			button,
								XmNlabelString,			string,
								NULL);
		XmStringFree (string);

		string = XmStringCreate ((char *) "Select",UICharSetBold);
		button = XtVaCreateManagedWidget ("RGISToolsConvertToPointXCoordButton",xmPushButtonWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_WIDGET,
								XmNtopWidget,				button,
								XmNtopOffset,				10,
								XmNrightAttachment,		XmATTACH_FORM,
								XmNrightOffset,			10,
								XmNmarginHeight,			5,
								XmNtraversalOn,			False,
								XmNlabelString,			string,
								XmNuserData,				DBTableFieldIsNumeric,
								NULL);
		XmStringFree (string);
		xCoordTextF = XtVaCreateManagedWidget ("RGISToolsConvertToPointXCoordTextF",xmTextFieldWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_OPPOSITE_WIDGET,
								XmNtopWidget,				button,
								XmNrightAttachment,		XmATTACH_WIDGET,
								XmNrightWidget,			button,
								XmNrightOffset,			10,
								XmNbottomAttachment,		XmATTACH_OPPOSITE_WIDGET,
								XmNbottomWidget,			button,
								XmNmaxLength,				DBStringLength,
								XmNcolumns,					DBStringLength / 2,
								NULL);
		XtAddCallback (button,XmNactivateCallback,(XtCallbackProc) _RGISToolsConvertToPointSelectCBK,xCoordTextF);
		string = XmStringCreate ((char *) "X Coordinate:",UICharSetBold);
		XtVaCreateManagedWidget ("RGISToolsConvertToPointXCoordLabel",xmLabelWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_OPPOSITE_WIDGET,
								XmNtopWidget,				button,
								XmNleftAttachment,		XmATTACH_FORM,
								XmNleftOffset,				10,
								XmNrightAttachment,		XmATTACH_WIDGET,
								XmNrightWidget,			nameTextF,
								XmNrightOffset,			10,
								XmNbottomAttachment,		XmATTACH_OPPOSITE_WIDGET,
								XmNbottomWidget,			button,
								XmNlabelString,			string,
								NULL);
		XmStringFree (string);

		string = XmStringCreate ((char *) "Select",UICharSetBold);
		button = XtVaCreateManagedWidget ("RGISToolsConvertToPointYCoordButton",xmPushButtonWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_WIDGET,
								XmNtopWidget,				button,
								XmNtopOffset,				10,
								XmNrightAttachment,		XmATTACH_FORM,
								XmNrightOffset,			10,
								XmNbottomAttachment,		XmATTACH_FORM,
								XmNbottomOffset,			10,
								XmNmarginHeight,			5,
								XmNtraversalOn,			False,
								XmNlabelString,			string,
								XmNuserData,				DBTableFieldIsNumeric,
								NULL);
		XmStringFree (string);
		yCoordTextF = XtVaCreateManagedWidget ("RGISToolsConvertToPointYCoordTextF",xmTextFieldWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_OPPOSITE_WIDGET,
								XmNtopWidget,				button,
								XmNrightAttachment,		XmATTACH_WIDGET,
								XmNrightWidget,			button,
								XmNrightOffset,			10,
								XmNbottomAttachment,		XmATTACH_OPPOSITE_WIDGET,
								XmNbottomWidget,			button,
								XmNmaxLength,				DBStringLength,
								XmNcolumns,					DBStringLength / 2,
								NULL);
		XtAddCallback (button,XmNactivateCallback,(XtCallbackProc) _RGISToolsConvertToPointSelectCBK,yCoordTextF);
		string = XmStringCreate ((char *) "Y Coordinate:",UICharSetBold);
		XtVaCreateManagedWidget ("RGISToolsConvertToPointYCoordLabel",xmLabelWidgetClass,mainForm,
								XmNtopAttachment,			XmATTACH_OPPOSITE_WIDGET,
								XmNtopWidget,				button,
								XmNleftAttachment,		XmATTACH_FORM,
								XmNleftOffset,				10,
								XmNrightAttachment,		XmATTACH_WIDGET,
								XmNrightWidget,			nameTextF,
								XmNrightOffset,			10,
								XmNbottomAttachment,		XmATTACH_OPPOSITE_WIDGET,
								XmNbottomWidget,			button,
								XmNlabelString,			string,
								NULL);
		XmStringFree (string);
		XtAddCallback (UIDialogFormGetOkButton (dShell),XmNactivateCallback,(XtCallbackProc) UIAuxSetBooleanTrueCBK,&convert);
		}
	convert = false;
	UIDialogFormPopup (dShell);
	while (UILoop ())
		{
		xText = XmTextFieldGetString (xCoordTextF);
		yText = XmTextFieldGetString (yCoordTextF);
		allowOk = (strlen (xText) > 0) && (strlen (yText) > 0);
		XtFree (xText);	XtFree (yText);
		XtSetSensitive (UIDialogFormGetOkButton (dShell),allowOk);
		}
	UIDialogFormPopdown (dShell);

	if (convert)
		{
		DBObjData *pntData = new DBObjData ("",DBTypeVectorPoint);

		pntData->Name (dbData->Name ());
		pntData->Document (DBDocSubject,dbData->Document (DBDocSubject));
		pntData->Document (DBDocGeoDomain,dbData->Document (DBDocGeoDomain));

		if (UIDataHeaderForm (pntData))
			{
			DBCoordinate coord;
			DBObjTableField *nField, *xField, *yField, *tblFLD, *pntFLD;
			DBObjRecord *tblRec, *pntRec, *symRec;
			DBObjTable *pntTable = pntData->Table (DBrNItems);
			DBObjTable *symbols	= pntData->Table (DBrNSymbols);
			DBObjTableField *symbolIDFLD = symbols->Field (DBrNSymbolID);
			DBObjTableField *fgFLD = symbols->Field (DBrNForeground);
			DBObjTableField *bgFLD = symbols->Field (DBrNBackground);
			DBObjTableField *stFLD = symbols->Field (DBrNStyle);
			DBObjTableField *coordFLD = pntTable->Field (DBrNCoord);
			DBObjTableField *symbolFLD = pntTable->Field (DBrNSymbol);
			DBRegion extent;

			symRec = symbols->Add ("Default Symbol");
			symbolIDFLD->Int (symRec,0);
			fgFLD->Int (symRec,1);
			bgFLD->Int (symRec,2);
			stFLD->Int (symRec,0);

			nText = XmTextFieldGetString (nameTextF);
			xText = XmTextFieldGetString (xCoordTextF);
			yText = XmTextFieldGetString (yCoordTextF);
			nField = _RGISToolsConvertToPointFields->Item (nText);
			xField = _RGISToolsConvertToPointFields->Item (xText);
			yField = _RGISToolsConvertToPointFields->Item (yText);
			XtFree (nText);	XtFree (xText);	XtFree (yText);
			for (tblRec = itemTable->First ();tblRec != (DBObjRecord *) NULL;tblRec = itemTable->Next ())
				{
				if (CMmathEqualValues (coord.X = xField->Float (tblRec),xField->FloatNoData ())) continue;
				if (CMmathEqualValues (coord.Y = yField->Float (tblRec),yField->FloatNoData ())) continue;
				if (nField == (DBObjTableField *) NULL) pntRec = pntTable->Add (tblRec->Name ());
				else pntRec = pntTable->Add (nField->String (tblRec));
				coordFLD->Coordinate (pntRec,coord);
				symbolFLD->Record (pntRec,symRec);
				extent.Expand (coord);
				}
			pntData->Extent (extent);
         pntData->Projection (DBMathGuessProjection (extent));
         pntData->Precision  (DBMathGuessPrecision  (extent));
			for (tblFLD = _RGISToolsConvertToPointFields->First ();tblFLD != (DBObjTableField *) NULL;tblFLD = _RGISToolsConvertToPointFields->Next ())
				if ((tblFLD != nField) && (tblFLD != xField) && (tblFLD != yField) && DBTableFieldIsVisible (tblFLD))
					{
					pntTable->AddField (pntFLD = new DBObjTableField (*tblFLD));
					pntFLD->Required (false);
					pntRec = pntTable->First ();
					for (tblRec = itemTable->First ();tblRec != (DBObjRecord *) NULL;tblRec = itemTable->Next ())
						{
						if (CMmathEqualValues (coord.X = xField->Float (tblRec),xField->FloatNoData ())) continue;
						if (CMmathEqualValues (coord.Y = yField->Float (tblRec),yField->FloatNoData ())) continue;
						switch (tblFLD->Type ())
							{
							case DBTableFieldString:	pntFLD->String	(pntRec,tblFLD->String (tblRec));	break;
							case DBTableFieldInt:		pntFLD->Int		(pntRec,tblFLD->Int (tblRec));		break;
							case DBTableFieldFloat:		pntFLD->Float	(pntRec,tblFLD->Float (tblRec));		break;
							case DBTableFieldDate:		pntFLD->Date	(pntRec,tblFLD->Date (tblRec));		break;
							default: CMmsgPrint (CMmsgAppError, "Invalid Field Type in: %s %d",__FILE__,__LINE__);		break;
							}
						pntRec = pntTable->Next ();
						}
					}
			workspace->CurrentData (pntData);
			}
		else delete pntData;
		}
	}
Exemple #8
0
int main(int argc, char *argv[]) {
    int argPos, argNum = argc, ret, verbose = false;
    char *tableName = DBrNItems;
    char *nameSTR = (char *) NULL;
    char *xcoordSTR = (char *) "XCoord";
    char *ycoordSTR = (char *) "YCoord";
    char *title = (char *) NULL, *subject = (char *) NULL;
    char *domain = (char *) NULL, *version = (char *) NULL;
    DBObjData *data, *pntData;
    DBObjTable *table;
    DBCoordinate coord;
    DBObjTableField *nField, *xField, *yField, *tblFLD, *pntFLD;
    DBObjRecord *tblRec, *pntRec, *symRec;
    DBObjTable *pntTable;
    DBObjTable *symbols;
    DBObjTableField *symbolIDFLD;
    DBObjTableField *fgFLD;
    DBObjTableField *bgFLD;
    DBObjTableField *stFLD;
    DBObjTableField *coordFLD;
    DBObjTableField *symbolFLD;
    DBRegion extent;

    for (argPos = 1; argPos < argNum;) {
        if (CMargTest (argv[argPos], "-a", "--table")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing table name!");
                return (CMfailed);
            }
            tableName = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-n", "--name")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing next field name!");
                return (CMfailed);
            }
            nameSTR = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-x", "--xcoord")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing source field name!");
                return (CMfailed);
            }
            xcoordSTR = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-y", "--ycoord")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing target field name!");
                return (CMfailed);
            }
            ycoordSTR = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-t", "--title")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing title!");
                return (CMfailed);
            }
            title = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-u", "--subject")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing subject!");
                return (CMfailed);
            }
            subject = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-d", "--domain")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing domain!");
                return (CMfailed);
            }
            domain = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-V", "--verbose")) {
            verbose = true;
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-h", "--help")) {
            CMmsgPrint(CMmsgInfo, "%s [options] <input file> <output file>", CMfileName(argv[0]));
            CMmsgPrint(CMmsgInfo, "     -a,--table     [table name]");
            CMmsgPrint(CMmsgInfo, "     -n,--name      [name field]");
            CMmsgPrint(CMmsgInfo, "     -x,--xcoord    [xcoord field]");
            CMmsgPrint(CMmsgInfo, "     -y,--ycoord    [ycoord field]");
            CMmsgPrint(CMmsgInfo, "     -t,--title     [dataset title]");
            CMmsgPrint(CMmsgInfo, "     -u,--subject   [subject]");
            CMmsgPrint(CMmsgInfo, "     -d,--domain    [domain]");
            CMmsgPrint(CMmsgInfo, "     -V,--verbose");
            CMmsgPrint(CMmsgInfo, "     -h,--help");
            return (DBSuccess);
        }
        if ((argv[argPos][0] == '-') && ((int) strlen(argv[argPos]) > 1)) {
            CMmsgPrint(CMmsgUsrError, "Unknown option: %s!", argv[argPos]);
            return (CMfailed);
        }
        argPos++;
    }

    if (tableName == (char *) NULL) tableName = DBrNItems;

    if (argNum > 3) {
        CMmsgPrint(CMmsgUsrError, "Extra arguments!");
        return (CMfailed);
    }
    if (verbose) RGlibPauseOpen(argv[0]);

    data = new DBObjData();
    if (((argNum > 1) && (strcmp(argv[1], "-") != 0) ? data->Read(argv[1]) : data->Read(stdin)) == DBFault) {
        delete data;
        if (argNum > 1) printf("File error in: %s", argv[1]);
        return (DBFault);
    }

    if ((table = data->Table(tableName)) == (DBObjTable *) NULL) {
        CMmsgPrint(CMmsgUsrError, "Invalid table!");
        delete data;
        return (CMfailed);
    }
    if (nameSTR == (char *) NULL) nField = (DBObjTableField *) NULL;
    else if ((nField = table->Field(nameSTR)) == ((DBObjTableField *) NULL)) {
        CMmsgPrint(CMmsgUsrError, "Name field!");
        delete data;
        return (CMfailed);
    }
    if ((xField = table->Field(xcoordSTR)) == (DBObjTableField *) NULL) {
        CMmsgPrint(CMmsgUsrError, "XCoord field!");
        delete data;
        return (CMfailed);
    }
    if ((yField = table->Field(ycoordSTR)) == (DBObjTableField *) NULL) {
        CMmsgPrint(CMmsgUsrError, "YCoord field!");
        delete data;
        return (CMfailed);
    }

    pntData = new DBObjData("", DBTypeVectorPoint);
    if (title == (char *) NULL) title = (char *) "Converted Point coverage";
    if (subject == (char *) NULL) subject = (char *) "Points";
    if (domain == (char *) NULL) domain = data->Document(DBDocGeoDomain);
    if (version == (char *) NULL) version = (char *) "0.01pre";
    pntData->Name(title);
    pntData->Document(DBDocSubject, subject);
    pntData->Document(DBDocGeoDomain, domain);
    pntData->Document(DBDocVersion, version);

    pntTable = pntData->Table(DBrNItems);
    symbols = pntData->Table(DBrNSymbols);
    symbolIDFLD = symbols->Field(DBrNSymbolID);
    fgFLD = symbols->Field(DBrNForeground);
    bgFLD = symbols->Field(DBrNBackground);
    stFLD = symbols->Field(DBrNStyle);
    coordFLD = pntTable->Field(DBrNCoord);
    symbolFLD = pntTable->Field(DBrNSymbol);

    symRec = symbols->Add("Default Symbol");
    symbolIDFLD->Int(symRec, 0);
    fgFLD->Int(symRec, 1);
    bgFLD->Int(symRec, 2);
    stFLD->Int(symRec, 0);

    for (tblRec = table->First(); tblRec != (DBObjRecord *) NULL; tblRec = table->Next()) {
        if (CMmathEqualValues(coord.X = xField->Float(tblRec), xField->FloatNoData())) continue;
        if (CMmathEqualValues(coord.Y = yField->Float(tblRec), yField->FloatNoData())) continue;
        if (nField == (DBObjTableField *) NULL) pntRec = pntTable->Add(tblRec->Name());
        else pntRec = pntTable->Add(nField->String(tblRec));
        coordFLD->Coordinate(pntRec, coord);
        symbolFLD->Record(pntRec, symRec);
        extent.Expand(coord);
    }
    pntData->Extent(extent);
    pntData->Projection(DBMathGuessProjection(extent));
    pntData->Precision(DBMathGuessPrecision(extent));
    for (tblFLD = table->Fields()->First(); tblFLD != (DBObjTableField *) NULL; tblFLD = table->Fields()->Next())
        if ((tblFLD != nField) && (tblFLD != xField) && (tblFLD != yField) && DBTableFieldIsVisible(tblFLD)) {
            pntTable->AddField(pntFLD = new DBObjTableField(*tblFLD));
            pntFLD->Required(false);
            pntRec = pntTable->First();
            for (tblRec = table->First(); tblRec != (DBObjRecord *) NULL; tblRec = table->Next()) {
                if (CMmathEqualValues(coord.X = xField->Float(tblRec), xField->FloatNoData())) continue;
                if (CMmathEqualValues(coord.Y = yField->Float(tblRec), yField->FloatNoData())) continue;
                switch (tblFLD->Type()) {
                    case DBTableFieldString:
                        pntFLD->String(pntRec, tblFLD->String(tblRec));
                        break;
                    case DBTableFieldInt:
                        pntFLD->Int(pntRec, tblFLD->Int(tblRec));
                        break;
                    case DBTableFieldFloat:
                        pntFLD->Float(pntRec, tblFLD->Float(tblRec));
                        break;
                    case DBTableFieldDate:
                        pntFLD->Date(pntRec, tblFLD->Date(tblRec));
                        break;
                    default:
                        CMmsgPrint(CMmsgAppError, "Invalid Field Type in: %s %d", __FILE__, __LINE__);
                        break;
                }
                pntRec = pntTable->Next();
            }
        }

    ret = (argNum > 2) && (strcmp(argv[2], "-") != 0) ? pntData->Write(argv[2]) : pntData->Write(stdout);

    delete data;
    delete pntData;
    if (verbose) RGlibPauseClose();
    return (ret);
}