Reading a DBF file and extracting data with Python in City Engine 2019

4045
3
Jump to solution
03-18-2021 01:51 AM
CL11
by
New Contributor

I am trying to use Python in City Engine 2019 to read the DBF file of an SHP that is imported into City Engine

I tried to install the simpledbf python module, but Python in City Engine gives an error on execution, 

 

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "testdbf.py", line 9, in <module>
    import simpledbf
ImportError: No module named simpledbf

 

 

Is there something I have to do to include external python modules in order to work with City Engine Python?

0 Kudos
1 Solution

Accepted Solutions
CL11
by
New Contributor

Thanks for the suggestion. I tried making a scripting.py with the import sys and sys.path.append and import of simpledbf  and placed that in the project root folder, then re-started city Engine. 

Unfortunately, it still gives the same error on execution for simpledbf.  I also tried just doing the myHelper.py example and it also did not work (perhaps this solution is for the Python console and not for scripts.)

I also tried moving simpledbf into my project's scripts directory. No luck.

The final solution:

In my python script itself,

I added the lines - 

import sys

sys.path.append (with the path being my project scripts directory)

from simpledbf import Dbf5

 

And then I put simpledbf.py in that scripts directory. 

This did work - I was able to read DBF's and query values.

As far as Fiona - I would like to use it, unfortunately, City Engine's python is using Jython 2.7 while Fiona requires CPython 3.

View solution in original post

3 Replies
jcarlson
MVP Esteemed Contributor

My Esri/Python experience doesn't extend into CityEngine much, as I only used it for a single project a couple years ago. Looking at the docs, however, it does look like you could get an external module brought in by using the scripting.py file.

Typical use cases include adding your scripting directory to the system path, or loading your custom modules to the scripting environment.

Tutorial 10 goes a step further and gives a concrete example:

To extend scripting.py, complete the following steps:

  1. Create a new file scripting.py in your CityEngine workspace using the file browser of your operating system.
  2. Add the following lines to automatically map your helper script at startup:
import sys

sys.path.append({PATH_TO_YOUR_SCRIPTS_DIRECTORY})
// e.g. sys.path.append("C:\user\CityEngine\MyProject\scripts")
import myHelpers

I would presume that the path being appended could as easily point to a module somewhere.

Side note: have you checked out the python module Fiona? It's fantastic for working with SHPs and DBFs.

- Josh Carlson
Kendall County GIS
0 Kudos
CL11
by
New Contributor

Thanks for the suggestion. I tried making a scripting.py with the import sys and sys.path.append and import of simpledbf  and placed that in the project root folder, then re-started city Engine. 

Unfortunately, it still gives the same error on execution for simpledbf.  I also tried just doing the myHelper.py example and it also did not work (perhaps this solution is for the Python console and not for scripts.)

I also tried moving simpledbf into my project's scripts directory. No luck.

The final solution:

In my python script itself,

I added the lines - 

import sys

sys.path.append (with the path being my project scripts directory)

from simpledbf import Dbf5

 

And then I put simpledbf.py in that scripts directory. 

This did work - I was able to read DBF's and query values.

As far as Fiona - I would like to use it, unfortunately, City Engine's python is using Jython 2.7 while Fiona requires CPython 3.

jcarlson
MVP Esteemed Contributor

Great detective work! I'm sure the next person to have this issue will be glad you took the time to post the solution.

- Josh Carlson
Kendall County GIS
0 Kudos