Добавление полигона

ArcGIS 8.x,9.x,10.x (Arcview, ArcEditor, Arcinfo).
Ответить
BodyZ
Участник
Сообщения: 70
Зарегистрирован: 04 сен 2009, 16:47
Репутация: 1

Добавление полигона

Сообщение BodyZ »

Доброго времени суток. Подскажите пожалуйста как программно добавить полигон (polygon) к уже существующему шейп-файлу. Заранее спасибо
Аватара пользователя
Дмитрий Барышников
Гуру
Сообщения: 2572
Зарегистрирован: 17 ноя 2009, 19:17
Репутация: 261
Откуда: Москва

Re: Добавление полигона

Сообщение Дмитрий Барышников »

http://resources.esri.com/help/9.3/arcg ... _start.htm
Не понятно что именно вызывает проблемы:
1) Создание полигона
2) Добавление полигона
3) Открытие шейп-фалй для записи
4) еще что-то
BodyZ
Участник
Сообщения: 70
Зарегистрирован: 04 сен 2009, 16:47
Репутация: 1

Re: Добавление полигона

Сообщение BodyZ »

Как добавляется точка, понятно :wink:
Полилиния добавляется через FromPoint и ToPoint
А как добавляется полигон, не могу сообразить. :?:
Аватара пользователя
Дмитрий Барышников
Гуру
Сообщения: 2572
Зарегистрирован: 17 ноя 2009, 19:17
Репутация: 261
Откуда: Москва

Re: Добавление полигона

Сообщение Дмитрий Барышников »

На сколько понимаю - затык в создании "правильного" полигона. Тогда сюда:
http://resources.esri.com/help/9.3/arcg ... _start.htm
BodyZ
Участник
Сообщения: 70
Зарегистрирован: 04 сен 2009, 16:47
Репутация: 1

Re: Добавление полигона

Сообщение BodyZ »

Вот код, который создает полилинии

Код: Выделить всё

    public void IFeatureBuffer_Example(IFeatureClass featureClass)    {   
     //Function is designed to work with polyline data        
if (featureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline) { return; }
       
 //get the Workspace from the IDataset interface on the feature class       
 IDataset dataset = (IDataset)featureClass;       
 IWorkspace workspace = dataset.Workspace;       

 //Cast for an IWorkspaceEdit        
IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;

        //Start an edit session and operation       
 workspaceEdit.StartEditing(true);       
 workspaceEdit.StartEditOperation();

        //Create the Feature Buffer       
 IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();      
  //Create insert Feature Cursor using buffering = true.       
 IFeatureCursor featureCursor = featureClass.Insert(true);
object featureOID;
        //With a feature buffer you have the ability to set the attribute for a specific field to be        
        //the same for all features added to the buffer.        featureBuffer.set_Value(featureBuffer.Fields.FindField("InstalledBy"), "K Johnston");

        //Here you can set the featurebuffers's shape by setting the featureBuffer.Shape         
        //to a geomerty that matched the featureclasses.        
        //Create 100 features using FeatureBuffer and insert into a feature cursor       
 ESRI.ArcGIS.Geometry.IPolyline polyline = new ESRI.ArcGIS.Geometry.PolylineClass();       
 ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass();       
 for (int i = 0; i < 100; i++)      
  {         
   //Create the polyline geometry to assign to the new feature      
      point.X = 498490 + i * 10;            
      point.Y = 675380 + i * 10;           
      polyline.FromPoint = point;           
      point = new ESRI.ArcGIS.Geometry.PointClass();         
      point.X = 498480 + i * 10;
      point.Y = 675390 + i * 10;  
      polyline.ToPoint = point; 
      featureBuffer.Shape = polyline;

            //Insert the feature into the feature cursor           
 featureOID = featureCursor.InsertFeature(featureBuffer);     
   }       
 //Flush the feature cursor to the database        
//Calling flush allows you to handle any errors at a known time rather then on the cursor destruction.        featureCursor.Flush();
        //Stop editing       
 workspaceEdit.StopEditOperation();       
 workspaceEdit.StopEditing(true);
        //Release the Cursor        
System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);    
}
Как его необходимо переделать, чтобы добавлять полигон?
Заранее спасибо
Аватара пользователя
Дмитрий Барышников
Гуру
Сообщения: 2572
Зарегистрирован: 17 ноя 2009, 19:17
Репутация: 261
Откуда: Москва

Re: Добавление полигона

Сообщение Дмитрий Барышников »

Код: Выделить всё

public void IFeatureBuffer_Example(IFeatureClass featureClass)    {   
     //Function is designed to work with polyline data        
if (featureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon) { return; }
        //Create the Feature Buffer       
IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();      
  //Create insert Feature Cursor using buffering = true.       
IFeatureCursor featureCursor = featureClass.Insert(true);
object featureOID;
        //With a feature buffer you have the ability to set the attribute for a specific field to be        
        //the same for all features added to the buffer.        featureBuffer.set_Value(featureBuffer.Fields.FindField("InstalledBy"), "K Johnston");

        //Here you can set the featurebuffers's shape by setting the featureBuffer.Shape         
        //to a geomerty that matched the featureclasses.        
        //Create 100 features using FeatureBuffer and insert into a feature cursor       
ESRI.ArcGIS.Geometry.IGeometryCollection geometrycollection = new ESRI.ArcGIS.Geometry.PolygonClass(); 
ESRI.ArcGIS.Geometry.ISegmentCollection segmentcollection = new ESRI.ArcGIS.Geometry.RingClass(); 
for (int i = 0; i < 100; i++)      
  {         
	segmentcollection.PutCoords(498490 + i * 10, 675380 + i * 10);
	segmentcollection.PutCoords(498480 + i * 10, 675390 + i * 10);
   }   
              ESRI.ArcGIS.Geometry.IRing ring = segmentcollection;
              ring.Close();
	geometrycollection.AddGeometry(ring); 
	featureBuffer.Shape = geometrycollection;
	featureOID = featureCursor.InsertFeature(featureBuffer);     
	//Flush the feature cursor to the database        
	featureCursor.Flush();
	
	System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);    
}
Примерно так. За точность не ручаюсь ибо С# плохо знаю (переписывал с С++).
BodyZ
Участник
Сообщения: 70
Зарегистрирован: 04 сен 2009, 16:47
Репутация: 1

Re: Добавление полигона

Сообщение BodyZ »

Выбрасывает ошибку в строке segmentcollection.PutCoords(498490 + i * 10, 675380 + i * 10);
В классе ISegmentCollection нет метода PutCoords
Что делать? Спасибо
Аватара пользователя
Дмитрий Барышников
Гуру
Сообщения: 2572
Зарегистрирован: 17 ноя 2009, 19:17
Репутация: 261
Откуда: Москва

Re: Добавление полигона

Сообщение Дмитрий Барышников »

Код: Выделить всё

public void IFeatureBuffer_Example(IFeatureClass featureClass)    
{   
     //Function is designed to work with polyline data
	 if (featureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon) { return; }
        //Create the Feature Buffer       
	IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();      
	//Create insert Feature Cursor using buffering = true.       
	IFeatureCursor featureCursor = featureClass.Insert(true);
	object featureOID;
	ESRI.ArcGIS.Geometry.IGeometryCollection geometrycollection = new ESRI.ArcGIS.Geometry.PolygonClass(); 
	ESRI.ArcGIS.Geometry.IPointCollection pointcollection = new ESRI.ArcGIS.Geometry.RingClass(); 
	ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass();
	for (int i = 0; i < 100; i++)      
	{  		 	
		point.PutCoords(498490 + i * 10, 675380 + i * 10);
		pointcollection.AddPoint(point);
		point.PutCoords(498480 + i * 10, 675390 + i * 10);
		pointcollection.AddPoint(point);
	}   
    ESRI.ArcGIS.Geometry.IRing ring = pointcollection;
    ring.Close();
	geometrycollection.AddGeometry(ring); 
	featureBuffer.Shape = geometrycollection;
	featureOID = featureCursor.InsertFeature(featureBuffer);     
	//Flush the feature cursor to the database        
	featureCursor.Flush();
   
	System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);    
}
Сорри, схалявил.
BodyZ
Участник
Сообщения: 70
Зарегистрирован: 04 сен 2009, 16:47
Репутация: 1

Re: Добавление полигона

Сообщение BodyZ »

Пока не идет. :oops: Попробую разобраться сам. :|
Спасибо за помощь
BodyZ
Участник
Сообщения: 70
Зарегистрирован: 04 сен 2009, 16:47
Репутация: 1

Re: Добавление полигона

Сообщение BodyZ »

Здравствуйте.
Привел код к такому виду

Код: Выделить всё

           //Function is designed to work with polyline data
            if (featureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon) { return; }
            //Create the Feature Buffer       
            IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
            //Create insert Feature Cursor using buffering = true.       
            IFeatureCursor featureCursor = featureClass.Insert(true);
            object featureOID;
            ESRI.ArcGIS.Geometry.IGeometryCollection geometrycollection = new ESRI.ArcGIS.Geometry.PolygonClass();
            ESRI.ArcGIS.Geometry.IPointCollection pointcollection = new ESRI.ArcGIS.Geometry.RingClass();
            ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass();
            object obj = Type.Missing;
            for (int i = 0; i < 100; i++)
            {
                point.PutCoords(10 + i * 10, 0 + i * 10);
                pointcollection.AddPoint(point,ref obj,ref obj);
                point.PutCoords(0 + i * 10,10 + i * 10);
                pointcollection.AddPoint(point, ref obj, ref obj);
            }
            ESRI.ArcGIS.Geometry.IRing ring = pointcollection as IRing;
            ring.Close();
            geometrycollection.AddGeometry(ring,ref obj,ref obj);
            featureBuffer.Shape = geometrycollection.get_Geometry(0);
            featureOID = featureCursor.InsertFeature(featureBuffer);
            //Flush the feature cursor to the database        
            featureCursor.Flush();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
Возникла проблема. Компилятор ошибку не выдает но программа слетает на строке
:!: featureBuffer.Shape = geometrycollection.get_Geometry(0); :?:
Подскажите что можно сделать. Заранее спасибо
Аватара пользователя
Дмитрий Барышников
Гуру
Сообщения: 2572
Зарегистрирован: 17 ноя 2009, 19:17
Репутация: 261
Откуда: Москва

Re: Добавление полигона

Сообщение Дмитрий Барышников »

Так делать нельзя. featureBuffer.Shape принимает геометрию высшего порядка (Point, PolyLine, Polygon), а вы пытаетесь передать Ring. Нужно featureBuffer.Shape = geometrycollection; Или как то так.
Может преобразование сделать типа IGeometry somegeom = geometrycollection; а потом попробовать присвоить featureBuffer.Shape = somegeom;
BodyZ
Участник
Сообщения: 70
Зарегистрирован: 04 сен 2009, 16:47
Репутация: 1

Re: Добавление полигона

Сообщение BodyZ »

Спасибо большое за помощь.
Привел тип и все заработало.
Аватара пользователя
Дмитрий Барышников
Гуру
Сообщения: 2572
Зарегистрирован: 17 ноя 2009, 19:17
Репутация: 261
Откуда: Москва

Re: Добавление полигона

Сообщение Дмитрий Барышников »

Вы бы рабочий кусочек кода сюда запостили, так сказать для будущих поколений.
BodyZ
Участник
Сообщения: 70
Зарегистрирован: 04 сен 2009, 16:47
Репутация: 1

Re: Добавление полигона

Сообщение BodyZ »

Рабочий вариант функции, которая добавляет полигон к слою

Код: Выделить всё

            IFeatureClass featureClass = pFtLayer.FeatureClass;
            //Function is designed to work with polyline data
            if (featureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon) { return; }
            //Create the Feature Buffer       
            IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
            //Create insert Feature Cursor using buffering = true.       
            IFeatureCursor featureCursor = featureClass.Insert(true);
            object featureOID;
            ESRI.ArcGIS.Geometry.IGeometryCollection geometrycollection = new ESRI.ArcGIS.Geometry.PolygonClass();
            ESRI.ArcGIS.Geometry.IPointCollection pointcollection = new ESRI.ArcGIS.Geometry.RingClass();
            ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass();
            object obj = Type.Missing;
            point.PutCoords(0,0);
            pointcollection.AddPoint(point, ref obj, ref obj);
            point.PutCoords(0,100);
            pointcollection.AddPoint(point, ref obj, ref obj);
            point.PutCoords(100, 100);
            pointcollection.AddPoint(point, ref obj, ref obj);
            point.PutCoords(100,0);
            pointcollection.AddPoint(point, ref obj, ref obj);
            ESRI.ArcGIS.Geometry.IRing ring = pointcollection as IRing;
            ring.Close();
            geometrycollection.AddGeometry(ring, ref obj, ref obj);
            IGeometry someGeometry = geometrycollection as IGeometry;
            featureBuffer.Shape = geometrycollection as IGeometry;
            featureOID = featureCursor.InsertFeature(featureBuffer);
            //Flush the feature cursor to the database        
            featureCursor.Flush();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
Ответить

Вернуться в «ArcGIS»

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и 3 гостя