Select to view content in your preferred language

Unable to apply CSV file in CGA rules, CGA rules display errors

454
4
11-15-2024 01:02 AM
Yu_Tung_Lin0
New Contributor

I am currently using CityEngine for a project and need to import a CSV file into CGA rules for application. However, I have encountered issues with CGA rules in CityEngine and have been unable to resolve them. I am reaching out for your assistance.

Below are the requirements for the project:

  1. I need the CGA rule to successfully read and apply data from the CSV file.
  2. My CSV file contains 5 parameters (Buildingoffset, Height, ArcadeSetback, Streetoffset, and GreenCoverPercentage) with 75 sample records, as shown in the attached file.
  3. I would like the CGA rule to randomly select and apply one of the 75 samples.

Currently, no matter how I modify the CGA rules, different parts of the code keep displaying an "unexpected token" error, as shown in the attached image. I am unsure of the source of the issue, so I would like to ask if there are any errors in my CGA rule code or if there is a recommended way to write it.

Here is my current CGA rule code:

const filePath = "75sampling_results.csv"
const data = readStringTable(filePath)
const numSamples = 75 // Our sample range is from row 2 to row 76

// Define attributes
attr Buildingoffset = 0
attr Height = 0
attr ArcadeSetback = 0
attr Streetoffset = 0
attr GreenCoverPercentage = 0

// Read the CSV file and randomly select a sample
@StartRule
Lot -->
randomSelectSample()

// Randomly select a row from the CSV file and set attributes
randomSelectSample -->
setAttributes(rand(1, numSamples) + 1) // Randomly select rows 2 to 76

// Set attributes
setAttributes(index) -->
Buildingoffset = toFloat(data[index][0])
Height = toFloat(data[index][1])
ArcadeSetback = toFloat(data[index][2])
Streetoffset = toFloat(data[index][3])
GreenCoverPercentage = toFloat(data[index][4])
generateBuilding()

// Generate the building using attributes
generateBuilding -->
offset(Buildingoffset)
extrude(Height)
applyArcadeSetback()
applyStreetoffset()
applyGreenCover()

// Apply ArcadeSetback
applyArcadeSetback -->
setback(ArcadeSetback)

// Apply Streetoffset
applyStreetoffset -->
setback(Streetoffset)

// Apply GreenCoverPercentage
applyGreenCover -->
case GreenCoverPercentage > 0:
color(0, GreenCoverPercentage / 100, 0)
extrude(0.1)
else:
nil

No matter how many times I modify and rewrite the rules, I continue to encounter similar issues. I apologize for the inconvenience and would be grateful if you could advise me on where the error might be or how I should structure this CGA rule. Thank you!

0 Kudos
4 Replies
ThomasFuchs
Esri Regular Contributor

Hello @Yu_Tung_Lin0 

Thank you for your question. I looked into the CGA code you provided and found several issues:

  1. Setting attributes needs the set operation—ArcGIS CityEngine Resources | Documentation
  2. When addressing elements in an array, this is how to use the index operator—ArcGIS CityEngine Resources | Documentation
  3. The rand function returns floats, for addressing first convert to integer using rint function—ArcGIS CityEngine Resources | Documentation

The unexpected token errors are resolved in this version of the setAttributes(index) rule above:

// Set attributes
setAttributes(i) -->
	set(Buildingoffset, float(data[rint(i),0]))
	set(Height, float(data[rint(i),1]))
	set(ArcadeSetback, float(data[rint(i),2]))
	set(Streetoffset, float(data[rint(i),3]))
	set(GreenCoverPercentage, float(data[rint(i),4]))
	generateBuilding()

 

I hope this helps you completing your project successfully 😊

 

0 Kudos
Yu_Tung_Lin0
New Contributor

Thank you very much for your prompt response. After rewriting the rules according to your suggestions, the issues with setAttributes have been resolved. However, new unexpected token errors have appeared in the applyStreetoffset and applyGreenCover sections (as shown in the attached image). Could you please advise how these parts of the CGA rules should be modified?

Here are the CGA rules code that have errors:

// Apply Streetoffset
applyStreetoffset -->
setback(Streetoffset)

// Apply GreenCoverPercentage
applyGreenCover -->
case GreenCoverPercentage > 0:
color(0, GreenCoverPercentage / 100, 0)
extrude(0.1)
else:
nil

I apologize for asking again, and thank you so much for your help!

0 Kudos
desert
by
Occasional Contributor

I have modified the code you provided, but I believe it might not produce the intended result. The current outcome is due to applying setbacks simultaneously to a single parcel. It is better to use the 'selector' to distinguish areas for the offset and setback commands, and apply setbacks step by step while progressing through the rule for each area.

desert_0-1731735329705.png

const data = readStringTable("data/75sampling_results.csv")
const numSamples = 75 // Our sample range is from row 2 to row 76

// Define attributes
attr Buildingoffset = 0
attr Height = 0
attr ArcadeSetback = 0
attr Streetoffset = 0
attr GreenCoverPercentage = 0

// Read the CSV file and randomly select a sample
@StartRule
Lot -->
randomSelectSample()

// Randomly select a row from the CSV file and set attributes
randomSelectSample -->
setAttributes(rand(1, numSamples) + 1) // Randomly select rows 2 to 76

// Set attributes
setAttributes(i) -->
set(Buildingoffset,float(data[rint(i),0]))
set(Height, float(data[rint(i),1]))
set(ArcadeSetback, float(data[rint(i),2]))
set(Streetoffset, float(data[rint(i),3]))
set(GreenCoverPercentage, float(data[rint(i),4]))
generateBuilding

// Generate the building using attributes
generateBuilding -->
	offset(Buildingoffset)
	extrude(Height)
	print(Height)
	applyArcadeSetback
	applyStreetoffset
	applyGreenCover

// Apply ArcadeSetback
applyArcadeSetback -->
setback(ArcadeSetback){all = ARC_offset. | remainder : X.}

applyStreetoffset -->
setback(Streetoffset){all = ST_offset. | remainder : X.}

// Apply GreenCoverPercentage
applyGreenCover -->
case GreenCoverPercentage > 0:
color(0, GreenCoverPercentage / 100, 0)
extrude(0.1)
else: NIL

I hope this helps.



ThomasFuchs
Esri Regular Contributor

Thanks @desert for providing more help.

At this point it also needs to be mentioned, that those "unexpected token" errors generally occur when using a wrong syntax in a previous code statement. It is always a good idea to consult the online help. Usually there are also code snippets provided that show common usage.

ThomasFuchs_0-1731943335413.png

Also the code completion feature (hit <ctrl + space> while typing) can help finding the right syntax.

ThomasFuchs_1-1731943712936.png

 



setback operation—ArcGIS CityEngine Resources | Documentation

0 Kudos