- using System;
- using System.Drawing;
- namespace MainSystem
- {
- /// <summary>
- ///
- /// </summary>
- public struct LayerInfo
- {
- public string szName;
- public string szLayerName;
- public string szFileName;
- public string szTableName;
- public string szFieldName;
- public string szType;
- public string szSubType; //中类
- public string szSubType2; //小类
- public string szSubType3;
- public bool bCanControl;
- public bool bVisible; //可显示状态,只能通过图层控制来改变
- public bool bSelected;
- public bool bCanSelected; //可选择
- public bool bBackground;
- public bool bLable;
- public double dScale;
- public double dShowScale;
- public int nCharacterIndex;
- public string szFontName;
- public int nFontSize;
- public int nSymSize;
- public uint nSymColor;
- public MapObjects2.MapLayer layer;
- public MapObjects2.Recordset rsSel;
- };
- public class MLine
- {
- public int nPointNumber;
- public MPoint[] pPoint;
- public MLine()
- {
- }
- };
- public class CloestPath
- {
- public MPoint pt1 = null;
- public MPoint pt2 = null;
- };
- public class MPoint
- {
- public double x;
- public double y;
- public MPoint()
- {
- }
- };
- public class Buses
- {
- public int nNum;
- public MPoint[] pts;
- public Buses()
- {
- pts = new MPoint[200];
- }
- }
- enum MapOpr {MO_NULL=0,MO_ZOOMIN,MO_ZOOMOUT,MO_ZOOMFULL,MO_PAN,MO_POINTSEL,MO_RECTSEL,MO_CIRCLESEL,MO_POLYGONSEL,MO_INFO,MO_LINEMEAS,MO_POLYMEAS,MO_SEACHBYDIST,MO_CLOSEST};
- enum MapDisp {MO_ALL = 0,MO_SCHOOL = 1, MO_TOUR,MO_HOSPITAL,MO_SHOP,MO_HOTEL,MO_GAS,MO_BANK,MO_MOVIE,MO_RESTAURANT,MO_WS,MO_POST,MO_LIBRAY,MO_STATION}
- public struct MapInfo
- {
- public string szName;
- public string szMetaTable;
- public string szIndexTable;
- public string szType;
- public MapObjects2.Rectangle rect;
- };
- public struct IndexInfo
- {
- public string szName;
- public double dX;
- public double dY;
- public MapObjects2.Rectangle m_extent;
- };
- public class CEnvironment
- {
- //常量
- public const string BUSLINE_LAYERNAME = "公交线路";
- public const string BUSSTATION_LAYERNAME = "公交车站";
- public const uint SYMBOL_COLOR_NONE = 9999;
- public string m_szDBName = "";
- public string m_szSDBPath = "";
- public string m_AppPath = "";
- public string m_szHelpPath = "";
- public MapObjects2.DataConnection m_db = null;
- public int m_nCurrMapIndex = -1;
- public int m_nMapNum = -1;
- public MapInfo[] m_mapInfos = null;
- public int m_nLayerNum = -1;
- public LayerInfo[] m_layerInfos = null;
- public int m_nIndexNum = -1;
- public IndexInfo[] m_indexInfos = null;
- public MapObjects2.Symbol m_selSymbol = null;
- public string m_szfntStation = "";
- public char m_chStation = 'a';
- public int m_nfntStation = 10;
- // public string m_szFontName = "";
- public System.Data.DataSet m_dataSet = null;
- public long m_x,m_y;
- public int m_MapOpr;
- public CloestPath m_cloestPath = null;
- public double m_dDistance; //最短距离查询
- public object m_selectedFeature = null;
- public MapObjects2.Symbol m_selectedSymbol = null;
- public short m_selectedSymbolSize;
- public double m_selectedScale;
- public MLine[] m_drawLine = null;
- public Buses m_buses = null;
- public string m_szBusFilter = "";
- public bool m_bPathInit = false;
- public CPath m_path;
- public string m_szPlaceName = "";
- public MapObjects2.Rectangle m_shapeRect;
- public MapObjects2.MapLayer m_layerRoad = null;
- public CEnvironment()
- {
- m_MapOpr = (int)MapOpr.MO_NULL;
- m_selSymbol = new MapObjects2.Symbol();
- m_selSymbol.SymbolType = MapObjects2.SymbolTypeConstants.moPointSymbol;
- m_selSymbol.Color = 0xff;
- m_path = new CPath();
- m_bPathInit = false;
- m_cloestPath = new CloestPath();
- }
- public int GetMapIndex(string szMapName)
- {
- int nIndex = -1;
- for (int i = 0; i < m_nMapNum; i ++)
- {
- if (szMapName.Equals(m_mapInfos[i].szName))
- {
- nIndex = i;
- break;
- }
- }
- return nIndex;
- }
- public int GetLayerIndexByName(string szName)
- {
- int nIndex = -1;
- for (int i = 0; i < m_nLayerNum; i ++)
- {
- if (szName.Equals(this.m_layerInfos[i].szName))
- {
- nIndex = i;
- break;
- }
- }
- return nIndex;
- }
- /// <summary>
- /// 根据地名得到地名所在的图层名
- /// </summary>
- /// <param name="szName">string 地名名称</param>
- /// <param name="szTblName">string 索引表名称</param>
- /// <returns>图层名称</returns>
- public string GetLayerName(string szName,string szTblName)
- {
- string szTableName = "";
- string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_szDBName +
- ";Persist Security Info=False";
- System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strConnectionString);
- myConnection.Open();
- System.Data.DataSet dataSet = new System.Data.DataSet("临时库");
- System.Data.OleDb.OleDbDataAdapter myDataAdapter;
- string szSQL;
- szSQL = "Select * From " + szTblName + " Where 名称 ='"+szName+"'";
- myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(szSQL,myConnection);
- myDataAdapter.Fill(dataSet,"地名索引");
- System.Data.DataTable indexTbl = dataSet.Tables["地名索引"];
- System.Data.DataRow[] rowsType = indexTbl.Select();
- if (0 == rowsType.Length)
- return "";
- szTableName = rowsType[0]["图层名"].ToString();
- return szTableName;
- }
- public string GetFieldName(string szName)
- {
- string szTable = GetTableName(szName,"地名索引");
- string szFieldName = "单位名称";
- string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_szDBName +
- ";Persist Security Info=False";
- System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strConnectionString);
- myConnection.Open();
- System.Data.DataSet dataSet = new System.Data.DataSet("临时库");
- System.Data.OleDb.OleDbDataAdapter myDataAdapter;
- string szSQL;
- szSQL = "Select * From "+ this.m_mapInfos[this.m_nCurrMapIndex].szMetaTable + " Where 属性表名 ='"+szTable+"'";
- myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(szSQL,myConnection);
- myDataAdapter.Fill(dataSet,"索引");
- System.Data.DataTable indexTbl = dataSet.Tables["索引"];
- System.Data.DataRow[] rowsType = indexTbl.Select();
- if (0 == rowsType.Length)
- return szFieldName;
- szFieldName = rowsType[0]["字段名"].ToString();
- return szFieldName;
- }
- public string GetTableName(string szName, string szTbleName)
- {
- string szTableName = "";
- string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_szDBName +
- ";Persist Security Info=False";
- System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strConnectionString);
- myConnection.Open();
- System.Data.DataSet dataSet = new System.Data.DataSet("临时库");
- System.Data.OleDb.OleDbDataAdapter myDataAdapter;
- string szSQL;
- szSQL = "Select * From " + szTbleName + " Where 名称 ='"+szName+"'";
- myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(szSQL,myConnection);
- myDataAdapter.Fill(dataSet,"地名索引");
- System.Data.DataTable indexTbl = dataSet.Tables["地名索引"];
- System.Data.DataRow[] rowsType = indexTbl.Select();
- if (0 == rowsType.Length)
- return szTableName;
- szTableName = rowsType[0]["属性表名"].ToString();
- return szTableName;
- }
- public void ExecuteSpatial(AxMapObjects2.AxMap map,object shape,MapObjects2.SearchMethodConstants sMode,bool bMPoint)
- {
- for (int i = 0; i < this.m_nLayerNum; i ++)
- {
- if (bMPoint)
- {
- double dScale = CalcScale(map);
- if (dScale > 8000)
- {
- dScale = dScale/10000;
- dScale = dScale / 5000;
- }
- else
- {
- dScale = dScale/10000;
- dScale = dScale / 2500;
- }
- if (m_layerInfos[i].layer.Visible && m_layerInfos[i].bCanSelected)
- m_layerInfos[i].rsSel = m_layerInfos[i].layer.SearchByDistance(shape, dScale,"");
- else
- m_layerInfos[i].rsSel = null;
- }
- else
- {
- if (m_layerInfos[i].layer.Visible && m_layerInfos[i].bCanSelected)
- m_layerInfos[i].rsSel = m_layerInfos[i].layer.SearchShape(shape, sMode,"");
- else
- m_layerInfos[i].rsSel = null;
- }
- }
- }
- public void ClearSelRsts()
- {
- for (int i = 0; i < this.m_nLayerNum; i ++)
- {
- m_layerInfos[i].rsSel = null;
- }
- }
- public void DrawRecordset(AxMapObjects2.AxMap map)
- {
- for (int i = 0; i < this.m_nLayerNum; i ++)
- {
- if (m_layerInfos[i].layer.Visible)
- {
- if (m_layerInfos[i].rsSel != null)
- {
- MapObjects2.Symbol sym = new MapObjects2.Symbol();
- sym.SymbolType = m_layerInfos[i].layer.Symbol.SymbolType;
- sym.Style = m_layerInfos[i].layer.Symbol.Style;
- sym.Size = m_layerInfos[i].layer.Symbol.Size;
- sym.Color = 0xff;
- if (m_layerInfos[i].nCharacterIndex >= 0 && m_layerInfos[i].layer.shapeType == MapObjects2.ShapeTypeConstants.moShapeTypePoint )
- {
- // sym = m_layerInfos[i].layer.Symbol;
- // sym.Color = 0xff;
- sym.SymbolType = MapObjects2.SymbolTypeConstants.moPointSymbol;
- sym.Font.Name = m_layerInfos[i].szFontName;
- sym.Style = 4;
- sym.Size = m_layerInfos[i].layer.Symbol.Size;
- sym.CharacterIndex = (short)m_layerInfos[i].nCharacterIndex;
- }
- m_layerInfos[i].rsSel.MoveFirst();
- while (!m_layerInfos[i].rsSel.EOF)
- {
- map.DrawShape(m_layerInfos[i].rsSel.Fields.Item("Shape").Value,sym);
- m_layerInfos[i].rsSel.MoveNext();
- }
- }
- }
- }
- }
- public void DrawSelectedShape(AxMapObjects2.AxMap map, frmMain frm)
- {
- MapObjects2.Symbol sym = new MapObjects2.Symbol();
- sym.Color = 0xff;
- if (this.m_selectedSymbol == null)
- map.DrawShape(m_selectedFeature,sym );
- else
- {
- double dScale = this.CalcScale(map);
- // if (m_selectedScale > dScale)
- {
- m_selectedSymbol.Size = frm.ReCalcFontSize(m_selectedSymbolSize,dScale);
- map.DrawShape(m_selectedFeature,m_selectedSymbol );
- }
- }
- }
- ///////////////////////////////////////////////////////////////////
- /// 功能:判断图层是否可见
- /// 参数:
- /// 返回值:
- /// ///////////////////////////////////////////////////////////////
- public bool GetLayerVisible(int disp)
- {
- bool bVisible = true;
- int nType = 2;
- string szSubType = "";
- switch ((MapDisp)disp)
- {
- case MapDisp.MO_ALL:
- nType = 1;
- break;
- case MapDisp.MO_HOSPITAL:
- szSubType = "医院";
- break;
- case MapDisp.MO_SCHOOL:
- szSubType = "教育";
- break;
- case MapDisp.MO_SHOP:
- szSubType = "零售";
- break;
- case MapDisp.MO_TOUR:
- szSubType = "旅游";
- break;
- case MapDisp.MO_GAS:
- break;
- case MapDisp.MO_HOTEL:
- szSubType = "住宿";
- break;
- case MapDisp.MO_LIBRAY:
- szSubType = "图书馆";
- nType = 3;
- break;
- case MapDisp.MO_MOVIE:
- szSubType = "影剧院、音乐厅";
- nType = 3;
- break;
- case MapDisp.MO_POST:
- szSubType = "邮政";
- break;
- case MapDisp.MO_RESTAURANT:
- szSubType = "餐饮";
- break;
- case MapDisp.MO_WS:
- szSubType = "dddd";
- break;
- case MapDisp.MO_STATION:
- szSubType = "站点";
- nType = 4;
- break;
- }
- int nCount = 0;
- for (int i = 0; i < this.m_nLayerNum; i ++)
- {
- if (m_layerInfos[i].bCanControl)
- {
- switch (nType)
- {
- case 1:
- nCount ++;
- if (!m_layerInfos[i].bVisible)
- {
- return false;
- }
- break;
- case 2:
- if (szSubType == m_layerInfos[i].szSubType)
- {
- nCount ++;
- if (!m_layerInfos[i].bVisible)
- {
- return false;
- }
- }
- break;
- case 3:
- if (szSubType == m_layerInfos[i].szSubType2)
- {
- nCount ++;
- if (!m_layerInfos[i].bVisible)
- {
- return false;
- }
- }
- break;
- case 4:
- if (szSubType == m_layerInfos[i].szSubType3)
- {
- nCount ++;
- if (!m_layerInfos[i].bVisible)
- {
- return false;
- }
- }
- break;
- }
- }
- }
- if (0 == nCount)
- bVisible = false;
- return bVisible;
- }
- ///////////////////////////////////////////////////////////////////
- /// 功能:设置图层是否可见
- /// 参数:
- /// 返回值:
- /// ///////////////////////////////////////////////////////////////
- public void SetLayerVisible(int disp, bool bVisible, double dScale)
- {
- int nType = 2;
- string szSubType = "";
- switch ((MapDisp)disp)
- {
- case MapDisp.MO_ALL:
- nType = 1;
- break;
- case MapDisp.MO_HOSPITAL:
- szSubType = "医院";
- break;
- case MapDisp.MO_SCHOOL:
- szSubType = "教育";
- break;
- case MapDisp.MO_SHOP:
- szSubType = "零售";
- break;
- case MapDisp.MO_TOUR:
- szSubType = "旅游";
- break;
- case MapDisp.MO_GAS:
- break;
- case MapDisp.MO_HOTEL:
- szSubType = "住宿";
- break;
- case MapDisp.MO_LIBRAY:
- szSubType = "图书馆";
- nType = 3;
- break;
- case MapDisp.MO_MOVIE:
- szSubType = "影剧院、音乐厅";
- nType = 3;
- break;
- case MapDisp.MO_POST:
- szSubType = "邮政";
- break;
- case MapDisp.MO_RESTAURANT:
- szSubType = "餐饮";
- break;
- case MapDisp.MO_WS:
- szSubType = "厕所";
- break;
- case MapDisp.MO_STATION:
- szSubType = "站点";
- nType = 4;
- break;
- }
- for (int i = 0; i < this.m_nLayerNum; i ++)
- {
- if (m_layerInfos[i].bCanControl)
- {
- switch (nType)
- {
- case 1:
- if (!bVisible)
- {
- m_layerInfos[i].bVisible = bVisible;
- m_layerInfos[i].layer.Visible = m_layerInfos[i].bVisible;
- }
- else
- {
- m_layerInfos[i].bVisible = bVisible;
- if (m_layerInfos[i].dScale > dScale)
- m_layerInfos[i].layer.Visible = m_layerInfos[i].bVisible;
- }
- break;
- case 2:
- if (szSubType == m_layerInfos[i].szSubType)
- {
- if (!bVisible)
- {
- m_layerInfos[i].bVisible = bVisible;
- m_layerInfos[i].layer.Visible = m_layerInfos[i].bVisible;
- }
- else
- {
- m_layerInfos[i].bVisible = bVisible;
- if (m_layerInfos[i].dScale > dScale)
- m_layerInfos[i].layer.Visible = m_layerInfos[i].bVisible;
- }
- }
- break;
- case 3:
- if (szSubType == m_layerInfos[i].szSubType2)
- {
- if (!bVisible)
- {
- m_layerInfos[i].bVisible = bVisible;
- m_layerInfos[i].layer.Visible = m_layerInfos[i].bVisible;
- }
- else
- {
- m_layerInfos[i].bVisible = bVisible;
- if (m_layerInfos[i].dScale > dScale)
- m_layerInfos[i].layer.Visible = m_layerInfos[i].bVisible;
- }
- }
- break;
- case 4:
- if (szSubType == m_layerInfos[i].szSubType3)
- {
- if (!bVisible)
- {
- m_layerInfos[i].bVisible = bVisible;
- m_layerInfos[i].layer.Visible = m_layerInfos[i].bVisible;
- }
- else
- {
- m_layerInfos[i].bVisible = bVisible;
- if (m_layerInfos[i].dScale > dScale)
- m_layerInfos[i].layer.Visible = m_layerInfos[i].bVisible;
- }
- }
- break;
- }
- }
- }
- }
- private void SubGussFs(ref double X,ref double Y,double B,double L,int nCenterLongi)
- {
- //高斯投影分带
- int nzonenum;
- if(nCenterLongi==0)
- {
- nzonenum = (int)L/6+1;
- nCenterLongi = nzonenum*6-3;
- }
- else
- nzonenum = (int)nCenterLongi/6+1;
- //以弧度为单位的经纬度数值
- double rB = B/180*3.1415926;
- double rL = (L-nCenterLongi)/180*3.1415926; //同时计算了中央经线
- //1980坐标系参数
- const double a = 6378245.00; //长轴
- const double b = 6356863.50; //短轴
- // const double alpha = 0.0033523299; //扁率
- double sqre1 = (a*a-b*b)/(a*a); //第一偏心率平方
- // double sqre2 = (a*a-b*b)/(b*b); //第二偏心率平方
- //B:纬度
- //L:精度
- //子午圈曲率半径
- double sinb = Math.Sin(rB);
- double cosb = Math.Cos(rB);
- double M = a*(1-sqre1)/(1-sqre1*sinb*sinb)/Math.Sqrt(1-sqre1*sinb*sinb);
- //卯酉圈曲率半径
- double N = a/Math.Sqrt(1-sqre1*sinb*sinb);
- double sqrita = N/M-1;
- //该纬度点到赤道的子午线弧长
- double s = a*(1-sqre1)*(1.00505117739*rB-0.00506237764/2*Math.Sin(2*rB)+
- 0.0000106245/4*Math.Sin(4*rB)-0.00000002081/6*Math.Sin(6*rB));
- double tanb = Math.Tan(rB);
- X = s+rL*rL*N/2*sinb*cosb+rL*rL*rL*rL*N/24*sinb*cosb*cosb*cosb*(5-tanb*tanb+9*sqrita*sqrita+4*sqrita);
- Y = rL*N*cosb+rL*rL*rL*N/6*cosb*cosb*cosb*(1-tanb*tanb+sqrita)+
- rL*rL*rL*rL*rL*N/120*cosb*cosb*cosb*cosb*cosb*(5-18*tanb*tanb+tanb*tanb*tanb*tanb);
- Y = Y+500000+nzonenum*1.0e+6;
- }
- private void SubGussFs(double X,double Y,double L0,ref double B,ref double L)
- {
- double p=57.29577951472;
- const double a=6.378245000e+06;
- const double e2=0.00669342162297;
- const double e12=0.00673852541468;
- const double c0=0.157046064172e-06;
- const double c1=0.005051773759;
- const double c2=0.000029837302;
- const double c3=0.000000238189;
- double bf0=c0*X;
- double bf0c=Math.Cos(bf0);
- double bf0s=Math.Sin(bf0);
- double bf=bf0+bf0c*(c1*bf0s-c2*Math.Pow(bf0s,3)+c3*Math.Pow(bf0s,5));
- double bt=Math.Tan(bf);
- double bc=Math.Cos(bf);
- double bs=Math.Sin(bf);
- double bi=e12*Math.Pow(bc,2);
- double v2=1.0e+0+bi;
- double bn=a/Math.Sqrt(1.0-e2*Math.Pow(bs,2));
- double yn=Y/bn;
- /*calculate lantitude b */
- double b1=-v2*bt*Math.Pow(yn,2)/2.0;
- double b2=-(5.0+3.0*Math.Pow(bt,2)+bi-9.0*bi*Math.Pow(bt,2))*b1*Math.Pow(yn,2)/12.0;
- double b3=(61.0+90.0*Math.Pow(bt,2)+45.0*Math.Pow(bt,4))*b1*Math.Pow(yn,4)/360.0;
- B = bf+b1+b2+b3;
- B = B * p;
- /* calculate the longitude l */
- double l1=yn/bc;
- double l2=-(1.0+2.0*Math.Pow(bt,2)+bi)*l1*Math.Pow(yn,2)/6.0;
- double l3=(5.0+28.0*Math.Pow(bt,2)+24.0*Math.Pow(bt,4)+6.0*bi
- +8.0*bi*Math.Pow(bt,2))*l1*Math.Pow(yn,4)/120.0;
- L=l1+l2+l3;
- L=L * p;
- L=L + L0;
- if(L > 360.0)
- L=L - 360.0;
- }
- private void CalGuassToLB(double dX, double dY, ref double dLongitude, ref double dLatitude)
- {
- // TODO: Add your dispatch handler code here
- double L0;
- int nZoonNum;
- nZoonNum = (int)(dY/(1.0E+6));
- L0 = nZoonNum * 6-3;
- dY = dY - nZoonNum*1.0E+6;
- SubGussFs(dX,dY-500000,L0,ref dLatitude,ref dLongitude);
- dLongitude = dLongitude + nZoonNum*6 - 3 ;
- }
- private void CalGuassFromLB(double dLongitude, double dLatitude, ref double dX, ref double dY, long nCenterL)
- {
- // TODO: Add your dispatch handler code here
- int CenterL = (int)nCenterL;
- SubGussFs(ref dX,ref dY,dLatitude,dLongitude,CenterL);
- nCenterL = (long)CenterL;
- }
- private double CalcLenght(MPoint[] pt,int nSize)
- {
- double dLength = 0;
- double x1=0,x2=0,y1=0,y2=0;
- int nCenterL = ((int)(pt[0].x)/6+1)*6-3;
- for(int i=0;i<nSize-1;i++)
- {
- CalGuassFromLB(pt[i].x, pt[i].y, ref x1, ref y1, nCenterL);
- CalGuassFromLB(pt[i+1].x, pt[i+1].y, ref x2, ref y2, nCenterL);
- dLength += Math.Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
- }
- return dLength;
- }
- private double CalcArea(MPoint[] pt,int nSize)
- {
- double dArea = 0;
- double x1=0,x2=0,y1=0,y2=0;
- int nCenterL = ((int)(pt[0].x)/6+1)*6-3;
- for(int i=0;i<nSize-1;i++)
- {
- CalGuassFromLB(pt[i].x, pt[i].y, ref x1, ref y1, nCenterL);
- CalGuassFromLB(pt[i+1].x, pt[i+1].y, ref x2, ref y2, nCenterL);
- dArea += (x2-x1)*(y1+y2)/2;
- }
- if (dArea < 0)
- dArea = 0 - dArea;
- return dArea;
- }
- public double GetLineLength(MapObjects2.Line line)
- {
- MPoint[] pt;
- int nPtCount;
- // double dLength;
- MapObjects2.Points pts;
- pts = (MapObjects2.Points)line.Parts.Item(0);
- nPtCount = pts.Count;
- if (nPtCount < 2)
- return 0.0;
- pt = new MPoint[nPtCount];
- for (int i = 0; i < nPtCount; i ++)
- {
- MapObjects2.Point point = (MapObjects2.Point)pts.Item(i);
- pt[i] = new MPoint();
- pt[i].x = point.X;
- pt[i].y = point.Y;
- }
- return CalcLenght(pt, nPtCount);
- }
- public double GetPolygonLength(MapObjects2.Polygon poly)
- {
- MPoint[] pt;
- int nPtCount;
- MapObjects2.Points pts;
- pts = (MapObjects2.Points)poly.Parts.Item(0);
- nPtCount = pts.Count;
- if (nPtCount < 2)
- return 0.0;
- pt = new MPoint[nPtCount+1];
- for (int i = 0; i < nPtCount; i ++)
- {
- MapObjects2.Point point = (MapObjects2.Point)pts.Item(i);
- pt[i] = new MPoint();
- pt[i].x = point.X;
- pt[i].y = point.Y;
- }
- pt[nPtCount] = new MPoint();
- pt[nPtCount].x = pt[0].x;
- pt[nPtCount].y = pt[0].y;
- return CalcLenght(pt, nPtCount+1);
- }
- public double GetPolygonArea(MapObjects2.Polygon poly)
- {
- MPoint[] pt;
- int nPtCount;
- MapObjects2.Points pts;
- pts = (MapObjects2.Points)poly.Parts.Item(0);
- nPtCount = pts.Count;
- if (nPtCount < 3)
- return 0.0;
- pt = new MPoint[nPtCount+1];
- for (int i = 0; i < nPtCount; i ++)
- {
- MapObjects2.Point point = (MapObjects2.Point)pts.Item(i);
- pt[i] = new MPoint();
- pt[i].x = point.X;
- pt[i].y = point.Y;
- }
- pt[nPtCount] = new MPoint();
- pt[nPtCount].x = pt[0].x;
- pt[nPtCount].y = pt[0].y;
- return CalcArea(pt, nPtCount+1);
- }
- private void CreatePolygon(double dCenterX,double dCenterY,double dR,ref MPoint[] pPoints,ref int nSize)
- {
- long nCenterL = ((long)(dCenterX)/6+1)*6-3;
- double ncx=0.0,ncy=0.0;
- CalGuassFromLB(dCenterX, dCenterY, ref ncx, ref ncy, nCenterL);
- const int nPointNumber = 20;
- const double PI = 3.1415926;
- pPoints = new MPoint[nPointNumber+1];
- double x1,y1;
- double x2=0.0,y2=0.0;
- for(int i=0;i<nPointNumber;i++)
- {
- x1 = dCenterX + dR * Math.Cos(2*PI/nPointNumber*i);
- y1 = dCenterY + dR * Math.Sin(2*PI/nPointNumber*i);
- CalGuassToLB(x1, y1, ref x2, ref y2);
- pPoints[i].x = x2;
- pPoints[i].y = y2;
- }
- pPoints[nPointNumber] = pPoints[0];
- nSize = nPointNumber + 1;
- }
- public void CreatePolygon(MapObjects2.Polygon poly, double x, double y, double dLength)
- {
- MPoint[] pts = null;
- int nSize = 0;
- CreatePolygon(x,y,dLength,ref pts,ref nSize);
- poly.Parts.Count = 1;
- MapObjects2.Points points = (MapObjects2.Points)poly.Parts.Item(0);
- points.Count = nSize;
- for (int i = 0; i < nSize; i ++)
- {
- points.Item(i).X = pts[i].x;
- points.Item(i).Y = pts[i].y;
- }
- }
- public long SearchByDistance(double dX, double dY,double dDistance, System.Windows.Forms.ListBox listBox)
- {
- bool bClosest = false;
- if (dDistance <= 0.0)
- {
- bClosest = true;
- dDistance = 1000000000;
- }
- listBox.DataSource = null;
- listBox.Items.Clear();
- for (int i = 0; i < this.m_nLayerNum; i ++)
- {
- if (this.m_layerInfos[i].bSelected
- && this.m_layerInfos[i].layer.shapeType == MapObjects2.ShapeTypeConstants.moShapeTypePoint)
- {
- MapObjects2.Recordset rs = null;
- string szPlaceName = "";
- double dMinDist = -1.0;
- double dDist = 0.0;
- rs = this.m_layerInfos[i].layer.Records;
- if (rs != null)
- {
- rs.MoveFirst();
- while (!rs.EOF)
- {
- MPoint[] pts=new MPoint[2];
- MapObjects2.Point pt;
- pt = (MapObjects2.Point)rs.Fields.Item("shape").Value;
- pts[0] = new MPoint();
- pts[0].x = dX;
- pts[0].y = dY;
- pts[1] = new MPoint();
- pts[1].x = pt.X ;
- pts[1].y = pt.Y;
- dDist = this.CalcLenght(pts, 2);
- if (dDistance >= this.CalcLenght(pts, 2))
- {
- if (bClosest)
- {
- //查找最近
- string szTemp = rs.Fields.Item("名称").Value.ToString();
- if (szTemp == "")
- continue;
- if (((dMinDist < 0) || (dMinDist > dDist)) && (!this.m_szPlaceName.Equals(szTemp)))
- {
- dMinDist = dDist;
- szPlaceName = szTemp;
- }
- }
- else
- {
- listBox.Items.Add(rs.Fields.Item("名称").Value.ToString());
- }
- }
- rs.MoveNext();
- }
- if (bClosest && (szPlaceName != "") && (!this.m_szPlaceName.Equals(szPlaceName)) )
- listBox.Items.Add(szPlaceName);
- }
- }
- else
- {
- this.m_layerInfos[i].rsSel = null;
- }
- }
- return listBox.Items.Count;
- }
- public bool IsImage(string szName)
- {
- string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_szDBName +
- ";Persist Security Info=False";
- System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strConnectionString);
- myConnection.Open();
- System.Data.DataSet dataSet = new System.Data.DataSet("临时库");
- System.Data.OleDb.OleDbDataAdapter myDataAdapter;
- string szSQL;
- szSQL = "Select * From 多媒体索引 Where 名称 ='"+szName+"'";
- myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(szSQL,myConnection);
- myDataAdapter.Fill(dataSet,"多媒体索引");
- System.Data.DataTable indexTbl = dataSet.Tables["多媒体索引"];
- System.Data.DataRow[] rowsType = indexTbl.Select();
- if (0 == rowsType.Length)
- return false;
- return true;
- }
- public MLine CreateLine(MapObjects2.Line moline)
- {
- MLine line = new MLine();
- MapObjects2.Points pts = (MapObjects2.Points)moline.Parts.Item(0);
- line.nPointNumber = pts.Count;
- line.pPoint = new MPoint[line.nPointNumber];
- for (int i = 0; i < line.nPointNumber; i++)
- {
- MapObjects2.Point pt = (MapObjects2.Point)pts.Item(i);
- line.pPoint[i] = new MPoint();
- line.pPoint[i].x = pt.X;
- line.pPoint[i].y = pt.Y;
- }
- return line;
- }
- public MapObjects2.Point GetPoint(string szName)
- {
- MapObjects2.Point pt = null;
- string szLayer = GetLayerName(szName,"地名索引");
- string szTable = GetTableName(szName,"地名索引");
- if ("" == szTable)
- {
- return null;
- }
- int nIndex = GetLayerIndexByName(szTable);
- if (nIndex < 0)
- return null;
- // if (!m_layerInfos[nIndex].layer.Visible)
- // {
- // return null;
- // }
- //
- MapObjects2.Recordset rs;
- rs = m_layerInfos[nIndex].layer.SearchExpression("名称 like '" + szName + "'");
- if (rs != null)
- {
- rs.MoveFirst();
- if (!rs.EOF)
- {
- switch (m_layerInfos[nIndex].layer.shapeType)
- {
- case MapObjects2.ShapeTypeConstants.moShapeTypePoint:
- {
- pt = (MapObjects2.Point)rs.Fields.Item("shape").Value;
- }
- break;
- case MapObjects2.ShapeTypeConstants.moShapeTypeLine:
- {
- MapObjects2.Line line;
- line = (MapObjects2.Line)(rs.Fields.Item("shape").Value);
- pt = line.Extent.Center;
- }
- break;
- case MapObjects2.ShapeTypeConstants.moShapeTypePolygon:
- {
- MapObjects2.Polygon poly;
- poly = (MapObjects2.Polygon)(rs.Fields.Item("shape").Value);
- pt = poly.Extent.Center;
- }
- break;
- }
- }
- }
- return pt;
- }
- public MapObjects2.Line GetLine(string szName)
- {
- MapObjects2.Line line = null;
- string szLayer = GetLayerName(szName,"地名索引");
- string szTable = GetTableName(szName,"地名索引");
- if ("" == szTable)
- {
- return null;
- }
- int nIndex = GetLayerIndexByName(szTable);
- if (nIndex < 0)
- return null;
- // if (!m_layerInfos[nIndex].layer.Visible)
- // {
- // return null;
- // }
- MapObjects2.Recordset rs;
- rs = m_layerInfos[nIndex].layer.SearchExpression("名称 like '" + szName + "'");
- if (rs != null)
- {
- rs.MoveFirst();
- if (!rs.EOF)
- {
- switch (m_layerInfos[nIndex].layer.shapeType)
- {
- case MapObjects2.ShapeTypeConstants.moShapeTypePoint:
- return line;
- case MapObjects2.ShapeTypeConstants.moShapeTypeLine:
- line = (MapObjects2.Line)(rs.Fields.Item("shape").Value);
- break;
- case MapObjects2.ShapeTypeConstants.moShapeTypePolygon:
- return line;
- default:
- break;
- }
- }
- }
- return line;
- }
- public MPoint FromMapPoint(AxMapObjects2.AxMap map, double x,double y)
- {
- MPoint pt = new MPoint();
- double dW = System.Math.Abs( map.Extent.Right - map.Extent.Left);
- double dH = System.Math.Abs(map.Extent.Top-map.Extent.Bottom) ;
- double dRatio = 1.0;
- double dOrgX=0;
- double dOrgY=0;
- if(map.Width/dW>map.Height/dH) //横向居中
- {
- dRatio = map.Height /dH;
- dOrgX = (map.Width-dW*dRatio)/2;
- }
- else if(map.Width/dW<map.Height/dH) //纵向居中
- {
- dRatio = map.Width /dW;
- dOrgY = (map.Height-dH*dRatio)/2;
- }
- pt.x = (x-map.Extent.Left)*dRatio+dOrgX;
- pt.y = map.Height-(y-map.Extent.Bottom)*dRatio+dOrgY;
- return pt;
- }
- public void DrawLine( AxMapObjects2.AxMap map)
- {
- if (m_drawLine == null)
- return;
- System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd((System.IntPtr)map.hWnd);
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red,3);
- for (int i = 0; i < m_drawLine.Length; i ++)
- {
- System.Drawing.Point[] pts = new System.Drawing.Point[m_drawLine[i].nPointNumber];
- for (int j = 0; j < m_drawLine[i].nPointNumber; j ++)
- {
- MPoint pt =FromMapPoint(map,m_drawLine[i].pPoint[j].x,m_drawLine[i].pPoint[j].y);
- pts[j] = new System.Drawing.Point((int)pt.x,(int)pt.y );
- }
- g.DrawLines(pen,pts);
- }
- }
- /// //////////////////////////////////////////////////////////////////////////////////////////////
- /// 功能:计算地图比例尺
- /// 参数:AxMapObjects2.AxMap map
- /// 返回值:地图比例尺
- /// /////////////////////////////////////////////////////////////////////////////////////////////
- public double CalcScale(AxMapObjects2.AxMap map)
- {
- System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd((System.IntPtr)map.hWnd);
- MPoint[] pts = new MPoint[2];
- pts[0] = new MPoint();
- pts[0].x = map.Extent.Left;
- pts[0].y = map.Extent.Top;
- pts[1] = new MPoint();
- pts[1].x = map.Extent.Right;
- pts[1].y = map.Extent.Top;
- double dLen1 = this.CalcLenght(pts,2);
- double dLen2 = map.Width / g.DpiX * 2.54 /100;
- return dLen1 / dLen2;
- }
- /// //////////////////////////////////////////////////////////////////////////////////////////////
- /// 功能:计算地图比例尺
- /// 参数:AxMapObjects2.Rectangle extent
- /// 返回值:地图比例尺
- /// /////////////////////////////////////////////////////////////////////////////////////////////
- public double CalcScale(AxMapObjects2.AxMap map,MapObjects2.Rectangle extent)
- {
- System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd((System.IntPtr)map.hWnd);
- MPoint[] pts = new MPoint[2];
- pts[0] = new MPoint();
- pts[0].x = extent.Left;
- pts[0].y = extent.Top;
- pts[1] = new MPoint();
- pts[1].x = extent.Right;
- pts[1].y = extent.Top;
- double dLen1 = this.CalcLenght(pts,2);
- double dLen2 = map.Width / g.DpiX * 2.54 /100;
- return dLen1 / dLen2;
- }
- /// //////////////////////////////////////////////////////////////////////////////////////
- //功能:得到公交线路上车站
- //参数:[in]object node 乘车路线结构
- // [in]int nIndex 第几次换乘
- // [out]Buses buses 车站数组
- // [out]int nCount 车站数目
- //返回值:true 成功
- /// //////////////////////////////////////////////////////////////////////////////////////
- public bool GetStation(object node, int nIndex,Buses buses, ref int nCount)
- {
- PathNode line = (PathNode)node;
- int nOrder1,nOrder2;
- nOrder1 = GetStationOrder(line.szRoutineName[nIndex],line.szFromStationName[nIndex]);
- nOrder2 = GetStationOrder(line.szRoutineName[nIndex],line.szToStationName[nIndex]);
- if ((nOrder1 < 0) || (nOrder2 < 0))
- return false;
- string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_szDBName +
- ";Persist Security Info=False";
- System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strConnectionString);
- myConnection.Open();
- System.Data.DataSet dataSet = new System.Data.DataSet("临时库");
- System.Data.OleDb.OleDbDataAdapter myDataAdapter;
- string szSQL;
- szSQL = "Select * From 公交车站路线 " + " Where 线路名 ='"+line.szRoutineName[nIndex]+"' And 顺序 Between "+nOrder1.ToString() +" And "+nOrder2.ToString() ;
- myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(szSQL,myConnection);
- myDataAdapter.Fill(dataSet,"索引");
- System.Data.DataTable indexTbl = dataSet.Tables["索引"];
- System.Data.DataRow[] rowsType = indexTbl.Select();
- if (0 == rowsType.Length)
- return false;
- foreach (System.Data.DataRow myRow in rowsType)
- {
- buses.pts[nCount] = new MPoint();
- string szStation = "";
- szStation = myRow["站名"].ToString();
- if (!GetStationPt(szStation,buses.pts[nCount]))
- {
- buses.pts[nCount].x = -1;
- buses.pts[nCount].y = -1;
- }
- nCount ++;
- }
- return true;
- }
- /// //////////////////////////////////////////////////////////////////////////////////////
- //功能:得到公交线路上车站
- //参数:[in]string szLineName 公交线路名
- // [out]Buses buses 车站数组
- // [out]int nCount 车站数目
- //返回值:true 成功
- /// //////////////////////////////////////////////////////////////////////////////////////
- public bool GetStation(string szLineName,Buses buses, ref int nCount)
- {
- string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_szDBName +
- ";Persist Security Info=False";
- System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strConnectionString);
- myConnection.Open();
- System.Data.DataSet dataSet = new System.Data.DataSet("临时库");
- System.Data.OleDb.OleDbDataAdapter myDataAdapter;
- string szSQL;
- szSQL = "Select * From 公交车站路线 " + " Where 线路名 ='"+szLineName +"'" ;
- myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(szSQL,myConnection);
- myDataAdapter.Fill(dataSet,"索引");
- System.Data.DataTable indexTbl = dataSet.Tables["索引"];
- System.Data.DataRow[] rowsType = indexTbl.Select();
- if (0 == rowsType.Length)
- return false;
- foreach (System.Data.DataRow myRow in rowsType)
- {
- buses.pts[nCount] = new MPoint();
- string szStation = "";
- szStation = myRow["站名"].ToString();
- if (!GetStationPt(szStation,buses.pts[nCount]))
- {
- buses.pts[nCount].x = -1;
- buses.pts[nCount].y = -1;
- }
- nCount ++;
- }
- return true;
- }
- public MapObjects2.MapLayer GetLayerByName(string szName)
- {
- int i;
- for (i = 0; i < this.m_nLayerNum - 1; i ++)
- {
- if (this.m_layerInfos[i].szName.Equals(szName))
- break;
- }
- if (i == this.m_nLayerNum)
- return null; ;
- return m_layerInfos[i].layer;
- }
- public bool IsStation(AxMapObjects2.AxMap map,string szName)
- {
- int i;
- for (i = 0; i < this.m_nLayerNum ; i ++)
- {
- if (this.m_layerInfos[i].szName.Equals(CEnvironment.BUSSTATION_LAYERNAME))
- break;
- }
- if (i == this.m_nLayerNum)
- return false;
- MapObjects2.MapLayer ly = m_layerInfos[i].layer;
- MapObjects2.Recordset rs = ly.SearchExpression("名称 like '"+szName +"'");;
- if (rs == null)
- return false;
- rs.MoveFirst();
- if (rs.EOF)
- return false;
- return true;
- }
- public bool IsBusLine(AxMapObjects2.AxMap map,string szName)
- {
- int i;
- for (i = 0; i < this.m_nLayerNum; i ++)
- {
- if (this.m_layerInfos[i].szName.Equals(CEnvironment.BUSLINE_LAYERNAME))
- break;
- }
- if (i == this.m_nLayerNum)
- return false;
- MapObjects2.MapLayer ly = m_layerInfos[i].layer;
- MapObjects2.Recordset rs = ly.SearchExpression("名称 like '"+szName +"'");;
- if (rs == null)
- return false;
- rs.MoveFirst();
- if (rs.EOF)
- return false;
- return true;
- }
- /// ////////////////////////////////////////////////////////////////////////////////////////////
- /// 功能:得到给定的车站名的地理坐标
- /// 参数:[in]string szLineName 公交线路名
- /// [out]MPoint pt 公交车的地理坐标
- /// 返回值:false 失败
- /// ////////////////////////////////////////////////////////////////////////////////////////////
- public bool GetStationPt(string szStationName, MPoint pt)
- {
- string szName = szStationName;
- string szLayer = GetLayerName(szName,"地名索引");
- string szTable = GetTableName(szName,"地名索引");
- int nIndex = GetLayerIndexByName(szTable);
- if (nIndex < 0)
- return false;
- MapObjects2.Recordset rs;
- rs = m_layerInfos[nIndex].layer.SearchExpression("名称 like '"+szName +"'");
- if (rs != null)
- {
- rs.MoveFirst();
- if (!rs.EOF)
- {
- MapObjects2.Point pt1;
- pt1 = (MapObjects2.Point)rs.Fields.Item("shape").Value;
- pt.x = pt1.X;
- pt.y = pt1.Y;
- return true;
- }
- }
- return false;
- }
- /// ////////////////////////////////////////////////////////////////////////////////////////////
- /// 功能:得到给定的车站名在给定的公交线路上的车站顺序(如第2站)
- /// 参数:[in]string szLineName 公交线路名
- /// [in]string szStationName 公交车站名
- /// 返回值:> 0 车站顺序,否则失败
- /// ////////////////////////////////////////////////////////////////////////////////////////////
- public int GetStationOrder(string szLineName, string szStationName)
- {
- string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_szDBName +
- ";Persist Security Info=False";
- System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strConnectionString);
- myConnection.Open();
- System.Data.DataSet dataSet = new System.Data.DataSet("临时库");
- System.Data.OleDb.OleDbDataAdapter myDataAdapter;
- string szSQL;
- szSQL = "Select * From 公交车站路线 " + " Where 线路名 ='"+szLineName+"' And 站名 ='"+szStationName+" '";
- myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(szSQL,myConnection);
- myDataAdapter.Fill(dataSet,"索引");
- System.Data.DataTable indexTbl = dataSet.Tables["索引"];
- System.Data.DataRow[] rowsType = indexTbl.Select();
- if (0 == rowsType.Length)
- return -1;
- return (int)rowsType[0]["顺序"];
- }
- }
- }