qtvplugin_grid.cpp 25.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "qtvplugin_grid.h"
#include "ui_qtvplugin_grid.h"
#include <QPainter>
#include <QBrush>
#include <QCoreApplication>
#include <QPen>
#include <QDebug>
#include <QLibraryInfo>
#include <QFileInfo>
#include <QMutex>
#include <QMap>
#include <assert.h>
13 14
#include <QMessageBox>
#include <QSettings>
15
#include <math.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*!
 * The plugin dynamic library (.dll in windows or .so in linux) will be loaded into memory only once.
 * for example, a windows app like test_container will contain 2 qtaxviewer_planetosm OCX  ctrls ,
 * each OCX ctrl will load plugins when initializing. We need a mechanism to handle this situation,
 * that maintains a connection between plugin instances and their parents(always be viewer_interface).
 * For the reason above, these paraments are introduced:
*/
QMutex mutex_instances;										//!This QMutex protect map_instances and count_instances
QMap<viewer_interface *,  qtvplugin_grid * > map_instances;	//!Mapping viewer_interface to qtvplugins
QMap<QString,  int > count_instances;						//!a counter for instances numbering


qtvplugin_grid::qtvplugin_grid(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::qtvplugin_grid),
	m_nInstance(0)
{
	ui->setupUi(this);
	m_pVi = 0;
	m_bVisible = true;
	m_pt_end = QPointF(-1,-1);
	bFinished = true;
	m_bActive = false;
39
	m_nMarks = 0;
丁劲犇's avatar
丁劲犇 已提交
40 41 42 43 44
	m_pModelCombo = new QStandardItemModel(this);
	m_pModelCombo->appendRow(new QStandardItem(tr("POINT")));
	m_pModelCombo->appendRow(new QStandardItem(tr("LINE")));
	m_pModelCombo->appendRow(new QStandardItem(tr("POLYGON")));
	ui->combox_type->setModel(m_pModelCombo);
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
}

qtvplugin_grid::~qtvplugin_grid()
{
	delete ui;
}
void qtvplugin_grid::load_retranslate_UI()
{
	ui->retranslateUi(this);
}

layer_interface * qtvplugin_grid::load_initial_plugin(QString strSLibPath,viewer_interface  * ptrviewer)
{
	//!In this instance, we will see how to create a new instance for each ptrviewer
	qtvplugin_grid * instance = 0;

	//!1.Check whether there is already a instance for ptrviewer( viewer_interface)
	mutex_instances.lock();
	if (map_instances.empty()==true)
	{
		map_instances[ptrviewer] = this;
		instance = this;
	}
	else if (map_instances.contains(ptrviewer)==false)
	{
		//Create a new instance for ptrviewer
		instance = new qtvplugin_grid;
		map_instances[ptrviewer] = instance;
	}
	else
		instance = map_instances[ptrviewer];
	mutex_instances.unlock();

	//!2.if the instance correspones to this, do init operations.
	if (instance==this)
	{
		QFileInfo info(strSLibPath);
		m_SLLibName = info.completeBaseName();
83 84
		m_SLLibPath = strSLibPath;

85 86 87 88 89 90 91
		m_pVi = ptrviewer;

		mutex_instances.lock();
		m_nInstance = ++count_instances[m_SLLibName];
		mutex_instances.unlock();

		loadTranslation();
92
		load_ini();
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
	}
	//!3.if the instance not correspones to this, call the instances' load_initial_plugin instead.
	else
	{
		 layer_interface * ret = instance->load_initial_plugin(strSLibPath,ptrviewer);
		 assert(ret==instance);
	}

	return instance;
}

bool	qtvplugin_grid::loadTranslation()
{
	bool res = false;
	//Trans
	QCoreApplication * app =  QCoreApplication::instance();
	if (app && m_nInstance==1)
	{
		QString strTransLocalFile =
				QCoreApplication::applicationDirPath()+"/" +
				m_SLLibName+ "_" +
				QLocale::system().name()+".qm";
		if (true==pluginTranslator.load(strTransLocalFile ))
		{
			QTVOSM_DEBUG("Load translationfile")<<"\n\t"<<strTransLocalFile<<" Succeeded.";
			app->installTranslator(&pluginTranslator);
			ui->retranslateUi(this);
			res = true;
		}
		else
			QTVOSM_WARNING("Load translationfile")<<"\n\t"<<strTransLocalFile<<" Not Found.";
	}
	return res;
}

QWidget * qtvplugin_grid::load_prop_window()
{
	return this;
}

void qtvplugin_grid::cb_paintEvent( QPainter * pImage )
{
	int step_level[10] = {20,20,10,10,5,5,2,1,1,1};
	double dstep_level[10] = {0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002,0.001,0.0005};
	if (!m_pVi || m_bVisible==false)
		return ;
	QRect rect = m_pVi->windowRect();
	//Get current viewport pos, in LLA
	double lat_top,lon_left;
	double lat_bottom,lon_right;
	m_pVi->CV_DP2LLA(
		rect.left(),
		rect.top(),
		&lat_top,&lon_left
		);
	if (lat_top >=85) lat_top = 85;
	m_pVi->CV_DP2LLA(
		rect.right(),
		rect.bottom(),
		&lat_bottom,&lon_right
		);
	if (lat_bottom <=-85) lat_bottom = -85;

	QPen pen_line(QColor(0,0,0,96)), pen_area(QColor(255,0,0,96));
	pen_area.setWidth(4);
	QPen oldpen = pImage->pen();
	pImage->setPen(pen_line);
	char str[1024];

	if (m_pVi->level()<9)
	{
		int step = step_level[m_pVi->level()];
		for (int lat=int(lat_bottom/10.0-1.5)*10;lat<=int(lat_top/10.0+1.5)*10&&m_pVi->level()>0;lat+=step)
		{
			if (lat>=85 || lat<=-85)
				continue;
			int DP_left,DP_top,DP_right,DP_bottom;
			if (false==m_pVi->CV_LLA2DP(lat,lon_left,&DP_left,&DP_top))
				continue;
			if (false==m_pVi->CV_LLA2DP(lat,lon_right,&DP_right,&DP_bottom))
				continue;
			pImage->drawLine(DP_left,DP_top,DP_right,DP_bottom);
			sprintf(str,"%-3d",lat);
			pImage->drawText(DP_left,DP_top-8,str);
		}
		for (int lon=int(lon_left/10.0-1.5)*10;lon<=(lon_right/10.0+1.5)*10&&m_pVi->level()>0;lon+=step)
		{

			int DP_left,DP_top,DP_right,DP_bottom;
			if (false==m_pVi->CV_LLA2DP(lat_top,lon,&DP_left,&DP_top))
				continue;
			if (false==m_pVi->CV_LLA2DP(lat_bottom,lon,&DP_right,&DP_bottom))
				continue;
			pImage->drawLine(DP_left,DP_top,DP_right,DP_bottom);
			//warpping
			int nLon = lon;
			while (nLon<-180)
				nLon+=360;
			while (nLon>180)
				nLon-=360;
			sprintf(str,"%-4d",nLon);
			pImage->drawText(DP_left,DP_bottom-16,str);
		}
	}
	else
	{
		double step = dstep_level[m_pVi->level()-9];
		for (double lat=int(lat_bottom*(1/step)-1.5)*step;
			lat<=int(lat_top*(1/step)+1.5)*step&&m_pVi->level()>0;lat+=step)
		{
			if (lat>=85 || lat<=-85)
				continue;
			int DP_left,DP_top,DP_right,DP_bottom;
			if (false==m_pVi->CV_LLA2DP(lat,lon_left,&DP_left,&DP_top))
				continue;
			if (false==m_pVi->CV_LLA2DP(lat,lon_right,&DP_right,&DP_bottom))
				continue;
			pImage->drawLine(DP_left,DP_top,DP_right,DP_bottom);
			sprintf(str,"%-8.3f",lat);
			pImage->drawText(DP_left,DP_top-8,str);
		}
		for (double lon=int(lon_left*(1/step)-1.5)*step;lon<=int(lon_right*(1/step)+1.5)*step&&m_pVi->level()>0;lon+=step)
		{
			//if (lon>=180 || lon<=-180)
			//	continue;
			int DP_left,DP_top,DP_right,DP_bottom;
			if (false==m_pVi->CV_LLA2DP(lat_top,lon,&DP_left,&DP_top))
				continue;
			if (false==m_pVi->CV_LLA2DP(lat_bottom,lon,&DP_right,&DP_bottom))
				continue;
			pImage->drawLine(DP_left,DP_top,DP_right,DP_bottom);
			//Warpping
			float dLon = lon;
			while (dLon<-180)
				dLon+=360;
			while (dLon>180)
				dLon-=360;
			sprintf(str,"%-8.3f",(float)dLon);
			pImage->drawText(DP_left,DP_bottom-16,str);
		}

	}

236 237 238 239 240 241 242 243 244 245 246
	//draw
	{
		double clat,clon;
		char buftmp[256];

		m_pVi->CV_DP2LLA(m_mousePos.x(),m_mousePos.y(),&clat,&clon);
		sprintf (buftmp,"Mouse LAT=%14.9lf, LON=%14.9lf\n",clat,clon);
		QString strMsg = buftmp;
		pImage->drawText(0,16,strMsg);

	}
247

248

249 250
	int x1,y1,x2,y2;
	int sz = m_list_points.size();
251
	QPen pen_text(QColor(0,0,255));
252 253 254 255
	for (int i=0;i<sz-1;++i)
	{
		m_pVi->CV_LLA2DP(m_list_points[i].x(),m_list_points[i].y(),&x1,&y1);
		m_pVi->CV_LLA2DP(m_list_points[i+1].x(),m_list_points[i+1].y(),&x2,&y2);
256
		pImage->setPen(pen_area);
257
		pImage->drawLine(x1,y1,x2,y2);
258 259 260 261 262 263
		double sita;
		double dis = CalDistance(m_list_points[i].x(),m_list_points[i+1].x(),m_list_points[i].y(),m_list_points[i+1].y(), &sita);
		sprintf(str,"%.2lfkm",dis/1000.0);
		pImage->setPen(pen_text);
		QPointF pos = QLineF(x1,y1,x2,y2).pointAt(i*0.8 / sz+0.1);
		pImage->drawText(pos,str);
264 265 266 267 268
	}
	if (bFinished==false && sz>0)
	{
		m_pVi->CV_LLA2DP(m_list_points.last().x(),m_list_points.last().y(),&x1,&y1);
		m_pVi->CV_LLA2DP(m_pt_end.x(),m_pt_end.y(),&x2,&y2);
269
		pImage->setPen(pen_area);
270
		pImage->drawLine(x1,y1,x2,y2);
271 272 273 274 275 276
		double sita;
		double dis = CalDistance(m_list_points.last().x(),m_pt_end.x(),m_list_points.last().y(),m_pt_end.y(), &sita);
		sprintf(str,"%.1lfkm",dis/1000.0);
		pImage->setPen(pen_text);
		QPointF pos = QLineF(x1,y1,x2,y2).pointAt(1*0.8 / sz+0.1);
		pImage->drawText(pos,str);
277
	}
278

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	pImage->setPen(oldpen);
}

bool qtvplugin_grid::is_visible()
{
	return m_bVisible;
}

void qtvplugin_grid::set_visible(bool vb)
{
	m_bVisible = vb;
}

QString qtvplugin_grid::get_name()
{
	QString strName = m_SLLibName.mid(10);
	if (m_SLLibName.left(3)=="lib")
		strName = m_SLLibName.mid(13);
	if (strName.length())
298
		return strName /*+ QString("%1").arg(m_nInstance)*/;
299 300 301 302 303 304 305 306 307 308 309
	else
		return "grid";
}
bool qtvplugin_grid::is_active()
{
	return m_bActive;
}

void qtvplugin_grid::set_active(bool ab)
{
	m_bActive = ab;
310
	ui->checkBox_QTV_measure->setChecked(m_bActive);
311 312 313 314 315 316
}

void qtvplugin_grid::set_name(QString /*vb*/)
{

}
317
void qtvplugin_grid::on_checkBox_QTV_measure_clicked(bool acti)
318
{
319
	save_ini();
320 321 322 323 324 325 326 327 328 329 330 331 332 333
	m_bActive = acti;
	m_pVi->updateLayerGridView();
}
bool qtvplugin_grid::cb_event(const QMap<QString, QVariant> para)
{
	if (m_pVi==0)
		return false;
	return false;
}
bool qtvplugin_grid::cb_mouseMoveEvent(QMouseEvent * e )
{
	if (m_pVi==0)
		return false;
	m_mousePos = e->pos();
334
	if (m_bVisible==true)
335 336 337 338 339 340 341 342 343 344
	{
		double clat,clon;
		char buftmp[256];

		m_pVi->CV_DP2LLA(m_mousePos.x(),m_mousePos.y(),&clat,&clon);
		sprintf (buftmp,"Mouse LAT=%14.9lf, LON=%14.9lf\n",clat,clon);
		QString strMsg = buftmp;
		m_pVi->centerLLA(&clat,&clon);
		sprintf (buftmp,"Center LAT=%14.9lf, LON=%14.9lf",clat,clon);
		strMsg += buftmp;
345
		ui->plainTextEdit_QTV_cursor->setPlainText(strMsg);
346
	}
347 348
	if (m_bActive==false)
		return false;
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
	if (m_list_points.size()>0&&bFinished==false)
	{
		QPoint pt = e->pos();
		double lat,lon;
		m_pVi->CV_DP2LLA(pt.x(),pt.y(),&lat,&lon);
		QPointF point(lat,lon);
		m_pt_end = point;
		return true;
	}
	return false;
}

bool qtvplugin_grid::cb_mousePressEvent(QMouseEvent *e )
{
	if (m_pVi==0)
		return false;
	if (m_bActive==false)
		return false;
	if (e->buttons() & Qt::LeftButton)
	{
		QPoint pt = e->pos();
		double lat,lon;
		m_pVi->CV_DP2LLA(pt.x(),pt.y(),&lat,&lon);
		QPointF point(lat,lon);
		if (bFinished==true)
		{
			m_list_points.clear();
			bFinished = false;
		}
		m_list_points.push_back(point);
		m_pt_end = point;
		CalArea();
		return true;
	}
	else if (e->buttons() & Qt::RightButton)
	{
		if (bFinished==true)
		{
			m_list_points.clear();
			return true;
		}
		if (m_list_points.empty()==false)
			m_list_points.push_back(m_list_points.first());
		bFinished = true;
		CalArea();
		return true;
	}
	return false;
}

void qtvplugin_grid::CalArea()
{
	QString strMsg;
	int Count = m_list_points.size();
	if (Count<1)
		return;
	double * PointX = new double [Count];
	double * PointY = new double [Count];
	char  * buffertmp = new char[2048];
	bool Valid = true;
	strMsg += "Points:\n";
	//Length
	double dTotalDis = 0;
	double dPreLat,dPreLon;
丁劲犇's avatar
丁劲犇 已提交
413
	QString str_Polygon,str_LineString,str_QuickMark;
414 415 416

	str_Polygon = ("ST_Transform(ST_GeomFromText('POLYGON(\n(");
	str_LineString = ("ST_Transform(ST_GeomFromText('LINESTRING(\n(");
丁劲犇's avatar
丁劲犇 已提交
417

418
	bool bLatFirst = ui->radioButton_QTV_latfirst->isChecked();
丁劲犇's avatar
丁劲犇 已提交
419

420 421 422
	for (int i=0;i<Count;i++)
	{
		double dLatCurr = m_list_points[i].x(),dLonCurr = m_list_points[i].y();
丁劲犇's avatar
丁劲犇 已提交
423 424 425 426 427

		if (bLatFirst)
			str_QuickMark += QString("%1 , %2\n").arg(dLatCurr,0,'f',7).arg(dLonCurr,0,'f',7);
		else
			str_QuickMark += QString("%2 , %1\n").arg(dLatCurr,0,'f',7).arg(dLonCurr,0,'f',7);
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
		if (i>0)
		{
			double sita;
			double dis = CalDistance(dPreLat,dLatCurr,dPreLon,dLonCurr, &sita);

			sprintf(buffertmp,"(%11.6lf,%11.6lf)->(%11.6lf,%11.6lf),%11.2lf(m),%11.2lf(km),SITA=%.12lf (degree)\n",
				dPreLat,dPreLon,dLatCurr,dLonCurr,dis,dis/1000.0,sita);
			strMsg += buffertmp;
			dTotalDis += dis;
		}
		sprintf(buffertmp,("\t%14.9lf %14.9lf"),dLonCurr,dLatCurr);
		strMsg += buffertmp;
		if (i!=Count-1)
			strcat(buffertmp,",");
		strcat(buffertmp,"\n");
		str_Polygon += buffertmp;
		str_LineString += buffertmp;

		dPreLat = dLatCurr;
		dPreLon = dLonCurr;


		PointX [i] = dLonCurr;
		PointY [i] = dLatCurr;
	}
	str_Polygon += (")\n)',4326),900913)\n");
	str_LineString +=  (")',4326),900913)\n");

	if (Valid==true)
	{
		double Area = GetArea(PointX,PointY,Count);
		sprintf(buffertmp,"\n\nArea:\n%lf\tm^2\n%lf\tkm^2\n\nTotalLength:\n%lf\t(m)\n%lf\t(km)\n\nPostGIS String:\n",
			Area,Area/1000000.0,
			dTotalDis,dTotalDis/1000.0);
		strMsg += buffertmp;
		strMsg += str_Polygon;
		strMsg += str_LineString;
	}


	delete [] PointX;
	delete [] PointY;
	delete [] buffertmp;

472 473
	ui->plainTextEdit_QTV_RES->setPlainText(strMsg);
	ui->plainTextEdit_QTV_markcmd->setPlainText(str_QuickMark);
474 475
}

丁劲犇's avatar
丁劲犇 已提交
476 477 478 479 480 481 482 483
/**
 * @brief Classical Polygon Area caculate method
 *
 * @param PointX	the x points
 * @param PointY	the y points
 * @param Count		the points' count
 * @return double	area.
 */
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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
double qtvplugin_grid::GetArea(double * PointX,double * PointY,int Count)
{
	const double pi=3.1415926535897932384626433832795;
	const double R=6378137;
	try
	{
		if (Count>3)
		{
			double mtotalArea = 0;
			if((PointX[0]!=PointX[Count-1])||(PointY[0]!=PointY[Count-1]))
				return 0;
			Count--;
			double LowX=0.0;
			double LowY=0.0;
			double MiddleX=0.0;
			double MiddleY=0.0;
			double HighX=0.0;
			double HighY=0.0;

			double AM = 0.0;
			double BM = 0.0;
			double CM = 0.0;

			double AL = 0.0;
			double BL = 0.0;
			double CL = 0.0;

			double AH = 0.0;
			double BH = 0.0;
			double CH = 0.0;

			double CoefficientL = 0.0;
			double CoefficientH = 0.0;

			double ALtangent = 0.0;
			double BLtangent = 0.0;
			double CLtangent = 0.0;

			double AHtangent = 0.0;
			double BHtangent = 0.0;
			double CHtangent = 0.0;

			double ANormalLine = 0.0;
			double BNormalLine = 0.0;
			double CNormalLine = 0.0;

			double OrientationValue = 0.0;

			double AngleCos = 0.0;

			double Sum1 = 0.0;
			double Sum2 = 0.0;
			double Count2 = 0;
			double Count1 = 0;


			double Sum = 0.0;
			double Radius = R;
			double Math_PI = pi;
			for(int i=0;i<Count;i++)
			{
				if(i==0)
				{
					LowX = PointX[Count-1] * Math_PI / 180;
					LowY = PointY[Count-1] * Math_PI / 180;
					MiddleX = PointX[0] * Math_PI / 180;
					MiddleY = PointY[0] * Math_PI / 180;
					HighX = PointX[1] * Math_PI / 180;
					HighY = PointY[1] * Math_PI / 180;
				}
				else if(i==Count-1)
				{
					LowX = PointX[Count-2] * Math_PI / 180;
					LowY = PointY[Count-2] * Math_PI / 180;
					MiddleX = PointX[Count-1] * Math_PI / 180;
					MiddleY = PointY[Count-1] * Math_PI / 180;
					HighX = PointX[0] * Math_PI / 180;
					HighY = PointY[0] * Math_PI / 180;
				}
				else
				{
					LowX = PointX[i-1] * Math_PI / 180;
					LowY = PointY[i-1] * Math_PI / 180;
					MiddleX = PointX[i] * Math_PI / 180;
					MiddleY = PointY[i] * Math_PI / 180;
					HighX = PointX[i+1] * Math_PI / 180;
					HighY = PointY[i+1] * Math_PI / 180;
				}

				AM = cos(MiddleY) * cos(MiddleX);
				BM = cos(MiddleY) * sin(MiddleX);
				CM = sin(MiddleY);
				AL = cos(LowY) * cos(LowX);
				BL = cos(LowY) * sin(LowX);
				CL = sin(LowY);
				AH = cos(HighY) * cos(HighX);
				BH = cos(HighY) * sin(HighX);
				CH = sin(HighY);


				CoefficientL = (AM*AM + BM*BM + CM*CM)/(AM*AL + BM*BL + CM*CL);
				CoefficientH = (AM*AM + BM*BM + CM*CM)/(AM*AH + BM*BH + CM*CH);

				ALtangent = CoefficientL * AL - AM;
				BLtangent = CoefficientL * BL - BM;
				CLtangent = CoefficientL * CL - CM;
				AHtangent = CoefficientH * AH - AM;
				BHtangent = CoefficientH * BH - BM;
				CHtangent = CoefficientH * CH - CM;


				AngleCos = (AHtangent * ALtangent + BHtangent * BLtangent + CHtangent * CLtangent)/
					(sqrt(AHtangent * AHtangent + BHtangent * BHtangent +CHtangent * CHtangent) *
					sqrt(ALtangent * ALtangent + BLtangent * BLtangent +CLtangent * CLtangent));

				AngleCos = acos(AngleCos);

				ANormalLine = BHtangent * CLtangent - CHtangent * BLtangent;
				BNormalLine = 0 - (AHtangent * CLtangent - CHtangent * ALtangent);
				CNormalLine = AHtangent * BLtangent - BHtangent * ALtangent;

				if(AM!=0)
					OrientationValue = ANormalLine/AM;
				else if(BM!=0)
					OrientationValue = BNormalLine/BM;
				else
					OrientationValue = CNormalLine/CM;

				if(OrientationValue>0)
				{
					Sum1 += AngleCos;
					Count1 ++;

				}
				else
				{
					Sum2 += AngleCos;
					Count2 ++;
				}

			}

			if(Sum1>Sum2){
				Sum = Sum1+(2*Math_PI*Count2-Sum2);
			}
			else{
				Sum = (2*Math_PI*Count1-Sum1)+Sum2;
			}

			//m^2
			mtotalArea = (Sum-(Count-2)*Math_PI)*Radius*Radius;
			return mtotalArea;
		}
	}
	catch(...)
	{
		return 0;
	}
	return 0;
}


丁劲犇's avatar
丁劲犇 已提交
646 647 648 649 650 651 652 653 654 655
/**
 * @brief Calculate distance between  2 positions
 *
 * @param dLatStart	start latitude
 * @param dLatEnd	end latitude
 * @param dLonStart	start longitude
 * @param dLonEnd	end longitude
 * @param psita		the earth center angle in polar
 * @return double	distance.
 */
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
double qtvplugin_grid::CalDistance(double dLatStart,double dLatEnd,double dLonStart,double dLonEnd,double * psita)
{
	//deg to rad
	const double pi=3.1415926535897932384626433832795;
	const double R=6378137;
	double drLatStart = dLatStart /180.0 * pi;
	double drLatEnd = dLatEnd /180.0 * pi;
	double drLonStart = dLonStart /180.0 * pi;
	double drLonEnd = dLonEnd /180.0 * pi;

	//Calc the ECEF coords
	double x1,y1,z1,x2,y2,z2;
	x1 = cos(drLatStart)*cos(drLonStart);
	y1 = sin(drLatStart);
	z1 = cos(drLatStart)*sin(drLonStart);

	x2 = cos(drLatEnd)*cos(drLonEnd);
	y2 = sin(drLatEnd);
	z2 = cos(drLatEnd)*sin(drLonEnd);


	//COS Angle
	double dot = x1*x2+y1*y2+z1*z2;
	double abs1 = sqrt(x1*x1+y1*y1+z1*z1);
	double abs2 = sqrt(x2*x2+y2*y2+z2*z2);
	double cossita = dot/(abs1*abs2);
	double sita = acos(cossita);

	double dist = R * sita;

	*psita = sita / pi * 180;

	return dist;
}

丁劲犇's avatar
丁劲犇 已提交
691 692
/**
 * function calls avaliable:
693
 * 	1.function=get_polygon, no other para needed. returns current selected polygon's cornor points, in lat, lon; size=N;lat0=XX;lon0=XX;
丁劲犇's avatar
丁劲犇 已提交
694 695 696 697 698 699 700 701
 * 	lat1=XX;lon1=XX;lat2=XX;lon2=XX;...;latN-1=XX;lonN-1=XX.
 * 	2.function=get_ruler_status, no other para needed.returns whether ruler tool is active now, status=0 means not active, status=-1 means active.
 * 	3.function=set_ruler_status, with para status, will set ruler status to given value.
 * please notice that the function should be called from the MAIN THREAD ONLY.
 *
 * @param paras	the key-value style paraments.
 * @return QMap<QString, QVariant>	the key-value style return paraments.
 */
702
QMap<QString, QVariant> qtvplugin_grid::call_func(const  QMap<QString, QVariant> & paras)
703 704
{
	QMap<QString, QVariant> res;
705 706 707
	if (paras.contains("function"))
	{
		QString funct = paras["function"].toString();
708
		if (funct=="get_polygon")
709 710 711 712 713 714 715 716 717 718 719
		{
			int Count = m_list_points.size();
			res["size"] = Count;
			for (int i=0;i<Count;++i)
			{
				QString latkey = QString("lat%1").arg(i);
				QString lonkey = QString("lon%1").arg(i);
				res[latkey] = m_list_points[i].x();
				res[lonkey] = m_list_points[i].y();
			}
		}
丁劲犇's avatar
丁劲犇 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733
		else if (funct=="get_ruler_status")
		{
			res["status"] = m_bActive==false?0:-1;
		}
		else if (funct=="set_ruler_status")
		{
			int status = -1;
			if (paras.contains("status"))
				status = paras["status"].toInt();
			set_active(status==0?false:true);
			res["status"] = m_bActive==false?0:-1;
		}
		else
			res["error"] = QString("unknown function \"%1\".").arg(funct);
734
	}
丁劲犇's avatar
丁劲犇 已提交
735 736
	else
		res["error"] = "\"function\" keyword not specified, nothing to do.";
737 738
	return std::move(res);
}
739
void qtvplugin_grid::on_pushButton_QTV_add_mark_clicked()
740 741 742
{
	if (!m_pVi)
		return;
743
	QString strMarkerName = "geomarker";/*QString("geomarker%1").arg(m_nInstance);*/
744 745
	layer_interface * pif =  m_pVi->layer(strMarkerName);
	save_ini();
丁劲犇's avatar
丁劲犇 已提交
746
	int tp = ui->combox_type->currentIndex();
747
	QString strAll = ui->plainTextEdit_QTV_markcmd->toPlainText();
748
	QStringList strLines = strAll.split("\n",QString::SkipEmptyParts);
丁劲犇's avatar
丁劲犇 已提交
749
	int c = 0;
750
	bool bLatFirst = ui->radioButton_QTV_latfirst->isChecked();
丁劲犇's avatar
丁劲犇 已提交
751
	QMap<QString, QVariant> map_multi;
752 753
	foreach (QString str, strLines)
	{
754 755
		QString strRegWest = QString("([%1])+").arg(ui->lineEdit_QTV_west_spliter->text());
		QString strRegsout = QString("([%1])+").arg(ui->lineEdit_QTV_south_spliter->text());
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
		int latNG = 1, lonNG = 1;

		if (str.indexOf(QRegExp(strRegWest))>=0)
			lonNG=-1;
		if (str.indexOf(QRegExp(strRegsout))>=0)
			latNG=-1;


		QStringList lst = str.split(QRegExp("([^0123456789.-])+"),QString::SkipEmptyParts);
		double lat = 0, lon = 0;
		if (lst.size()==6)
		{
			lat = lst.first().toDouble();			lst.pop_front();
			lat += lst.first().toDouble() /60.0;	lst.pop_front();
			lat += lst.first().toDouble() /3600.0;	lst.pop_front();
			lon = lst.first().toDouble();			lst.pop_front();
			lon += lst.first().toDouble() /60.0;	lst.pop_front();
			lon += lst.first().toDouble() /3600.0;
		}
		else if (lst.size()==4)
		{
			lat = lst.first().toDouble();			lst.pop_front();
			lat += lst.first().toDouble() /60.0;	lst.pop_front();
			lon = lst.first().toDouble();			lst.pop_front();
			lon += lst.first().toDouble() /60.0;
		}
		else if (lst.size()==2)
		{
			lat = lst.first().toDouble();			lst.pop_front();
			lon = lst.first().toDouble();
		}
		else
		{
789 790 791 792 793 794 795
//			QMessageBox::warning(
//						this,
//						tr("Error LLA formar"),
//						tr("lat lon must have same element nums.")
//						);
//			break;
			continue;
796
		}
丁劲犇's avatar
丁劲犇 已提交
797 798 799 800 801 802
		if (bLatFirst==false)
		{
			double tmp = lat;
			lat = lon;
			lon = tmp;
		}
803 804 805 806
		lat *= latNG;
		lon *= lonNG;


丁劲犇's avatar
丁劲犇 已提交
807
		if (tp==0)
808 809 810 811 812 813
		{
			QMap<QString, QVariant> inPara, outPara;
			inPara["function"] = "update_point";
			inPara["name"] = QString("%1_%2").arg(get_name()).arg(m_nMarks);
			inPara["lat"] = lat;
			inPara["lon"] = lon;
814 815 816 817 818
			//inPara["color_pen"] = "0,0,255,128";
			//inPara["color_brush"] = "0,0,0,64";
			//inPara["width"] = "7";
			//inPara["height"] = "7";
			//inPara["type"] = 1;
819 820 821 822
			outPara = pif->call_func(inPara);
			inPara.clear();
			inPara["function"] = "update_props";
			inPara["name"] = QString("%1_%2").arg(get_name()).arg(m_nMarks);
823
			inPara["POS"] = QString("%1,%2").arg(lat).arg(lon);
824 825 826
			++m_nMarks;
			outPara = pif->call_func(inPara);
		}
丁劲犇's avatar
丁劲犇 已提交
827 828 829 830 831 832
		else
		{
			map_multi[QString("lat%1").arg(c)] = lat;
			map_multi[QString("lon%1").arg(c)] = lon;
			++c;
		}
833

丁劲犇's avatar
丁劲犇 已提交
834 835 836 837 838 839
	}
	if (tp)
	{
		QMap<QString, QVariant> outPara;
		map_multi["function"] = "update_polygon";
		map_multi["name"] = QString("%1_%2").arg(get_name()).arg(m_nMarks);
840 841
		//map_multi["color_pen"] = "0,0,255,128";
		//map_multi["color_brush"] = "0,0,0,64";
丁劲犇's avatar
丁劲犇 已提交
842 843 844 845
		map_multi["type"] = (tp==1)?6:4;
		outPara = pif->call_func(map_multi);
		++m_nMarks;
	}
846 847
}

848
void qtvplugin_grid::on_pushButton_QTV_clear_clicked()
849 850 851 852
{
	if (!m_pVi || m_nMarks<=0)
		return;
	save_ini();
853
	QString strMarkerName = "geomarker";/*QString("geomarker%1").arg(m_nInstance)*/;
854 855 856 857 858 859 860 861 862 863 864 865 866
	layer_interface * pif =  m_pVi->layer(strMarkerName);
	if (pif)
	{
		QMap<QString, QVariant> inPara, outPara;
		inPara["function"] = "delete_marks";
		for (int i=0;i<m_nMarks;++i)
			inPara[QString("name%1").arg(i)] = QString("%1_%2").arg(get_name()).arg(i);
		outPara = pif->call_func(inPara);
	}
	m_nMarks = 0;


}
867
void qtvplugin_grid::on_pushButton_QTV_clear_all_clicked()
868 869 870 871 872
{

	if (!m_pVi)
		return;
	save_ini();
873
	QString strMarkerName = "geomarker";/*QString("geomarker%1").arg(m_nInstance);*/
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
	layer_interface * pif =  m_pVi->layer(strMarkerName);
	if (pif)
	{
		QMap<QString, QVariant> inPara, outPara;
		inPara["function"] = "delete_marks";
		outPara = pif->call_func(inPara);
	}
	m_nMarks = 0;

}
QString qtvplugin_grid::ini_file()
{
	if (m_SLLibPath.size())
		return m_SLLibPath + QString("%1").arg(m_nInstance) + ".ini";
	else
		return QCoreApplication::applicationFilePath() + QString("/grid%1.ini").arg(m_nInstance);
}
void  qtvplugin_grid::load_ini()
{
	QSettings settings(ini_file(),QSettings::IniFormat);
894 895 896
	ui->lineEdit_QTV_south_spliter->setText(settings.value("settings/lineEdit_QTV_south_spliter","S").toString());
	ui->lineEdit_QTV_west_spliter->setText(settings.value("settings/lineEdit_QTV_west_spliter","W").toString());
	ui->plainTextEdit_QTV_markcmd->setPlainText(settings.value("settings/plainTextEdit_QTV_markcmd","").toString());
丁劲犇's avatar
丁劲犇 已提交
897
	ui->combox_type->setCurrentIndex(settings.value("settings/combox_type",0).toInt());
丁劲犇's avatar
丁劲犇 已提交
898 899
	bool bLatFirst = settings.value("settings/latfirst",true).toBool();
	if (bLatFirst)
900
		ui->radioButton_QTV_latfirst->setChecked(true);
丁劲犇's avatar
丁劲犇 已提交
901
	else
902
		ui->radioButton_QTV_lonfirst->setChecked(true);
903 904 905 906 907
}

void  qtvplugin_grid::save_ini()
{
	QSettings settings(ini_file(),QSettings::IniFormat);
908 909 910
	settings.setValue("settings/lineEdit_QTV_south_spliter",ui->lineEdit_QTV_south_spliter->text());
	settings.setValue("settings/lineEdit_QTV_west_spliter",ui->lineEdit_QTV_west_spliter->text());
	settings.setValue("settings/plainTextEdit_QTV_markcmd",ui->plainTextEdit_QTV_markcmd->toPlainText());
丁劲犇's avatar
丁劲犇 已提交
911
	settings.setValue("settings/combox_type",ui->combox_type->currentIndex());
912
	bool bLatFirst = ui->radioButton_QTV_latfirst->isChecked();
丁劲犇's avatar
丁劲犇 已提交
913
	settings.setValue("settings/latfirst",bLatFirst);
914
}