[gdal-dev] How to edit a .dxf file?

Even Rouault even.rouault at mines-paris.org
Fri Mar 2 13:51:00 EST 2012


Le vendredi 02 mars 2012 18:26:52, huahu a écrit :
> Hi,
> I try to edit in this way, but it can not work,although it can save
> geometry,but it cannot save feature.

The DXF driver doesn't support update of existing files. It supports reading a 
file or writing a new one. So you'll have to iterate over the source file 
features and write them in a new DXF file.

> My purpose is  alter the previous file,and copy a DataSource to create a
> new file.
> 
> 
> OGRRegisterAll();
> OGRDataSource *poDS = OGRSFDriverRegistrar::Open( srcname.c_str(), FALSE );
>  int layercount=poDS->GetLayerCount();
> 
> for(int num=0;num<layercount;num++)
>  {
>     OGRLayer* poLayer = poDS->GetLayer(num);
>     OGRFeature * pFeature;
>      poLayer->ResetReading();
> 
>         while ((pFeature=poLayer->GetNextFeature())!=NULL)
> {
>    OGRGeometry* pGeometry = pFeature->GetGeometryRef();
>    OGRwkbGeometryType geoType = pGeometry->getGeometryType();
>    if (pGeometry != NULL &&wkbPoint==geoType)
>    {
>                //change the coordinate value
>  OGRPoint *poPoint = (OGRPoint *) pGeometry;
>  double pointx=poPoint->getX()-X;
>  double pointy=poPoint->getY()-Y;
>  poPoint->setX((pointx*cos(e)-pointy*sin(e))+X);
>  poPoint->setY((pointx*sin(e)+pointy*cos(e))+Y);
>    }
>    else if(pGeometry != NULL &&wkbLineString==geoType)
>    {
>  OGRLineString* pLineGeo = (OGRLineString*)pGeometry;
>  int linestringsize=pLineGeo->getNumPoints();
>  for(int i=0;i<linestringsize;i++)
>  {
>      double
> pointx=((pLineGeo->getX(i)-X)*cos(e*PI/180.0)-(pLineGeo->getY(i)-Y)*sin(e*P
> I/180.0))+X; double
> pointy=((pLineGeo->getX(i)-X)*sin(e*PI/180.0)+(pLineGeo->getY(i)-Y)*cos(e*P
> I/180.0))+Y; pLineGeo->setPoint(i,pointx,pointy);
>   }
>    }
>     else if( pGeometry != NULL &&wkbPolygon==geoType)
>    {
>    OGRPolygon *pPolygon=(OGRPolygon*)pGeometry;
>    OGRLinearRing *ogrlring=pPolygon->getExteriorRing();
>    int lenth=ogrlring->getNumPoints();
>    for(int i=0;i<lenth;i++)
>   {
>  double
> pointx=((ogrlring->getX(i)-X)*cos(e*PI/180.0)-(ogrlring->getY(i)-Y)*sin(e*P
> I/180.0))+X; double
> pointy=((ogrlring->getX(i)-X)*sin(e*PI/180.0)+(ogrlring->getY(i)-Y)*cos(e*P
> I/180.0))+Y; ogrlring->setPoint(i,pointx,pointy);
>   }
>    }
>    else
>    {
>    }
>  poLayer->SetFeature(pFeature);   //it can not save feature
> 
>  }
>  }
> OGRSFDriver* poDriver =
> OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("ESRI Shapefile");
>  OGRDataSource* desDS = poDriver->CopyDataSource(poDS,desname.c_str(),0);
> OGRDataSource::DestroyDataSource( desDS );
>  OGRDataSource::DestroyDataSource( poDS );
>        OGRCleanupAll();
> 
> 
> Can you help me?
> 
> Thank you!
> 
> Cheers,
> Huahu


More information about the gdal-dev mailing list