Example #1
0
        private void ExcelToLineFeatureClass()
        {
            if (cmbStartNoField.SelectedValue == null || cmbEndNoField.SelectedValue == null || cmbLineGroupField.SelectedValue == null)
            {
                return;
            }
            IWorkspaceEdit pWorkspaceEdit = _targetWorkspace as IWorkspaceEdit;

            if (pWorkspaceEdit == null)
            {
                return;
            }
            int lineRowCount   = _excelHelper.GetRowCount(_lineWorksheet.Name);
            int idxSNoColumn   = Convert.ToInt32(cmbStartNoField.SelectedValue);
            int idxENoColumn   = Convert.ToInt32(cmbEndNoField.SelectedValue);
            int idxGroupColumn = Convert.ToInt32(cmbLineGroupField.SelectedValue);

            for (int i = 2; i <= lineRowCount; i++)
            {
                try
                {
                    string groupName;

                    if (checkBoxIsGroup.Checked)
                    {
                        groupName = ((Range)_lineWorksheet.Cells[i, idxGroupColumn]).Value2.ToString().Trim();
                    }
                    else
                    {
                        groupName = _pointWorksheet.Name.Trim();
                    }
                    CustomFeatureClass pCustomFeatureClass = _lineFeatureClassList.FirstOrDefault(c => c.AName == groupName);
                    if (pCustomFeatureClass == null)
                    {
                        try
                        {
                            IFeatureClass pFeatureClass = CreateFeatureClass(string.Format("{0}_Line", groupName), esriGeometryType.esriGeometryPolyline, _targetLineFieldMappingList);
                            pCustomFeatureClass = new CustomFeatureClass
                            {
                                AName        = groupName,
                                FeatureClass = pFeatureClass
                            };
                            _lineFeatureClassList.Add(pCustomFeatureClass);
                        }
                        catch (Exception e)
                        {
                            throw new Exception(string.Format("{0};{1}({2})", i, e.Message, groupName));
                        }
                    }
                    if (pWorkspaceEdit.IsBeingEdited() == false)
                    {
                        pWorkspaceEdit.StartEditing(true);
                        pWorkspaceEdit.StartEditOperation();
                    }
                    IFeatureCursor featureCursor = pCustomFeatureClass.FeatureClass.Insert(true);
                    IFeatureBuffer newFeature    = pCustomFeatureClass.FeatureClass.CreateFeatureBuffer();
                    foreach (FieldMapping fieldMapping in _targetLineFieldMappingList)
                    {
                        try
                        {
                            if (fieldMapping.IndexOriginField <= 0)
                            {
                                continue;
                            }

                            object str = ((Range)_lineWorksheet.Cells[i, fieldMapping.IndexOriginField]).Value2;
                            if (str == null || string.IsNullOrEmpty(str.ToString()) || str.ToString() == "#N/A")
                            {
                                continue;
                            }
                            newFeature.Value[fieldMapping.IndexTargetField] = str.ToString() == "<空>" || str.ToString() == "" ? null : str;
                        }
                        catch (Exception e)
                        {
                            throw new Exception(string.Format("{0};{1}", i, e.Message));
                        }
                    }
                    object sno = ((Range)_lineWorksheet.Cells[i, idxSNoColumn]).Value2;
                    object eno = ((Range)_lineWorksheet.Cells[i, idxENoColumn]).Value2;
                    if (sno == null || string.IsNullOrEmpty(sno.ToString().Trim()) || eno == null || string.IsNullOrEmpty(eno.ToString().Trim()))
                    {
                        continue;
                    }
                    IPolyline polyline = new PolylineClass();
                    IZAware   pZAware  = polyline as IZAware;
                    pZAware.ZAware = true;
                    IMAware pMAware = polyline as IMAware;
                    pMAware.MAware = true;
                    CustomPoint startCustomPoint = _pointList.FirstOrDefault(c => c.No == sno.ToString().Trim());
                    if (startCustomPoint == null)
                    {
                        continue;
                    }
                    polyline.FromPoint = startCustomPoint.Point;
                    CustomPoint endCustomPoint = _pointList.FirstOrDefault(c => c.No == eno.ToString().Trim());
                    if (endCustomPoint == null)
                    {
                        continue;
                    }
                    polyline.ToPoint = endCustomPoint.Point;
                    newFeature.Shape = polyline;
                    featureCursor.InsertFeature(newFeature);
                    Marshal.ReleaseComObject(featureCursor);
                }
                catch (Exception e)
                {
                    throw new Exception(string.Format("{0};{1}", i, e.Message));
                }
            }
        }
Example #2
0
        private void ExcelToPointFeatureClass()
        {
            IWorkspaceEdit pWorkspaceEdit = _targetWorkspace as IWorkspaceEdit;

            if (pWorkspaceEdit == null)
            {
                return;
            }
            int pointRowCount  = _excelHelper.GetRowCount(_pointWorksheet.Name);
            int idxNoColumn    = Convert.ToInt32(cmbNoField.SelectedValue);
            int idxXColumn     = Convert.ToInt32(cmbXField.SelectedValue);
            int idxYColumn     = Convert.ToInt32(cmbYField.SelectedValue);
            int idxZColumn     = Convert.ToInt32(cmbZField.SelectedValue);
            int idxGroupColumn = Convert.ToInt32(cmbPointGroupField.SelectedValue);

            for (int i = 2; i <= pointRowCount; i++)
            {
                try
                {
                    string groupName;
                    if (checkBoxIsGroup.Checked)
                    {
                        groupName = ((Range)_pointWorksheet.Cells[i, idxGroupColumn]).Value2.ToString().Trim();
                    }
                    else
                    {
                        groupName = _pointWorksheet.Name.Trim();
                    }
                    CustomFeatureClass pCustomFeatureClass = _pointFeatureClassList.FirstOrDefault(c => c.AName == groupName);
                    if (pCustomFeatureClass == null)
                    {
                        try
                        {
                            IFeatureClass pFeatureClass = CreateFeatureClass($"{groupName}_Point", esriGeometryType.esriGeometryPoint, _targetPointFieldMappingList);
                            pCustomFeatureClass = new CustomFeatureClass
                            {
                                AName        = groupName,
                                FeatureClass = pFeatureClass
                            };
                            _pointFeatureClassList.Add(pCustomFeatureClass);
                        }
                        catch (Exception e)
                        {
                            throw new Exception($"{i};{e.Message}({groupName})");
                        }
                    }
                    if (pWorkspaceEdit.IsBeingEdited() == false)
                    {
                        pWorkspaceEdit.StartEditing(true);
                        pWorkspaceEdit.StartEditOperation();
                    }
                    IFeatureCursor featureCursor = pCustomFeatureClass.FeatureClass.Insert(true);
                    IFeatureBuffer newFeature    = pCustomFeatureClass.FeatureClass.CreateFeatureBuffer();
                    IPoint         point         = null;
                    string         no            = ((Range)_pointWorksheet.Cells[i, idxNoColumn]).Value2.ToString().Trim();
                    if (chkHasCoordInfo.Checked)
                    {
                        object sX = ((Range)_pointWorksheet.Cells[i, idxXColumn]).Value2;
                        object sY = ((Range)_pointWorksheet.Cells[i, idxYColumn]).Value2;
                        object sZ = ((Range)_pointWorksheet.Cells[i, idxZColumn]).Value2;
                        if (string.IsNullOrEmpty(sX?.ToString()) || string.IsNullOrEmpty(sY?.ToString()) || string.IsNullOrEmpty(sZ?.ToString()))
                        {
                            continue;
                        }
                        double x = Convert.ToDouble(sX);
                        double y = Convert.ToDouble(sY);
                        double z = Convert.ToDouble(sZ);
                        if (checkBoxConvertCoordinateSystem.Checked)
                        {
                            point = GeometryHelper.CreatePoint(y, x, z, 0, true, true);
                        }
                        else
                        {
                            point = GeometryHelper.CreatePoint(x, y, z, 0, true, true);
                        }
                        _pointList.Add(new CustomPoint
                        {
                            No    = no,
                            Point = point
                        });
                    }
                    else
                    {
                        CustomPoint customPoint = _pointList.FirstOrDefault(c => c.No == no);
                        if (customPoint != null)
                        {
                            point = customPoint.Point;
                        }
                    }
                    if (point != null)
                    {
                        newFeature.Shape = point;
                    }
                    foreach (FieldMapping fieldMapping in _targetPointFieldMappingList)
                    {
                        try
                        {
                            if (fieldMapping.IndexOriginField <= 0)
                            {
                                continue;
                            }
                            object obj = ((Range)_pointWorksheet.Cells[i, fieldMapping.IndexOriginField]).Value2;
                            //string str = ((Range)_pointWorksheet.Cells[i, fieldMapping.IndexOriginField]).Value2.ToString();
                            newFeature.Value[fieldMapping.IndexTargetField] = obj == null || obj.ToString() == "<空>" || obj.ToString() == "" ? null : obj;
                            if (point != null)
                            {
                                if (fieldMapping.OriginFieldName == ((KeyValuePair <int, string>)(cmbXField.SelectedItem)).Value)
                                {
                                    newFeature.Value[fieldMapping.IndexTargetField] = point.X.ToString("#0.0000");
                                }
                                else if (fieldMapping.OriginFieldName == ((KeyValuePair <int, string>)(cmbYField.SelectedItem)).Value)
                                {
                                    newFeature.Value[fieldMapping.IndexTargetField] = point.Y.ToString("#0.0000");
                                }
                                else if (fieldMapping.OriginFieldName == ((KeyValuePair <int, string>)(cmbZField.SelectedItem)).Value)
                                {
                                    newFeature.Value[fieldMapping.IndexTargetField] = point.Z.ToString("#0.0000");
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception($"{i};{e.Message}");
                        }
                    }
                    featureCursor.InsertFeature(newFeature);
                    Marshal.ReleaseComObject(featureCursor);
                }
                catch (Exception e)
                {
                    throw new Exception($"{i};{e.Message}");
                }
            }
        }