Пример #1
0
 public void GetPolygonOfArea(Core.Data.AreaInfo area)
 {
     if (_areaInfos.Contains(area))
     {
         //ours
         //get all records and add the data
         area.Polygons = new List <Core.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 (Exception e)
         {
             Core.ApplicationData.Instance.Logger.AddLog(this, e);
         }
     }
 }
Пример #2
0
        private void addPolygons(Core.Data.AreaInfo ai, List <Core.Data.Polygon> polys)
        {
            StringBuilder sb = new StringBuilder();

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

            foreach (Core.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 (Core.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("addPolygons", new object[] { sb.ToString() });
        }
Пример #3
0
        public bool Initialize(string dbfNameFieldName, CoordType coordType, Core.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]);
                                        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;
                                Core.Data.AreaInfo ai = new Core.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(Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
            return result;
        }
Пример #4
0
        public bool Initialize(string dbfNameFieldName, CoordType coordType, Core.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]);
                                        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;
                                Core.Data.AreaInfo ai = new Core.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 (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
            return(result);
        }
Пример #5
0
        public async Task AssignRegion()
        {
            if (Core.ApplicationData.Instance.ActiveDatabase != null)
            {
                List <Core.Data.Geocache> gcList = new List <Core.Data.Geocache>();

                using (Utils.DataUpdater upd = new Utils.DataUpdater(Core.ApplicationData.Instance.ActiveDatabase))
                {
                    await Task.Run(() => {
                        try
                        {
                            switch (SelectedAreaLevel)
                            {
                            case Core.Data.AreaType.Country:
                                gcList = (from g in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where (!UnassignedOnly || string.IsNullOrEmpty(g.Country)) && (g.Selected || !SelectedOnly) select g).ToList();
                                break;

                            case Core.Data.AreaType.State:
                                gcList = (from g in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where (!UnassignedOnly || string.IsNullOrEmpty(g.State)) && (g.Selected || !SelectedOnly) select g).ToList();
                                break;

                            case Core.Data.AreaType.Municipality:
                                gcList = (from g in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where (!UnassignedOnly || string.IsNullOrEmpty(g.Municipality)) && (g.Selected || !SelectedOnly) select g).ToList();
                                break;

                            case Core.Data.AreaType.City:
                                gcList = (from g in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where (!UnassignedOnly || string.IsNullOrEmpty(g.City)) && (g.Selected || !SelectedOnly) select g).ToList();
                                break;
                            }

                            DateTime nextUpdate = DateTime.Now.AddSeconds(1);
                            using (Utils.ProgressBlock prog = new Utils.ProgressBlock("AssignRegionsToGeocaches", "AssignRegionsToGeocaches", gcList.Count, 0, true))
                            {
                                List <Core.Data.AreaInfo> areasFilter = Shapefiles.ShapeFilesManager.Instance.GetAreasByLevel(SelectedAreaLevel);
                                if (areasFilter != null && areasFilter.Count > 0)
                                {
                                    int index = 0;
                                    foreach (var gc in gcList)
                                    {
                                        List <Core.Data.AreaInfo> areas = Shapefiles.ShapeFilesManager.Instance.GetAreasOfLocation(new Core.Data.Location(gc.Lat, gc.Lon), areasFilter);
                                        if (areas != null && areas.Count > 0)
                                        {
                                            Core.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 (SelectedAreaLevel)
                                                {
                                                case Core.Data.AreaType.Country:
                                                    gc.Country = ai.Name;
                                                    break;

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

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

                                                case Core.Data.AreaType.City:
                                                    gc.City = ai.Name;
                                                    break;
                                                }
                                            }
                                        }
                                        index++;
                                        if (DateTime.Now >= nextUpdate)
                                        {
                                            if (!prog.Update("AssignRegionsToGeocaches", gcList.Count, index))
                                            {
                                                break;
                                            }
                                            nextUpdate = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Core.ApplicationData.Instance.Logger.AddLog(this, e);
                        }
                    });
                }
            }
        }