예제 #1
0
 public void GetPolygonOfArea(AreaInfo area)
 {
     foreach (var sf in _shapeFiles)
     {
         sf.GetPolygonOfArea(area);
         if (area.Polygons != null)
         {
             break;
         }
     }
 }
예제 #2
0
        private bool IsLocationInEnvelopArea(Utils.Location loc, 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)
            {
                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;
                        }
                    }
                }
            }
            return(result);
        }
예제 #3
0
        public bool Initialize(string dbfNameFieldName, CoordType coordType, AreaType areaType, string namePrefix, string dbfEncoding)
        {
            bool result = false;

            try
            {
                _coordType     = coordType;
                _shpFileStream = File.OpenRead(_shpFilename);
                _areaType      = areaType;
                _dbfEncoding   = dbfEncoding;
                _namePrefix    = namePrefix ?? "";
                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)
                                    {
                                        var n = string.Format("{0}{1}", namePrefix, rec[0]);
                                        _indexRecords[index].Name = n;
                                        var g = _indexRecordsByName[n] as List <IndexRecord>;
                                        if (g == null)
                                        {
                                            g = new List <IndexRecord>();
                                            _indexRecordsByName.Add(n, g);
                                        }
                                        g.Add(_indexRecords[index]);
                                    }
                                    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 = _indexRecordsByName.Keys;
                            foreach (string name in areaNames)
                            {
                                var      records = _indexRecordsByName[name] as List <IndexRecord>;
                                AreaInfo ai      = new 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);
        }
예제 #4
0
 private bool IsLocationInArea(Utils.Location loc, AreaInfo area)
 {
     return(IsLocationInArea(loc.Lat, loc.Lon, area));
 }
예제 #5
0
 public void GetPolygonOfArea(AreaInfo area)
 {
     foreach (var sf in _shapeFiles)
     {
         sf.GetPolygonOfArea(area);
         if (area.Polygons != null)
         {
             break;
         }
     }
 }