コード例 #1
0
ファイル: SHPFile.cs プロジェクト: jugstalt/gview5
        private void UpdateHeaderEnvelope(BinaryWriter bw, HeaderEnvelope he)
        {
            try
            {
                double MinX = (_header.Xmin != 0.0) ? _header.Xmin : he.minx;
                double MinY = (_header.Ymin != 0.0) ? _header.Ymin : he.miny;
                double MaxX = (_header.Xmax != 0.0) ? _header.Xmax : he.maxx;
                double MaxY = (_header.Ymax != 0.0) ? _header.Ymax : he.maxy;
                double MinZ = (_header.Zmin != 0.0) ? _header.Zmin : he.minz;
                double MaxZ = (_header.Zmax != 0.0) ? _header.Zmax : he.maxz;
                double MinM = (_header.Mmin != 0.0) ? _header.Mmin : he.minm;
                double MaxM = (_header.Mmax != 0.0) ? _header.Mmax : he.maxm;

                bw.BaseStream.Position = 36;
                bw.Write((double)(_header.Xmin = Math.Min(MinX, he.minx)));
                bw.Write((double)(_header.Ymin = Math.Min(MinY, he.miny)));
                bw.Write((double)(_header.Xmax = Math.Max(MaxX, he.maxx)));
                bw.Write((double)(_header.Ymax = Math.Max(MaxY, he.maxy)));
                bw.Write((double)(_header.Zmin = Math.Min(MinZ, he.minz)));
                bw.Write((double)(_header.Zmax = Math.Max(MaxZ, he.maxz)));
                bw.Write((double)(_header.Mmin = Math.Min(MinM, he.minm)));
                bw.Write((double)(_header.Mmax = Math.Max(MaxM, he.maxm)));

                // FileLength in 16 bit Words
                bw.BaseStream.Position = 24;
                bw.Write((int)SwapWord((uint)(bw.BaseStream.Length / 2)));
                bw.BaseStream.Flush();
            }
            catch (Exception ex)
            {
                string err = ex.Message;
            }
        }
コード例 #2
0
ファイル: SHPFile.cs プロジェクト: jugstalt/gview5
 private void WriteEnvelope(BinaryWriter bw, IEnvelope envelope, HeaderEnvelope he)
 {
     if (envelope == null)
     {
         bw.Write((double)0.0);
         bw.Write((double)0.0);
         bw.Write((double)0.0);
         bw.Write((double)0.0);
     }
     else
     {
         bw.Write(he.minx = envelope.minx);
         bw.Write(he.miny = envelope.miny);
         bw.Write(he.maxx = envelope.maxx);
         bw.Write(he.maxy = envelope.maxy);
     }
 }
コード例 #3
0
ファイル: SHPFile.cs プロジェクト: jugstalt/gview5
        internal bool WriteShape(IFeature feature)
        {
            if (feature == null)
            {
                return(false);
            }

            StreamWriter sw_shx = null;
            StreamWriter sw_shp = null;
            BinaryWriter bw_shp = null;
            BinaryWriter bw_shx = null;
            FileStream   fs_shx = null, fs_shp = null;

            try
            {
                //this.Close();

                sw_shx = new StreamWriter(_file_SHX, true);
                sw_shp = new StreamWriter(_file_SHP, true);
                bw_shx = new BinaryWriter(sw_shx.BaseStream);
                bw_shp = new BinaryWriter(sw_shp.BaseStream);

                //sw_shx.BaseStream.Position = fi_shp.Length;
                //sw_shx.BaseStream.Position = fi_shx.Length;

                long pos1      = sw_shp.BaseStream.Position;
                uint recNumber = (uint)(sw_shx.BaseStream.Length - 100) / 8 + 1;

                HeaderEnvelope he = new HeaderEnvelope();

                long contentsLenthPos = 0;
                switch (_header.ShapeType)
                {
                case ShapeType.NullShape:
                    break;

                case ShapeType.PointM:
                case ShapeType.PointZ:
                case ShapeType.Point:
                    if (!(feature.Shape is IPoint))
                    {
                        return(false);
                    }

                    IPoint p = (IPoint)feature.Shape;
                    he.minx = he.maxx = p.X;
                    he.miny = he.maxy = p.Y;
                    if (_header.ShapeType == ShapeType.PointZ)
                    {
                        he.minz = he.maxz = p.Z;
                    }

                    contentsLenthPos = WriteFeatureHeader(bw_shp, recNumber);
                    WritePoint(bw_shp, (IPoint)feature.Shape);
                    if (_header.ShapeType == ShapeType.PointZ)
                    {
                        bw_shp.Write(((IPoint)feature.Shape).Z);
                    }
                    if (_header.ShapeType == ShapeType.PointM || _header.ShapeType == ShapeType.PointZ)
                    {
                        //bw_shp.Write(((IPoint)feature.Shape).M);
                        bw_shp.Write((double)0.0);
                    }
                    break;

                case ShapeType.MultiPointM:
                case ShapeType.MultiPointZ:
                case ShapeType.MultiPoint:
                    if (feature.Shape is IPoint)
                    {
                        contentsLenthPos = WriteFeatureHeader(bw_shp, recNumber);
                        WriteEnvelope(bw_shp, feature.Shape.Envelope, he);
                        bw_shp.Write((int)1);
                        WritePoint(bw_shp, (IPoint)feature.Shape);
                        if (_header.ShapeType == ShapeType.MultiPointZ)
                        {
                            bw_shp.Write(((IPoint)feature.Shape).Z);
                        }
                        if (_header.ShapeType == ShapeType.MultiPointM || _header.ShapeType == ShapeType.MultiPointZ)
                        {
                            //bw_shp.Write(((IPoint)feature.Shape).M);
                            bw_shp.Write((double)0.0);
                        }
                    }
                    else if (feature.Shape is IPointCollection)
                    {
                        contentsLenthPos = WriteFeatureHeader(bw_shp, recNumber);
                        WriteEnvelope(bw_shp, feature.Shape.Envelope, he);
                        bw_shp.Write((int)((IPointCollection)feature.Shape).PointCount);
                        WritePoints(bw_shp, (IPointCollection)feature.Shape);
                        if (_header.ShapeType == ShapeType.MultiPointZ)
                        {
                            WritePointsZRange(bw_shp, (IPointCollection)feature.Shape);
                            WritePointsZ(bw_shp, (IPointCollection)feature.Shape);
                        }
                        if (_header.ShapeType == ShapeType.MultiPointM || _header.ShapeType == ShapeType.MultiPointZ)
                        {
                            //bw_shp.Write(((IPoint)feature.Shape).M);
                            WritePointsMRange(bw_shp, (IPointCollection)feature.Shape);
                            WritePointsM(bw_shp, (IPointCollection)feature.Shape);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                    break;

                case ShapeType.PolyLineM:
                case ShapeType.PolyLineZ:
                case ShapeType.PolyLine:
                    if (!(feature.Shape is IPolyline))
                    {
                        return(false);
                    }
                    IPolyline pline = (IPolyline)feature.Shape;

                    contentsLenthPos = WriteFeatureHeader(bw_shp, recNumber);
                    WriteEnvelope(bw_shp, feature.Shape.Envelope, he);

                    bw_shp.Write((int)pline.PathCount);
                    WritePointCount(bw_shp, pline);
                    WriteParts(bw_shp, pline);

                    for (int i = 0; i < pline.PathCount; i++)
                    {
                        WritePoints(bw_shp, pline[i]);
                    }

                    if (_header.ShapeType == ShapeType.PolyLineM || _header.ShapeType == ShapeType.PolyLineZ)
                    {
                        IPointCollection pColl = gView.Framework.SpatialAlgorithms.Algorithm.GeometryPoints(pline, false);
                        if (_header.ShapeType == ShapeType.PolyLineZ)
                        {
                            WritePointsZRange(bw_shp, pColl);
                            WritePointsZ(bw_shp, pColl);
                        }
                        //bw_shp.Write(((IPoint)feature.Shape).M);
                        WritePointsMRange(bw_shp, pColl);
                        WritePointsM(bw_shp, pColl);
                    }
                    break;

                case ShapeType.PolygonM:
                case ShapeType.PolygonZ:
                case ShapeType.Polygon:
                    if (!(feature.Shape is IPolygon))
                    {
                        return(false);
                    }
                    IPolygon poly = (IPolygon)feature.Shape;

                    contentsLenthPos = WriteFeatureHeader(bw_shp, recNumber);
                    WriteEnvelope(bw_shp, feature.Shape.Envelope, he);

                    bw_shp.Write((int)poly.RingCount);
                    WritePointCount(bw_shp, poly);
                    WriteParts(bw_shp, poly);

                    for (int i = 0; i < poly.RingCount; i++)
                    {
                        WritePoints(bw_shp, poly[i]);
                    }

                    if (_header.ShapeType == ShapeType.PolygonM || _header.ShapeType == ShapeType.PolygonZ)
                    {
                        IPointCollection pColl = gView.Framework.SpatialAlgorithms.Algorithm.GeometryPoints(poly, false);
                        if (_header.ShapeType == ShapeType.PolygonZ)
                        {
                            WritePointsZRange(bw_shp, pColl);
                            WritePointsZ(bw_shp, pColl);
                        }
                        //bw_shp.Write(((IPoint)feature.Shape).M);
                        WritePointsMRange(bw_shp, pColl);
                        WritePointsM(bw_shp, pColl);
                    }
                    break;

                default:
                    return(false);
                }

                sw_shp.Flush();

                uint contentsSize = (uint)(sw_shp.BaseStream.Position - pos1 - 8) / 2; // -8 weil recnumber und nullword nicht mitzählen. Erst Shapetype und coordinaten,...
                bw_shx.Write((int)SwapWord((uint)(pos1 / 2)));                         // 16 bit Words
                bw_shx.Write((int)SwapWord(contentsSize));

                sw_shx.Flush();

                sw_shx.Close(); sw_shx = null;
                sw_shp.Close(); sw_shp = null;

                fs_shp = new FileStream(_file_SHP, FileMode.Open);
                fs_shx = new FileStream(_file_SHX, FileMode.Open);

                bw_shp = new BinaryWriter(fs_shp);
                bw_shx = new BinaryWriter(fs_shx);

                if (contentsLenthPos != 0)
                {
                    bw_shp.BaseStream.Position = contentsLenthPos;
                    bw_shp.Write((int)SwapWord((uint)contentsSize));
                }

                UpdateHeaderEnvelope(bw_shp, he);
                UpdateHeaderEnvelope(bw_shx, he);

                fs_shp.Flush();
                fs_shx.Flush();

                //this.Open();

                _dbfFile.WriteRecord(recNumber, feature);
                return(true);
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return(false);
            }
            finally
            {
                if (sw_shx != null)
                {
                    sw_shx.Close();
                }
                if (sw_shp != null)
                {
                    sw_shp.Close();
                }
                if (fs_shp != null)
                {
                    fs_shp.Close();
                }
                if (fs_shx != null)
                {
                    fs_shx.Close();
                }
            }
        }