Exemple #1
0
        private bool IsLocationInEnvelopArea(Framework.Data.Location loc, Framework.Data.AreaInfo area)
        {
            bool result = false;

            //point in envelope of area
            if (loc.Lat >= area.MinLat && loc.Lat <= area.MaxLat && loc.Lon >= area.MinLon && loc.Lon <= area.MaxLon)
            {
                bool releasePoly = area.Polygons == null;
                if (area.Polygons == null)
                {
                    GetPolygonOfArea(area);
                }
                if (area.Polygons != null)
                {
                    foreach (var r in area.Polygons)
                    {
                        //point in envelope of polygon
                        if (loc.Lat >= r.MinLat && loc.Lat <= r.MaxLat && loc.Lon >= r.MinLon && loc.Lon <= r.MaxLon)
                        {
                            result = true;
                            break;
                        }
                    }
                }
                if (releasePoly)
                {
                    area.Polygons = null;
                }
            }
            return(result);
        }
Exemple #2
0
 private void button1_Click(object sender, EventArgs e)
 {
     this.Cursor = Cursors.WaitCursor;
     try
     {
         Framework.Data.AreaInfo ai = comboBoxAreaName.SelectedItem as Framework.Data.AreaInfo;
         if (ai != null)
         {
             Utils.GeometrySupport.Instance.GetPolygonOfArea(ai);
             if (ai.Polygons != null)
             {
                 int count = 0;
                 while (count < ai.Polygons.Count)
                 {
                     addPolygons(ai, ai.Polygons.Skip(count).Take(20).ToList());
                     count += 100;
                 }
                 executeScript(string.Format("zoomToBounds({0}, {1}, {2}, {3})", ai.MinLat.ToString(CultureInfo.InvariantCulture), ai.MinLon.ToString(CultureInfo.InvariantCulture), ai.MaxLat.ToString(CultureInfo.InvariantCulture), ai.MaxLon.ToString(CultureInfo.InvariantCulture)));
             }
         }
     }
     catch
     {
     }
     this.Cursor = Cursors.Default;
 }
Exemple #3
0
 public void GetPolygonOfArea(Framework.Data.AreaInfo area)
 {
     PreLoadAreaInfo();
     if (area.Polygons == null)
     {
         Framework.Data.AreaInfo ainf = (from ai in _cachedAreaInfo where ai.ID == area.ID select ai).FirstOrDefault();
         if (ainf != null)
         {
             try
             {
                 area.Polygons = new List <Framework.Data.Polygon>();
                 using (Utils.DBCon dbcon = new Utils.DBConComSqlite(_databaseFilename))
                 {
                     int actPolyId = -1;
                     Framework.Data.Polygon polg = null;
                     DbDataReader           dr   = dbcon.ExecuteReader(string.Format("select * from poly where areainfoid={0} order by id, position", area.ID));
                     while (dr.Read())
                     {
                         int polyId = (int)dr["id"];
                         if (actPolyId != polyId)
                         {
                             polg = new Framework.Data.Polygon();
                             area.Polygons.Add(polg);
                             actPolyId = polyId;
                         }
                         polg.AddLocation(new Framework.Data.Location((double)dr["lat"], (double)dr["lon"]));
                     }
                 }
             }
             catch
             {
             }
         }
     }
 }
Exemple #4
0
        private void assignRegionsThreadMethod()
        {
            try
            {
                using (Utils.ProgressBlock prog = new Utils.ProgressBlock(_plugin, STR_ASSIGNINGREGION, STR_ASSIGNINGREGION, _gcList.Count, 0, true))
                {
                    List <Framework.Data.AreaInfo> areasFilter = Utils.GeometrySupport.Instance.GetAreasByLevel(_level);
                    if (areasFilter != null && areasFilter.Count > 0)
                    {
                        int index = 0;
                        foreach (var gc in _gcList)
                        {
                            List <Framework.Data.AreaInfo> areas = Utils.GeometrySupport.Instance.GetAreasOfLocation(new Framework.Data.Location(gc.Lat, gc.Lon), areasFilter);
                            if (areas != null && areas.Count > 0)
                            {
                                Framework.Data.AreaInfo ai = areas[0];
                                if (_prefix.Length > 0)
                                {
                                    ai = (from g in areas where g.Name.StartsWith(_prefix) select g).FirstOrDefault();
                                }
                                if (ai != null)
                                {
                                    switch (_level)
                                    {
                                    case Framework.Data.AreaType.Country:
                                        gc.Country = ai.Name;
                                        break;

                                    case Framework.Data.AreaType.State:
                                        gc.State = ai.Name;
                                        break;

                                    case Framework.Data.AreaType.Municipality:
                                        gc.Municipality = ai.Name;
                                        break;

                                    case Framework.Data.AreaType.City:
                                        gc.City = ai.Name;
                                        break;
                                    }
                                }
                            }
                            index++;
                            if (index % 50 == 0)
                            {
                                if (!prog.UpdateProgress(STR_ASSIGNINGREGION, STR_ASSIGNINGREGION, _gcList.Count, index))
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }
Exemple #5
0
 public void GetPolygonOfArea(Framework.Data.AreaInfo area)
 {
     foreach (Framework.Interfaces.IGeometry g in _geometryPlugins)
     {
         g.GetPolygonOfArea(area);
         if (area.Polygons != null)
         {
             break;
         }
     }
 }
 public void GetPolygonOfArea(Framework.Data.AreaInfo area)
 {
     foreach (var sf in _shapeFiles)
     {
         sf.GetPolygonOfArea(area);
         if (area.Polygons != null)
         {
             break;
         }
     }
 }
Exemple #7
0
        internal bool processGpxFile(Framework.Data.AreaType level, string f, Framework.Data.AreaInfo parent)
        {
            PreLoadAreaInfo();
            bool result = false;

            try
            {
            }
            catch
            {
            }
            return(result);
        }
Exemple #8
0
        private void comboBoxAreaName_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonDelete.Enabled = (comboBoxAreaName.SelectedIndex >= 0);
            comboBoxParentOfArea.Items.Clear();
            Framework.Data.AreaInfo ai = comboBoxAreaName.SelectedItem as Framework.Data.AreaInfo;
            if (ai != null)
            {
                List <Framework.Data.AreaInfo> ail2 = null;
                switch (ai.Level)
                {
                case Framework.Data.AreaType.City:
                    ail2 = _ownerPlugin.GetAreasByLevel(Framework.Data.AreaType.Municipality);
                    break;

                case Framework.Data.AreaType.Municipality:
                    ail2 = _ownerPlugin.GetAreasByLevel(Framework.Data.AreaType.State);
                    break;

                case Framework.Data.AreaType.State:
                    ail2 = _ownerPlugin.GetAreasByLevel(Framework.Data.AreaType.Country);
                    break;

                case Framework.Data.AreaType.Other:
                    ail2 = _ownerPlugin.GetAreasByLevel(Framework.Data.AreaType.Country);
                    ail2.AddRange(_ownerPlugin.GetAreasByLevel(Framework.Data.AreaType.State));
                    ail2.AddRange(_ownerPlugin.GetAreasByLevel(Framework.Data.AreaType.Municipality));
                    ail2.AddRange(_ownerPlugin.GetAreasByLevel(Framework.Data.AreaType.Other));
                    break;
                }
                comboBoxParentOfArea.Items.Add("-- none --");
                if (ail2 != null)
                {
                    comboBoxParentOfArea.Items.AddRange((from a in ail2 select a).ToArray());
                    if (ai.ParentID != null)
                    {
                        comboBoxParentOfArea.SelectedItem = (from a in ail2 where a.ID == ai.ParentID select a).FirstOrDefault();
                    }
                    else
                    {
                        comboBoxParentOfArea.SelectedIndex = 0;
                    }
                }
                else
                {
                    comboBoxParentOfArea.SelectedIndex = 0;
                }
            }
        }
Exemple #9
0
        private void PreLoadAreaInfo()
        {
            if (_cachedAreaInfo == null)
            {
                _cachedAreaInfo = new List <Framework.Data.AreaInfo>();

                try
                {
                    using (Utils.DBCon dbcon = new Utils.DBConComSqlite(_databaseFilename))
                    {
                        if (InitDatabase(dbcon))
                        {
                            DbDataReader dr = dbcon.ExecuteReader("select * from areainfo");
                            while (dr.Read())
                            {
                                Framework.Data.AreaInfo ai = new Framework.Data.AreaInfo();
                                ai.ID       = (int)dr["id"];
                                ai.ParentID = dr["parentid"] == DBNull.Value ? null : dr["parentid"];
                                ai.Level    = (Framework.Data.AreaType)(short)(int) dr["level"];
                                ai.Name     = (string)dr["name"];
                                ai.MinLat   = (double)dr["minlat"];
                                ai.MinLon   = (double)dr["minlon"];
                                ai.MaxLat   = (double)dr["maxlat"];
                                ai.MaxLon   = (double)dr["maxlon"];
                                _cachedAreaInfo.Add(ai);
                            }
                        }

                        object o = dbcon.ExecuteScalar("select Max(id) from areainfo");
                        if (o != null)
                        {
                            _maxAreaInfoId = (int)(long)o;
                        }
                        o = dbcon.ExecuteScalar("select Max(id) from poly");
                        if (o != null)
                        {
                            _maxPolyId = (int)(long)o;
                        }
                    }
                }
                catch
                {
                    //corrupt file
                    _cachedAreaInfo.Clear();
                }
            }
        }
Exemple #10
0
        internal bool setParent(Framework.Data.AreaInfo ai, Framework.Data.AreaInfo parent)
        {
            bool result = true;

            try
            {
                using (Utils.DBCon dbcon = new Utils.DBConComSqlite(_databaseFilename))
                {
                    dbcon.ExecuteNonQuery(string.Format("update areainfo set parentid= where id={0}", ai.ID, parent == null ? "NULL" : parent.ID));
                    ai.ParentID = parent == null ? null : parent.ID;
                }
            }
            catch
            {
                result = false;
            }
            return(result);
        }
Exemple #11
0
        internal bool deleteArea(Framework.Data.AreaInfo ai)
        {
            bool result = true;

            try
            {
                using (Utils.DBCon dbcon = new Utils.DBConComSqlite(_databaseFilename))
                {
                    dbcon.ExecuteNonQuery(string.Format("delete from poly where areainfoid={0}", ai.ID));
                    dbcon.ExecuteNonQuery(string.Format("delete from areainfo where id={0}", ai.ID));
                    _cachedAreaInfo.Remove(ai);
                }
            }
            catch
            {
                result = false;
            }
            return(result);
        }
Exemple #12
0
 private void buttonAdd_Click(object sender, EventArgs e)
 {
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
         _selectedAreaType = (Framework.Data.AreaType)Enum.Parse(typeof(Framework.Data.AreaType), comboBoxLevel.SelectedItem.ToString());
         _selectedParent   = comboBoxParent.SelectedItem as Framework.Data.AreaInfo;
         _filenames        = openFileDialog1.FileNames;
         _actionReady      = new ManualResetEvent(false);
         _actionReady.Reset();
         Thread thrd = new Thread(new ThreadStart(this.processMethod));
         thrd.Start();
         while (!_actionReady.WaitOne(100))
         {
             System.Windows.Forms.Application.DoEvents();
         }
         thrd.Join();
         _actionReady.Close();
         comboBoxLevel_SelectedIndexChanged(this, EventArgs.Empty);
     }
 }
Exemple #13
0
 public void GetPolygonOfArea(Framework.Data.AreaInfo area)
 {
     if (_areaInfos.Contains(area))
     {
         //ours
         //get all records and add the data
         area.Polygons = new List <Framework.Data.Polygon>();
         try
         {
             var recs = from r in _indexRecords where r.Name == area.Name select r;
             foreach (var rec in recs)
             {
                 GetPolygonOfArea(area.Polygons, rec);
             }
         }
         catch
         {
         }
     }
 }
Exemple #14
0
        private void addPolygons(Framework.Data.AreaInfo ai, List <Framework.Data.Polygon> polys)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("[");
            bool firstPoly = true;
            bool firstPoint;

            foreach (Framework.Data.Polygon ps in polys)
            {
                //[{points:[{lat, lon},{lat, lon}]},{points:[{lat, lon},{lat, lon}]}]
                if (!firstPoly)
                {
                    sb.Append(",");
                }
                else
                {
                    firstPoly = false;
                }
                sb.Append("{points:[");
                firstPoint = true;
                //List < Framework.Data.Location> reduced = Utils.DouglasPeucker.DouglasPeuckerReduction(ps, 0.00001);
                //foreach (Framework.Data.Location l in reduced)
                foreach (Framework.Data.Location l in ps)
                {
                    if (!firstPoint)
                    {
                        sb.Append(",");
                    }
                    else
                    {
                        firstPoint = false;
                    }
                    sb.AppendFormat("{{lat: {0}, lon: {1}}}", l.Lat.ToString().Replace(',', '.'), l.Lon.ToString().Replace(',', '.'));
                }
                sb.Append("]}");
            }
            sb.Append("]");
            executeScript(string.Format("addPolygons({0})", sb.ToString()));
        }
Exemple #15
0
 private void buttonAdd_Click(object sender, EventArgs e)
 {
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
         _selectedAreaType = (Framework.Data.AreaType)Enum.Parse(typeof(Framework.Data.AreaType), comboBoxLevel.SelectedItem.ToString());
         _selectedParent = comboBoxParent.SelectedItem as Framework.Data.AreaInfo;
         _filenames = openFileDialog1.FileNames;
         _actionReady = new ManualResetEvent(false);
         _actionReady.Reset();
         Thread thrd = new Thread(new ThreadStart(this.processMethod));
         thrd.Start();
         while (!_actionReady.WaitOne(100))
         {
             System.Windows.Forms.Application.DoEvents();
         }
         thrd.Join();
         _actionReady.Close();
         comboBoxLevel_SelectedIndexChanged(this, EventArgs.Empty);
     }
 }
Exemple #16
0
        internal bool processTextFile(Framework.Data.AreaType level, string f, Framework.Data.AreaInfo parent)
        {
            PreLoadAreaInfo();
            bool result = false;

            try
            {
                //filename = AreaName
                string areaName = System.IO.Path.GetFileNameWithoutExtension(f);
                char[] num      = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                int    pos      = 0;
                while (pos < areaName.Length && !num.Contains(areaName[pos]))
                {
                    pos++;
                }
                areaName = areaName.Substring(0, pos);
                string[] lines = System.IO.File.ReadAllLines(f);
                pos = 0;
                if (lines[0].StartsWith("# GsakName="))
                {
                    areaName = lines[0].Substring(lines[0].IndexOf('=') + 1).Trim();
                    pos++;
                }
                string slat = "";
                string slon = "";
                double lat;
                double lon;
                bool   newPoly;
                Framework.Data.AreaInfo        ai;
                List <Framework.Data.AreaInfo> ailist = GetAreasByName(areaName, level);
                bool firstPoint = true;
                if (ailist.Count > 0)
                {
                    ai         = ailist[0];
                    firstPoint = false;
                    newPoly    = false;
                }
                else
                {
                    newPoly = true;
                    ai      = new Framework.Data.AreaInfo();
                    ai.ID   = _maxAreaInfoId + 1;
                    _maxAreaInfoId++;
                    ai.Name     = areaName;
                    ai.Level    = level;
                    ai.Polygons = new List <Framework.Data.Polygon>();
                }
                Framework.Data.Polygon poly = new Framework.Data.Polygon();
                _maxPolyId++;
                using (Utils.DBCon dbcon = new Utils.DBConComSqlite(_databaseFilename))
                {
                    dbcon.BeginTran();
                    while (pos < lines.Length)
                    {
                        string s = lines[pos].Trim();
                        if (!s.StartsWith("#"))
                        {
                            string[] parts = s.Split(new char[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                            if (parts.Length == 2)
                            {
                                lat = Utils.Conversion.StringToDouble(parts[0]);
                                lon = Utils.Conversion.StringToDouble(parts[1]);
                                if (firstPoint)
                                {
                                    ai.MinLat  = lat;
                                    ai.MaxLat  = lat;
                                    ai.MinLon  = lon;
                                    ai.MaxLon  = lon;
                                    firstPoint = false;
                                }
                                else
                                {
                                    if (lat < ai.MinLat)
                                    {
                                        ai.MinLat = lat;
                                    }
                                    if (lat > ai.MaxLat)
                                    {
                                        ai.MaxLat = lat;
                                    }
                                    if (lon < ai.MinLon)
                                    {
                                        ai.MinLon = lon;
                                    }
                                    if (lon > ai.MaxLon)
                                    {
                                        ai.MaxLon = lon;
                                    }
                                }
                                poly.AddLocation(new Framework.Data.Location(lat, lon));
                                dbcon.ExecuteNonQuery(string.Format("insert into poly (id, areainfoid, position, lat, lon) values ({0}, {1}, {2}, {3}, {4})", _maxPolyId, ai.ID, poly.Count, lat.ToString().Replace(',', '.'), lon.ToString().Replace(',', '.')));
                                if (poly.Count == 1)
                                {
                                    slat = parts[0];
                                    slon = parts[1];
                                    ai.Polygons.Add(poly);
                                }
                                else if (slat == parts[0] && slon == parts[1])
                                {
                                    poly = new Framework.Data.Polygon();
                                    _maxPolyId++;
                                }
                            }
                        }
                        pos++;
                    }
                    if (newPoly)
                    {
                        _cachedAreaInfo.Add(ai);
                        dbcon.ExecuteNonQuery(string.Format("insert into areainfo (id, parentid, level, name, minlat, minlon, maxlat, maxlon) values ({0}, {1}, {2}, '{3}', {4}, {5}, {6}, {7})", ai.ID, parent == null ? "NULL" : parent.ID, (short)ai.Level, ai.Name.Replace("'", "''"), ai.MinLat.ToString().Replace(',', '.'), ai.MinLon.ToString().Replace(',', '.'), ai.MaxLat.ToString().Replace(',', '.'), ai.MaxLon.ToString().Replace(',', '.')));
                    }
                    else
                    {
                        dbcon.ExecuteNonQuery(string.Format("update areainfo set parentid={0}, minlat={1}, minlon={2}, maxlat={3}, maxlon={4} where id={5}", parent == null ? "NULL" : parent.ID, ai.MinLat.ToString().Replace(',', '.'), ai.MinLon.ToString().Replace(',', '.'), ai.MaxLat.ToString().Replace(',', '.'), ai.MaxLon.ToString().Replace(',', '.'), ai.ID));
                    }
                    dbcon.CommitTran();
                    result = true;
                }
            }
            catch
            {
            }
            return(result);
        }
Exemple #17
0
        public bool Initialize(string dbfNameFieldName, CoordType coordType, Framework.Data.AreaType areaType, string namePrefix)
        {
            bool result = false;
            try
            {
                _coordType = coordType;
                _shpFileStream = File.OpenRead(_shpFilename);
                _areaType = areaType;
                int FileCode = GetInt32(_shpFileStream, false);
                if (FileCode==9994)
                {
                    _shpFileStream.Position = 24;
                    _shpFileSize = GetInt32(_shpFileStream, false);
                    _shpVersion = GetInt32(_shpFileStream, true);
                    _shpShapeType = (ShapeType)GetInt32(_shpFileStream, true);
                    _shpXMin = GetDouble(_shpFileStream, true);
                    _shpYMin = GetDouble(_shpFileStream, true);
                    _shpXMax = GetDouble(_shpFileStream, true);
                    _shpYMax = GetDouble(_shpFileStream, true);

                    using (FileStream fs = File.OpenRead(string.Format("{0}shx", _shpFilename.Substring(0, _shpFilename.Length - 3))))
                    {
                        FileCode = GetInt32(fs, false);
                        if (FileCode == 9994)
                        {
                            fs.Position = 24;
                            int shxFileSize = GetInt32(fs, false);
                            int shxVersion = GetInt32(fs, true);

                            int intRecordCount = ((shxFileSize * 2) - 100) / 8;
                            fs.Position = 100;
                            _indexRecords = new IndexRecord[intRecordCount];
                            for (int i = 0; i < intRecordCount; i++)
                            {
                                _indexRecords[i] = new IndexRecord() { Offset = GetInt32(fs, false) * 2, ContentLength = GetInt32(fs, false) * 2 };
                            }
                            for (int i = 0; i < intRecordCount; i++)
                            {
                                IndexRecord ir = _indexRecords[i];
                                _shpFileStream.Position = ir.Offset + 8;
                                ir.ShapeType = (ShapeType)GetInt32(_shpFileStream, true);
                                if (ir.ShapeType == ShapeType.NullShape)
                                {
                                    ir.ShapeType = _shpShapeType;
                                }
                                switch (ir.ShapeType)
                                {
                                    case ShapeType.Polygon:
                                    case ShapeType.PolygonZ:
                                    case ShapeType.PolygonM:
                                    case ShapeType.MultiPatch:
                                        ir.XMin = GetDouble(_shpFileStream, true);
                                        ir.YMin = GetDouble(_shpFileStream, true);
                                        ir.XMax = GetDouble(_shpFileStream, true);
                                        ir.YMax = GetDouble(_shpFileStream, true);
                                        ir.Ignore = false;
                                        break;
                                    default:
                                        ir.Ignore = true;
                                        break;
                                }
                            }
                            using (DotNetDBF.DBFReader dbf = new DotNetDBF.DBFReader(string.Format("{0}dbf", _shpFilename.Substring(0, _shpFilename.Length - 3))))
                            {
                                var fields = dbf.Fields;
                                dbf.SetSelectFields(new string[]{dbfNameFieldName});
                                var rec = dbf.NextRecord();
                                int index = 0;
                                while (rec != null)
                                {
                                    if (!_indexRecords[index].Ignore)
                                    {
                                        _indexRecords[index].Name = string.Format("{0}{1}",namePrefix,rec[0]);
                                        if (_indexRecords[index].Name == "Fryslân" || _indexRecords[index].Name == "Frysl�n")
                                        {
                                            _indexRecords[index].Name = "Friesland";
                                        }
                                        else
                                        {
                                            _indexRecords[index].Name = _indexRecords[index].Name.Replace("�", "â");
                                        }
                                    }
                                    else
                                    {
                                        _indexRecords[index].Name = null;
                                    }
                                    index++;
                                    if (index < _indexRecords.Length)
                                    {
                                        rec = dbf.NextRecord();
                                    }
                                    else
                                    {
                                        rec = null;
                                    }
                                }
                            }

                            // all ok, check if we need to convert the coords to WGS84, the internal coord system
                            if (_coordType == CoordType.DutchGrid)
                            {
                                Utils.Calculus.LatLonFromRD(_shpXMin, _shpYMin, out _shpYMin, out _shpXMin);
                                Utils.Calculus.LatLonFromRD(_shpXMax, _shpYMax, out _shpYMax, out _shpXMax);

                                double lat;
                                double lon;
                                for (int i = 0; i < intRecordCount; i++)
                                {
                                    IndexRecord ir = _indexRecords[i];
                                    Utils.Calculus.LatLonFromRD(ir.XMin, ir.YMin, out lat, out lon);
                                    ir.YMin = lat;
                                    ir.XMin = lon;
                                    Utils.Calculus.LatLonFromRD(ir.XMax, ir.YMax, out lat, out lon);
                                    ir.YMax = lat;
                                    ir.XMax = lon;
                                }
                            }

                            var areaNames = (from a in _indexRecords select a.Name).Distinct();
                            foreach (var name in areaNames)
                            {
                                var records = from r in _indexRecords where r.Name == name select r;
                                Framework.Data.AreaInfo ai = new Framework.Data.AreaInfo();
                                ai.ID = ai;
                                ai.Level = areaType;
                                ai.MaxLat = records.Max(x => x.YMax);
                                ai.MaxLon = records.Max(x => x.XMax);
                                ai.MinLat = records.Min(x => x.YMin);
                                ai.MinLon = records.Min(x => x.XMin);
                                ai.Name = name;
                                ai.ParentID = null; //not supported
                                _areaInfos.Add(ai);
                            }

                            result = true;
                        }
                    }
                }
            }
            catch
            {
            }
            return result;
        }
Exemple #18
0
        public bool Initialize(string dbfNameFieldName, CoordType coordType, Framework.Data.AreaType areaType, string namePrefix, string dbfEncoding)
        {
            bool result = false;

            try
            {
                _coordType     = coordType;
                _shpFileStream = File.OpenRead(_shpFilename);
                _areaType      = areaType;
                _dbfEncoding   = dbfEncoding;
                int FileCode = GetInt32(_shpFileStream, false);
                if (FileCode == 9994)
                {
                    _shpFileStream.Position = 24;
                    _shpFileSize            = GetInt32(_shpFileStream, false);
                    _shpVersion             = GetInt32(_shpFileStream, true);
                    _shpShapeType           = (ShapeType)GetInt32(_shpFileStream, true);
                    _shpXMin = GetDouble(_shpFileStream, true);
                    _shpYMin = GetDouble(_shpFileStream, true);
                    _shpXMax = GetDouble(_shpFileStream, true);
                    _shpYMax = GetDouble(_shpFileStream, true);

                    using (FileStream fs = File.OpenRead(string.Format("{0}shx", _shpFilename.Substring(0, _shpFilename.Length - 3))))
                    {
                        FileCode = GetInt32(fs, false);
                        if (FileCode == 9994)
                        {
                            fs.Position = 24;
                            int shxFileSize = GetInt32(fs, false);
                            int shxVersion  = GetInt32(fs, true);

                            int intRecordCount = ((shxFileSize * 2) - 100) / 8;
                            fs.Position   = 100;
                            _indexRecords = new IndexRecord[intRecordCount];
                            for (int i = 0; i < intRecordCount; i++)
                            {
                                _indexRecords[i] = new IndexRecord()
                                {
                                    Offset = GetInt32(fs, false) * 2, ContentLength = GetInt32(fs, false) * 2
                                };
                            }
                            for (int i = 0; i < intRecordCount; i++)
                            {
                                IndexRecord ir = _indexRecords[i];
                                _shpFileStream.Position = ir.Offset + 8;
                                ir.ShapeType            = (ShapeType)GetInt32(_shpFileStream, true);
                                if (ir.ShapeType == ShapeType.NullShape)
                                {
                                    ir.ShapeType = _shpShapeType;
                                }
                                switch (ir.ShapeType)
                                {
                                case ShapeType.Polygon:
                                case ShapeType.PolygonZ:
                                case ShapeType.PolygonM:
                                case ShapeType.MultiPatch:
                                    ir.XMin   = GetDouble(_shpFileStream, true);
                                    ir.YMin   = GetDouble(_shpFileStream, true);
                                    ir.XMax   = GetDouble(_shpFileStream, true);
                                    ir.YMax   = GetDouble(_shpFileStream, true);
                                    ir.Ignore = false;
                                    break;

                                default:
                                    ir.Ignore = true;
                                    break;
                                }
                            }
                            using (DotNetDBF.DBFReader dbf = new DotNetDBF.DBFReader(string.Format("{0}dbf", _shpFilename.Substring(0, _shpFilename.Length - 3)), _dbfEncoding))
                            {
                                var fields = dbf.Fields;
                                dbf.SetSelectFields(new string[] { dbfNameFieldName });
                                var rec   = dbf.NextRecord();
                                int index = 0;
                                while (rec != null)
                                {
                                    if (!_indexRecords[index].Ignore)
                                    {
                                        _indexRecords[index].Name = string.Format("{0}{1}", namePrefix, rec[0]);
                                    }
                                    else
                                    {
                                        _indexRecords[index].Name = null;
                                    }
                                    index++;
                                    if (index < _indexRecords.Length)
                                    {
                                        rec = dbf.NextRecord();
                                    }
                                    else
                                    {
                                        rec = null;
                                    }
                                }
                            }

                            // all ok, check if we need to convert the coords to WGS84, the internal coord system
                            if (_coordType == CoordType.DutchGrid)
                            {
                                Utils.Calculus.LatLonFromRD(_shpXMin, _shpYMin, out _shpYMin, out _shpXMin);
                                Utils.Calculus.LatLonFromRD(_shpXMax, _shpYMax, out _shpYMax, out _shpXMax);

                                double lat;
                                double lon;
                                for (int i = 0; i < intRecordCount; i++)
                                {
                                    IndexRecord ir = _indexRecords[i];
                                    Utils.Calculus.LatLonFromRD(ir.XMin, ir.YMin, out lat, out lon);
                                    ir.YMin = lat;
                                    ir.XMin = lon;
                                    Utils.Calculus.LatLonFromRD(ir.XMax, ir.YMax, out lat, out lon);
                                    ir.YMax = lat;
                                    ir.XMax = lon;
                                }
                            }

                            var areaNames = (from a in _indexRecords select a.Name).Distinct();
                            foreach (var name in areaNames)
                            {
                                var records = from r in _indexRecords where r.Name == name select r;
                                Framework.Data.AreaInfo ai = new Framework.Data.AreaInfo();
                                ai.ID       = ai;
                                ai.Level    = areaType;
                                ai.MaxLat   = records.Max(x => x.YMax);
                                ai.MaxLon   = records.Max(x => x.XMax);
                                ai.MinLat   = records.Min(x => x.YMin);
                                ai.MinLon   = records.Min(x => x.XMin);
                                ai.Name     = name;
                                ai.ParentID = null; //not supported
                                _areaInfos.Add(ai);
                            }

                            result = true;
                        }
                    }
                }
            }
            catch
            {
            }
            return(result);
        }
Exemple #19
0
        private void PreLoadAreaInfo()
        {
            if (_cachedAreaInfo == null)
            {
                _cachedAreaInfo = new List<Framework.Data.AreaInfo>();

                try
                {
                    using (Utils.DBCon dbcon = new Utils.DBConComSqlite(_databaseFilename))
                    {
                        if (InitDatabase(dbcon))
                        {
                            DbDataReader dr = dbcon.ExecuteReader("select * from areainfo");
                            while (dr.Read())
                            {
                                Framework.Data.AreaInfo ai = new Framework.Data.AreaInfo();
                                ai.ID = (int)dr["id"];
                                ai.ParentID = dr["parentid"] == DBNull.Value ? null : dr["parentid"];
                                ai.Level = (Framework.Data.AreaType)(short)(int)dr["level"];
                                ai.Name = (string)dr["name"];
                                ai.MinLat = (double)dr["minlat"];
                                ai.MinLon = (double)dr["minlon"];
                                ai.MaxLat = (double)dr["maxlat"];
                                ai.MaxLon = (double)dr["maxlon"];
                                _cachedAreaInfo.Add(ai);
                            }
                        }

                        object o = dbcon.ExecuteScalar("select Max(id) from areainfo");
                        if (o != null)
                        {
                            _maxAreaInfoId = (int)(long)o;
                        }
                        o = dbcon.ExecuteScalar("select Max(id) from poly");
                        if (o != null)
                        {
                            _maxPolyId = (int)(long)o;
                        }
                    }
                }
                catch
                {
                    //corrupt file
                    _cachedAreaInfo.Clear();
                }
            }
        }
Exemple #20
0
 internal bool processTextFile(Framework.Data.AreaType level, string f, Framework.Data.AreaInfo parent)
 {
     PreLoadAreaInfo();
     bool result = false;
     try
     {
         //filename = AreaName
         string areaName = System.IO.Path.GetFileNameWithoutExtension(f);
         char[] num = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
         int pos = 0;
         while (pos < areaName.Length && !num.Contains(areaName[pos])) pos++;
         areaName = areaName.Substring(0, pos);
         string[] lines = System.IO.File.ReadAllLines(f);
         pos = 0;
         if (lines[0].StartsWith("# GsakName="))
         {
             areaName = lines[0].Substring(lines[0].IndexOf('=')+1).Trim();
             pos++;
         }
         string slat = "";
         string slon = "";
         double lat;
         double lon;
         bool newPoly;
         Framework.Data.AreaInfo ai;
         List<Framework.Data.AreaInfo> ailist = GetAreasByName(areaName, level);
         bool firstPoint = true;
         if (ailist.Count > 0)
         {
             ai = ailist[0];
             firstPoint = false;
             newPoly = false;
         }
         else
         {
             newPoly = true;
             ai = new Framework.Data.AreaInfo();
             ai.ID = _maxAreaInfoId + 1;
             _maxAreaInfoId++;
             ai.Name = areaName;
             ai.Level = level;
             ai.Polygons = new List<Framework.Data.Polygon>();
         }
         Framework.Data.Polygon poly = new Framework.Data.Polygon();
         _maxPolyId++;
         using (Utils.DBCon dbcon = new Utils.DBConComSqlite(_databaseFilename))
         {
             dbcon.BeginTran();
             while (pos < lines.Length)
             {
                 string s = lines[pos].Trim();
                 if (!s.StartsWith("#"))
                 {
                     string[] parts = s.Split(new char[] { ' ', ',', '\t'},  StringSplitOptions.RemoveEmptyEntries);
                     if (parts.Length == 2)
                     {
                         lat = Utils.Conversion.StringToDouble(parts[0]);
                         lon = Utils.Conversion.StringToDouble(parts[1]);
                         if (firstPoint)
                         {
                             ai.MinLat = lat;
                             ai.MaxLat = lat;
                             ai.MinLon = lon;
                             ai.MaxLon = lon;
                             firstPoint = false;
                         }
                         else
                         {
                             if (lat < ai.MinLat) ai.MinLat = lat;
                             if (lat > ai.MaxLat) ai.MaxLat = lat;
                             if (lon < ai.MinLon) ai.MinLon = lon;
                             if (lon > ai.MaxLon) ai.MaxLon = lon;
                         }
                         poly.AddLocation(new Framework.Data.Location(lat, lon));
                         dbcon.ExecuteNonQuery(string.Format("insert into poly (id, areainfoid, position, lat, lon) values ({0}, {1}, {2}, {3}, {4})", _maxPolyId, ai.ID, poly.Count, lat.ToString().Replace(',', '.'), lon.ToString().Replace(',', '.')));
                         if (poly.Count == 1)
                         {
                             slat = parts[0];
                             slon = parts[1];
                             ai.Polygons.Add(poly);
                         }
                         else if (slat == parts[0] && slon == parts[1])
                         {
                             poly = new Framework.Data.Polygon();
                             _maxPolyId++;
                         }
                     }
                 }
                 pos++;
             }
             if (newPoly)
             {
                 _cachedAreaInfo.Add(ai);
                 dbcon.ExecuteNonQuery(string.Format("insert into areainfo (id, parentid, level, name, minlat, minlon, maxlat, maxlon) values ({0}, {1}, {2}, '{3}', {4}, {5}, {6}, {7})", ai.ID, parent == null ? "NULL" : parent.ID, (short)ai.Level, ai.Name.Replace("'", "''"), ai.MinLat.ToString().Replace(',', '.'), ai.MinLon.ToString().Replace(',', '.'), ai.MaxLat.ToString().Replace(',', '.'), ai.MaxLon.ToString().Replace(',', '.')));
             }
             else
             {
                 dbcon.ExecuteNonQuery(string.Format("update areainfo set parentid={0}, minlat={1}, minlon={2}, maxlat={3}, maxlon={4} where id={5}", parent == null ? "NULL" : parent.ID, ai.MinLat.ToString().Replace(',', '.'), ai.MinLon.ToString().Replace(',', '.'), ai.MaxLat.ToString().Replace(',', '.'), ai.MaxLon.ToString().Replace(',', '.'), ai.ID));
             }
             dbcon.CommitTran();
             result = true;
         }
     }
     catch
     {
     }
     return result;
 }
Exemple #21
0
 public void GetPolygonOfArea(Framework.Data.AreaInfo area)
 {
     _shapeFilesManager.GetPolygonOfArea(area);
 }