上篇文章介绍了CityEngine + Python自动化建模的原理,本篇文章给出自动化建模的实际代码(代码已经过实际验证,可放心使用),将对应内容修改为自己项目中的实际内容即可。

如果你喜欢本文,欢迎收藏、分享和转载,转载请注明出处:https://blog.csdn.net/shaxiaozilove/article/details/116903530

如果你有任何问题,欢迎与我联系和沟通,谢谢。

下面是自动化建模全面和详细的代码,欢迎参考:

'''
Created on 2020-10-27@author: QinC
'''
from scripting import *
import os
import math# get a CityEngine instance
ce = CE()def isFileExist(f_path):return os.path.exists(f_path)# create a new scene and set it's spatial reference system
def createScene(scene_name, wkid):if not scene_name.endswith('.cej'):scene_name = scene_name + '.cej'ce.newFile('scenes/' + scene_name)ce.setSceneCoordSystem(wkid)print 'create secne and set srs successfully'# import spefify layer from gdb
def importShapeFromGDB(gdb_path, dataset):absolute_path = ce.toFSPath(gdb_path)if not isFileExist(absolute_path):print 'path of gdb not exist: %s' % absolute_pathreturnif not isinstance(dataset, list):print 'parameter type of dataset should be list: %s' % str(dataset)returnif not len(dataset):print 'at least one feature class should be imported: %s' % str(dataset)returngdbSettings = FGDBImportSettings()gdbSettings.setDatasetFilter(dataset)ce.importFile(ce.toFSPath(gdb_path), gdbSettings, False)ce.setSelection(None)print 'import shape successfully''''first, set cga rule file and start rule for scene whole shapesnext, set cag's parameter Floor from Object(connect Floor to layer's attribute Floor)then, set new name for object if specify a valid field_namecga_path: relative path of cgastart_rule: start rule, string type
'''
def setRule(cga_path, start_rule):# judge if the cga_path exist by absolute pathif not isFileExist(ce.toFSPath(cga_path)):print 'the specified cga file not exist: %s' % cga_pathreturnshapes = ce.getObjectsFrom(ce.scene, ce.isShape)ce.setRuleFile(shapes, cga_path, True)ce.setStartRule(shapes, start_rule)print 'set rule file and start rule successfully''''if attr in cga file has the same name with one of layer's attribute table fields, then it's attr source is Object default and value is sameso, if you want to get value from layer attribute table, it is import to set attr name in cgathe next line is not necessary'''ce.setAttributeSource(shapes, '/ce/rule/Floor', 'Object')print 'set Floor attr source successfully'ce.saveFile()'''set shape name use layer's field valueshapes: shaped to set new namefield_name: the field to set shape's name
'''
def setObjectName(shapes, field_name):for shape in shapes:ce.setName(shape, ce.getAttribute(shape, field_name))
'''export modelsformat: format to export, eg. gdb, gltf, glb ...path: where the model to export, use relative pathbase_name: base name of the exported filefield_name: source layer's field name, used to reset object nameadmin_codes: admin codes of a city, generate model of this area each time, default value is empty list,if not assigned or pass empty list, generate and export a certain number of model each timecertain_number: the number of models generate and export each time
'''
def exportModels(format, path, base_name, field_name = None, admin_codes = [], certain_number = 10000):shapes = ce.getObjectsFrom(ce.scene, ce.isShape)if field_name and len(admin_codes) > 0:# set a new name for object(here is shapes)setObjectName(shapes, field_name)print 'set object name with category code successfully'# generate and export model by category code        for code in admin_codes:# filter scene shapes with nameclassified_shapes = ce.getObjectsFrom(ce.scene, ce.isShape, ce.withName('"' + code + '"'))doGenerateAndExport(classified_shapes, format, path, base_name, code)print 'generate and export models successfully with field name [%s], codes %s.' % (field_name, str(admin_codes))else:# export certain number model each timelength = len(shapes)count = int(math.floor(length / certain_number))for i in range(count):certain_shapes = shapes[i * certain_number: (i + 1) * certain_number]doGenerateAndExport(certain_shapes, format, path, base_name, i + 1)# left models to process(number should be int, not floor)left_shapes = shapes[count * certain_number: length]doGenerateAndExport(left_shapes, format, path, base_name, count + 1)print 'generate and export models with certain number each time successfully, and total %d parts.' % (count + 1)ce.saveFile()'''genarate and export model, you can specify format, path and base nameshapes: objects to generate modelformat: format to export, eg. gdb, gltf, glb ...path: where the model to export, use relative pathbase_name: base name of the exported filesuffix: used as suffix of base name to distingush different part, default value is 1
'''
def doGenerateAndExport(shapes, format, path, base_name, suffix = 1):models = ce.generateModels(shapes, True, False)if len(models):exportSetting = getExportSetting(format, path, base_name, suffix)ce.export(models, exportSetting, False)print 'models of part %d has generate and export successfully' % suffixelse:print 'models of part %d has no model to generate and export' % suffix# get export setting info
def getExportSetting(format, path, f_name, suffix):if not f_name.endswith('.' + format):f_name = f_name + '_' + str(suffix) +  '.' + formatelse:f_name = os.path.splitext(f_name)[0] + '_' + str(suffix) + '.' + formatexportSettings = FGDBExportModelSettings()if format == 'gdb':exportSettings.setOutputPath(ce.toFSPath(path) + '/')           exportSettings.setGeodatabaseName(f_name)elif format == 'gltf' or format == 'glb':exportSettings = GLTFExportModelSettings()exportSettings.setOutputPath(ce.toFSPath(path))exportSettings.setBaseName(f_name)return exportSettingsif __name__ == '__main__':   scene_name = 'wuhan_auto_cga.cej'# coordinate system code, eg 3857 4547wkid = 'EPSG:4547'gdb_path = 'data/building.gdb'dataset = ['/wuhan_for_local_test']cga_path = 'rules/wuhan_blank.cga'start_rule = 'Lot'field_name = 'Adcode'# admin code of wuhanadcodes = ['420102', '420103', '420104', '420105', '420106', '420107', '420111', '420112', '420113', '420114', '420115', '420116', '420117']certain_number = 10000export_path = 'models'base_name = 'wuhan'createScene(scene_name, wkid)importShapeFromGDB(gdb_path, dataset)setRule(cga_path, start_rule)exportModels('gdb', export_path, base_name, certain_number = 5000)ce.refreshWorkspace()

CityEngine+Python自动化建模实现【系列文章之四】相关推荐

  1. 基于阿里云服务器环境搭建到项目上线系列文章之四——安装composer

    基于阿里云服务器环境搭建到项目上线系列 前言:最近购买了域名和一台阿里云服务器准备做点东西放上去,所以准备把环境搭建到项目上线的过程记录下来,计划一个系列6篇文章 基于阿里云服务器环境搭建到项目上线系 ...

  2. 【转】python开发大全、系列文章、精品教程

    版权声明:本文为博主原创文章,转载请注明来源.开发合作联系luanpenguestc@sina.com https://blog.csdn.net/luanpeng825485697/article/ ...

  3. Scott的ASP.net MVC框架系列文章之四:处理表单数据(2)

    前几周我发表了一系列文章介绍我们正在研究的ASP.NET MVC框架.ASP.NET MVC框架为你提供了一种新的开发Web应用程序的途径,这种途径可以让应用程序变得更加层次清晰,而且更加有利于对代码 ...

  4. Python编程思想【系列文章】

    <Python编程思想>专栏 本系列文章持续更新中....... 李宁老师已经在「极客起源」 微信公众号推出<Python编程思想>电子书,囊括了Python的核心技术,以及P ...

  5. python开发大全、系列文章、精品教程

    全栈工程师开发手册 (作者:栾鹏) python教程全解 python基础教程 python基础系列教程--Python的安装与测试:python解释器.PyDev编辑器.pycharm编译器 pyt ...

  6. Python实现决策树(系列文章1)--从最简单的算法开始

    从泰坦尼克号开始到决策树,千里之行始于coding. 1 初衷 初衷一:一直以来从网上各路大神的分享中受益匪浅,总也想有点feedback. 初衷二:很早就想好好写一写决策树(曾经用SAS写过ID3, ...

  7. AllenNLP系列文章之四:指代消解

    指代消解是自然语言处理的一大任务之一,它是信息抽取不可或缺的组成部分.在信息抽取中,由于用户关心的事件和实体间语义关系往往散布于文本的不同位置,其中涉及到的实体通常可以有多种不同的表达方式,例如某个语 ...

  8. Python实现决策树(系列文章6)-- 名义型变量属性值分割(修正)

    1 问题 决策树对于变量的竞争/选择分为两种: 变量间的比较选择(二分) 变量内的比较选择(二分) 首先,决策树选定一个变量,进行变量内的比较选择. 变量本身可能是N(Nominal, 名义型),O( ...

  9. Python字符串函数大全系列文章(三)

    1.join(iterable,/): 将一个迭代对象连接起来 >>> '-'.join(['hello','python','go']) 'hello-python-go' 2.l ...

  10. 一步步实现windows版ijkplayer系列文章之三——Ijkplayer播放器源码分析之音视频输出——音频篇

    https://www.cnblogs.com/harlanc/p/9693983.html 目录 OpenSL ES & AudioTrack 源码分析 创建播放器音频输出对象 配置并创建音 ...

最新文章

  1. 用stm32f10x建立新的工程重要步骤
  2. CSS和HTML做好看的界面
  3. NTU课程笔记 CE7454 (3):MLPCNN
  4. Boost:宏BOOST_TEST_GT的使用实例
  5. 操作系统的功能和特征
  6. pmp每日三题(2022年2月28日)
  7. C++ 对象的内 存布局(下)
  8. Mysql原理+ 多实例 +表损坏
  9. js实现城市名称拼音索引
  10. VSCode中Clangd无法找到stdio.h
  11. cat3 utp是不是网线_CAT网线品种分类
  12. 分清frontal plane(额状面)、coronal plane(冠状面)、transverse plane(横断面)、sagittal plane(矢状面)
  13. 数字藏品:传承优秀传统文化的新载体
  14. 华为服务器电源性能指标,华为服务器可服务性设计介绍-电源篇.PDF
  15. cpua55和a53哪个好_oppoa53和a55区别哪个好
  16. 小米路由器安装花生壳实现外网映射
  17. 微信开发之小程序实现倒计时
  18. 一文了解K8s-概念详解
  19. base64图裁剪 php_php图片上传类(支持缩放、裁剪、图片缩略功能)
  20. VIPS:基于视觉的Web页面分页算法

热门文章

  1. 发现最新的区块链应用-8月16日
  2. 小布助手对话短文本语义匹配
  3. win7 ShuipFCMS 配置 及问题
  4. html5 图片上传进度条,html5异步上传图片显示上传文件进度条
  5. linux文本三剑客演讲稿,Linux文本三剑客总结
  6. 迷茫时代的明白人——书摘
  7. [报错解决]CondaSSLError: OpenSSL appears to be unavailable on this machine. OpenSSL is required to downl
  8. CMake多版本共存
  9. mysql基于Python的影院会员管理系统的设计与实现毕业设计源码131621
  10. 信创办公--基于WPS的Word最佳实践系列(解决Word兼容性问题)