Read data from a file

390
18
Jump to solution
03-16-2012 09:18 AM
CharalamposApostolou
New Contributor II
Is there a way to read data from a file? I have a document file with some attributes in each line and i want to read some attributes and build objects according to that.
0 Kudos
1 Solution

Accepted Solutions
MatthiasBuehler1
Frequent Contributor II
hi !


as follows :

- create a file with you data, e.g. info.txt
- put that file into your project's data folder
- copy the attached python script into your project's scripts folder
- open the script (double click)
- edit the absolute path to your into.txt file so it works on your machine.

- create a shape with StartRule 'Lot' (not Lot50 !)
- open it's Object Attributes tab, right-click into it. create an object attr called 'buildingID', with a float value of 49.

- select your shapes of interest on which you have set all buildingIDs.
- run the python script via F9 (python editor must be active view, click on the code)


what happens is :
- python checks the building ID, which is an object attr you have set for each building.
- the list is parsed until the first entry of the line is the building ID.
- that line is given back
- a new object attr for 'rooms', 'arch' and 'yard' is created with the values given in that line.

once you have those attributes now set, you can access them via CGA. initialize each object attr of interest in the CGA code by using :

attr rooms = 0
attr arch = 0
attr yard = 0

make sure you then POINT TO the object attr value and not to the rule value ( which is initialized with 0 ) using the connection editor in the inspector (the little rectangle icon with the triangle).


let me know if it works or if you have questions !

View solution in original post

0 Kudos
18 Replies
MatthiasBuehler1
Frequent Contributor II
hi !

it depends on the file type .. what file is it ?

let me know ..
0 Kudos
CharalamposApostolou
New Contributor II
Hi. It is just a text file (.txt)
0 Kudos
MatthiasBuehler1
Frequent Contributor II
hi !


to do this, you'd need the CityEngine Advanced version and use it's Python Scripting functionality to 1] parse the file for the content, then 2] assign the values to the shapes.

it may be easier to bring them in via a format which already supports such attribute metadata, such as .shp or Esri's GDB.


what's your source application from where you get the .txt data files ?
0 Kudos
CharalamposApostolou
New Contributor II
Thanks very much for the reply,
I created the file. I have some shapes with a number id and in the file according to the id there are informations about the houses that are going to build. The text file is rows  and columns with integer numbers. Is there a tutorial to read?
0 Kudos
MatthiasBuehler1
Frequent Contributor II
hi ..

can you post a little example based on 2 shapes with their related text file ?


let me know ..
0 Kudos
CharalamposApostolou
New Contributor II
i have a Lot with the name Lot50, and a txt file with the following format:

id     rooms      yard      arch    ....
48     2             0          1
49     1             0          1
50     2             1          0
51     4             0          1

and i want to search in the txt file to find Lot50 and then build in the lot what it describes in the txt file. For example in Lot50 its going to build 2 rooms, a yard and no arch.
0 Kudos
MatthiasBuehler1
Frequent Contributor II
hi !


as follows :

- create a file with you data, e.g. info.txt
- put that file into your project's data folder
- copy the attached python script into your project's scripts folder
- open the script (double click)
- edit the absolute path to your into.txt file so it works on your machine.

- create a shape with StartRule 'Lot' (not Lot50 !)
- open it's Object Attributes tab, right-click into it. create an object attr called 'buildingID', with a float value of 49.

- select your shapes of interest on which you have set all buildingIDs.
- run the python script via F9 (python editor must be active view, click on the code)


what happens is :
- python checks the building ID, which is an object attr you have set for each building.
- the list is parsed until the first entry of the line is the building ID.
- that line is given back
- a new object attr for 'rooms', 'arch' and 'yard' is created with the values given in that line.

once you have those attributes now set, you can access them via CGA. initialize each object attr of interest in the CGA code by using :

attr rooms = 0
attr arch = 0
attr yard = 0

make sure you then POINT TO the object attr value and not to the rule value ( which is initialized with 0 ) using the connection editor in the inspector (the little rectangle icon with the triangle).


let me know if it works or if you have questions !
0 Kudos
CharalamposApostolou
New Contributor II
Something is going wrong now. It gives me this exception:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Xaralampos Apostolou\CityEngine\Python_Example\scripts\getListData.py", line 38, in <module>
buildingID = int (ce.getAttribute(shape, 'buildingID')) # get building ID
TypeError: int() argument must be a string or a number

at org.python.core.PyException.fillInStackTrace(PyException.java:70)
at java.lang.Throwable.<init>(Throwable.java:181)
at java.lang.Exception.<init>(Exception.java:29)
at java.lang.RuntimeException.<init>(RuntimeException.java:32)
at org.python.core.PyException.<init>(PyException.java:46)
at org.python.core.PyException.<init>(PyException.java:43)
at org.python.core.PyException.<init>(PyException.java:61)
at org.python.core.Py.TypeError(Py.java:195)
at org.python.core.PyInteger.asPyInteger(PyInteger.java:84)
at org.python.core.PyInteger.int_new(PyInteger.java:41)
at org.python.core.PyInteger$exposed___new__.new_impl(Unknown Source)
at org.python.core.PyType.invoke_new_(PyType.java:432)
at org.python.core.PyType.type___call__(PyType.java:1397)
at org.python.core.PyType.__call__(PyType.java:1388)
at org.python.core.PyObject.__call__(PyObject.java:397)
at org.python.core.PyObject.__call__(PyObject.java:401)
at org.python.pycode._pyx70.f$0(C:\Users\Xaralampos Apostolou\CityEngine\Python_Example\scripts\getListData.py:37)
at org.python.pycode._pyx70.call_function(C:\Users\Xaralampos Apostolou\CityEngine\Python_Example\scripts\getListData.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1204)
at org.python.core.Py.exec(Py.java:1248)
at org.python.pycode._pyx73.f$0(<string>:1)
at org.python.pycode._pyx73.call_function(<string>)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1204)
at org.python.core.Py.exec(Py.java:1248)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:172)
at org.python.pydev.jython.JythonPlugin.exec(Unknown Source)
at org.python.pydev.jython.JProcess.run(Unknown Source)
0 Kudos
MatthiasBuehler1
Frequent Contributor II
hmm.

what datatype is your buildingID ? did you create it as float or string ? ( CityEngine does not have ints, just floats for use with CGA )

are you in general familiar with Python scripting or is this the first time ?

let me know .. ;)
0 Kudos