Example #1
0
        private static ESRI.ArcGIS.Geometry.IPointCollection PathToPoint(System.Drawing.PointF[] arrPoints)
        {
            ESRI.ArcGIS.Geometry.IPointCollection pointColl = new ESRI.ArcGIS.Geometry.MultipointClass();
            ESRI.ArcGIS.Geometry.IPoint           point     = new ESRI.ArcGIS.Geometry.PointClass();

            foreach (System.Drawing.PointF pointf in arrPoints)
            {
                // switching coords - ESRI and Microsoft use a different 'paper space'.
                point.X = pointf.Y;
                point.Y = pointf.X;

                pointColl.AddPoint(point);
                point = new ESRI.ArcGIS.Geometry.PointClass();
            }

            return(pointColl);
        }
Example #2
0
        // FeatureDataTablet to shapefile
        private void WriteFeatureDataTable2shp(GeometryType gtype, FeatureDataTable ftable, IFeatureClass fc, String shapefilePath)
        {
            int rowCount = ftable.Rows.Count;
            int colCount = ftable.Columns.Count;
            int geometryColIndex = ftable.GeometryColumnIndex;
            //GeometryType gtype = selLayer.GeometryType;
            toolStripProgressBar1.Maximum = rowCount;
            toolStripProgressBar1.Value = 0;
            toolStripProgressBar1.Visible = true;

            // for each selected features
            for (int i = 0; i < rowCount; i++)
            {
                toolStripProgressBar1.Value = i + 1;
                selectionStripStatusLabel1.Text = toolStripProgressBar1.Value.ToString() + "/" + rowCount.ToString() + " features exported .";
                toolStripStatusLabel2.Text = shapefilePath;
                Application.DoEvents();

                // create a new feature
                IFeature feature = fc.CreateFeature();
                DataRow arow = ftable.Rows[i];
                string globalid;
                if (arow.Table.Columns.Contains("GlobalID"))
                {
                    globalid = arow["GlobalID"].ToString();
                }
                else
                {
                    globalid = arow["OBJECTID"].ToString();
                }

                // geometry
                ESRI.ArcGIS.Mobile.Geometries.Geometry mobileGeometry = arow[geometryColIndex] as ESRI.ArcGIS.Mobile.Geometries.Geometry;
                byte[] mobileGeometryByteArray = mobileGeometry.ExportToEsriShape();
                int len = mobileGeometryByteArray.Length;
                switch (gtype)
                {
                    case GeometryType.Point:
                        ESRI.ArcGIS.Geometry.IPoint aoShape = new ESRI.ArcGIS.Geometry.PointClass();
                        ((ESRI.ArcGIS.Geometry.IESRIShape)aoShape).ImportFromESRIShape(ref len, ref mobileGeometryByteArray[0]);
                        feature.Shape = aoShape;
                        break;

                    case GeometryType.Multipoint:
                        ESRI.ArcGIS.Geometry.IMultipoint aoShape2 = new ESRI.ArcGIS.Geometry.MultipointClass();
                        ((ESRI.ArcGIS.Geometry.IESRIShape)aoShape2).ImportFromESRIShape(ref len, ref mobileGeometryByteArray[0]);
                        feature.Shape = aoShape2;
                        break;

                    case GeometryType.Polyline:
                        ESRI.ArcGIS.Geometry.IPolyline aoShape3 = new ESRI.ArcGIS.Geometry.PolylineClass();
                        ((ESRI.ArcGIS.Geometry.IESRIShape)aoShape3).ImportFromESRIShape(ref len, ref mobileGeometryByteArray[0]);
                        feature.Shape = aoShape3;
                        break;

                    case GeometryType.Polygon:
                        ESRI.ArcGIS.Geometry.IPolygon aoShape4 = new ESRI.ArcGIS.Geometry.PolygonClass();
                        ((ESRI.ArcGIS.Geometry.IESRIShape)aoShape4).ImportFromESRIShape(ref len, ref mobileGeometryByteArray[0]);
                        feature.Shape = aoShape4;
                        break;
                }

                // insert other attributes
                // in mobile cache, the last col is for geometry
                // in shapefile, fid and shape fields are first two, for other fields index = +2
                for (int col = 0; col < colCount - 1; col++)
                {
                    // type
                    DataColumn dc = ftable.Columns[col];
                    Type dt = dc.DataType;

                    // guid or globalid, change to string
                    if (dt == typeof(Guid) || (dt == typeof(ESRI.ArcGIS.Mobile.GlobalId)))
                    {
                        feature.set_Value(col + 2, arow[col].ToString());
                        //feature.Store();
                        continue;
                    }

                    // if blob field, write to file
                    if (dt == typeof(Byte[]))
                    {
                        continue;
                    }
                    // raster field, write to jpg files
                    if (dt == typeof(Bitmap))
                    {
                        string photofoldername = shapefilePath + "\\" + dc.ColumnName;
                        if (!Directory.Exists(photofoldername))
                            continue;

                        // get bitmap from a raster field, save to ImageSource
                        System.Windows.Media.ImageSource imgsrc = null;
                        Bitmap bitmap = arow[col] as Bitmap;
                        if (bitmap == null)
                            continue;

                        System.Windows.Int32Rect rect = new System.Windows.Int32Rect(0, 0, bitmap.Width, bitmap.Height);
                        IntPtr hBitmap = IntPtr.Zero;
                        try
                        {
                            hBitmap = bitmap.GetHbitmap();
                            imgsrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                     hBitmap, IntPtr.Zero, rect, BitmapSizeOptions.FromEmptyOptions());

                            // write the ImageSource to jpg
                            if (imgsrc == null)
                                continue;
                            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                            encoder.Frames.Add(BitmapFrame.Create(imgsrc as BitmapSource));
                            string fileName = photofoldername + "\\" + globalid + ".jpg";
                            using (System.IO.FileStream file = File.OpenWrite(fileName))
                            {
                                encoder.Save(file);
                            }

                            continue;

                        }
                        finally
                        {
                            if (hBitmap != IntPtr.Zero)
                                DeleteObject(hBitmap);
                        }
                    }  // end of bitmap

                    // other data types, just set the value
                    if (arow[col].ToString() != "")
                        feature.set_Value(col + 2, arow[col]);
                    else
                        feature.set_Value(col + 2, 0);

                }

                feature.Store();
            }

            //MessageBox.Show(rowCount.ToString() + " features saved into a shapefile at: " + shapefilePath);
            toolStripProgressBar1.Value = 0;
            selectionStripStatusLabel1.Text = "";
            toolStripProgressBar1.Visible = false;

            return;
        }