GDAL1.9 写DXF填充多边形

          昨天下载了GDAL1.9.2。意外发现OGRDXFWriterLayer类中多了一个OGRErr WriteHATCH( OGRFeature *poFeature,  OGRGeometry *poGeom )函数,而在OGRErr CreateFeatureOGRFeature *poFeature )函数中使用WriteHATCH条件是:

if( eGType == wkbPolygon || eGType == wkbMultiPolygon )

    {

        if( bWriteHatch )

            return WriteHATCH( poFeature );

        else

            return WritePOLYLINE( poFeature );

}

       而在OGRDXFWriterLayer的构造函数中有bWriteHatch = CSLTestBoolean(CPLGetConfigOption("DXF_WRITE_HATCH""YES"));

        所以生成HATCH条件是要素必须是多边形或者是多多边形,并且要设置CPLSetConfigOption("DXF_WRITE_HATCH""YES")。下面来试一下吧。

绘制一个红色的四边形试试:

#include "ogrsf_frmts.h"
#include "stdio.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	const char *pszDriverName = "DXF";
	OGRSFDriver *poDriver;

	CPLSetConfigOption("DXF_WRITE_HATCH", "YES"); // 设置绘制填充多边形

	RegisterOGRDXF();

	poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
		pszDriverName );
	if( poDriver == NULL )
	{
		printf( "%s driver not available.\n", pszDriverName );
		exit( 1 );
	}

	OGRDataSource *poDS;
	// 模板文件名称,文件在GDAL中data下,拷贝到与cpp同目录下,按下设置,也可以设置一个正确的相对路径和绝对路径
	char **papszOptions = (char **)CPLCalloc(sizeof(char *),3);
	papszOptions[0] = "HEADER=header.dxf";
	papszOptions[1] = "TRAILER=trailer.dxf";
	poDS = poDriver->CreateDataSource( "hatch_out.dxf", papszOptions );
	CPLFree(papszOptions);
	if( poDS == NULL )
	{
		printf( "Creation of output file failed.\n" );
		exit( 1 );
	}

	OGRLayer *poLayer;

	poLayer = poDS->CreateLayer( "hatch_out", NULL, wkbUnknown, NULL );
	if( poLayer == NULL )
	{
		printf( "Layer creation failed.\n" );
		exit( 1 );
	}

	OGRFeature *poFeature;

	poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
	poFeature->SetField("Layer","正方形");

	// 生成一个正方形
	OGRPolygon oPolygon;
	OGRLinearRing oLinearRing;
	oLinearRing.addPoint(0.0,0.0);
	oLinearRing.addPoint(1.0,0.0);
	oLinearRing.addPoint(1.0,1.0);
	oLinearRing.addPoint(0.0,1.0);
	oLinearRing.closeRings();
	oPolygon.addRing(&oLinearRing);

	poFeature->SetGeometry( &oPolygon );
	// 设置样式字符串,第一个样式必须是画刷
	char strStyle[256];
	sprintf(strStyle,"BRUSH(fc:#%02x%02x%02x)",
		255, // 红色分量
		0,   // 绿色分量
		0);  // 蓝色分量

	poFeature->SetStyleString( strStyle );

	if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
	{
		printf( "Failed to create feature in shapefile.\n" );
		exit( 1 );
	}

	OGRFeature::DestroyFeature( poFeature );

	OGRDataSource::DestroyDataSource( poDS );
}


 

运行程序生成一个hetch_out.dxf文件使用AUTO CAD打开:

oPolygon.addRing(&oLinearRing);后面添加下面代码:

// 添加一个内环 也就是CAD中的孤岛

 OGRLinearRing oInteriorRing;

 oInteriorRing.addPoint(0.2,0.2);

 oInteriorRing.addPoint(0.2,0.4);

 oInteriorRing.addPoint(0.4,0.4);

 oInteriorRing.closeRings();

 oPolygon.addRing(&oInteriorRing);

 

运行程序,(主要要把上面用AUTO CAD打开的hetch_out.dxf关掉,不然会创建数据源失败)生成hetch_out.dxf文件使用AUTO CAD打开:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Python GDAL库可以用于读取和DXF文件。要使用GDAL库操作DXF文件,你需要首先安装GDAL库。安装方法可以参考GDAL官方文档或者使用pip安装。 安装完成后,你可以使用以下代码来读取DXF文件: ```python from osgeo import ogr def read_dxf_file(file_path): dxf_driver = ogr.GetDriverByName('DXF') dxf_dataset = dxf_driver.Open(file_path, 0) if dxf_dataset is None: print("Failed to open DXF file") return layer_count = dxf_dataset.GetLayerCount() for i in range(layer_count): layer = dxf_dataset.GetLayerByIndex(i) feature_count = layer.GetFeatureCount() for j in range(feature_count): feature = layer.GetFeature(j) geometry = feature.GetGeometryRef() # Do something with the geometry object dxf_dataset.Destroy() # 例子: 读取DXF文件 read_dxf_file('path/to/your/file.dxf') ``` 以上代码通过GDAL库打开DXF文件并遍历所有图层和要素,并返回几何对象,你可以根据自己的需求进行处理。 如果你需要DXF文件,可以使用以下代码: ```python from osgeo import ogr def write_dxf_file(file_path, geometries): dxf_driver = ogr.GetDriverByName('DXF') dxf_dataset = dxf_driver.CreateDataSource(file_path) if dxf_dataset is None: print("Failed to create DXF file") return for geometry in geometries: layer = dxf_dataset.CreateLayer('', None, ogr.wkbUnknown) feature_def = layer.GetLayerDefn() feature = ogr.Feature(feature_def) feature.SetGeometry(geometry) layer.CreateFeature(feature) dxf_dataset.Destroy() # 例子:DXF文件 point1 = ogr.Geometry(ogr.wkbPoint) point1.AddPoint(10, 20) point2 = ogr.Geometry(ogr.wkbPoint) point2.AddPoint(30, 40) geometries = [point1, point2] write_dxf_file('path/to/your/output.dxf', geometries) ``` 以上代码创建了一个DXF文件并入两个点要素。你可以根据自己的需求创建不同类型的几何对象并DXF文件。 请注意,GDAL库也支持其他常见的矢量文件格式,如Shapefile,你可以根据需要选择适合的文件格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值