Tutorial 3: Map control

To access the tutorials in CityEngine, click Help > Download Tutorials and Examples.... After choosing a tutorial or example, the project is automatically downloaded and added to your workspace.

CGA parameters

Cities consist of a large number of objects. Controlling these by setting attributes of single buildings is tedious and close to impossible. In this tutorial, you'll first learn how CGA rule parameters are used.

  1. Open the mapcontrolTutorial_01.cej scene.

    The opened scene contains two map layers (Terrain and Water) and a street network layer with lots of shapes. If you select a single lot now and check its attributes in the Inspector (Window > Inspector), there are no rule parameters assigned (the Rule Parameter pane is empty). This changes when you assign a rule file to the lot.

  2. Select the Streetnetwork layer in the Scene Editor.
  3. Click Shapes > Assign Rule File, and select the simpleBuildingShells_01.cga rule file.
  4. Select a single lot again.

    Now there are attributes visible in the Inspector view, namely height.

    Inspector window showing attributes

  5. Where did they come from? As soon as you assign a rule file to a shape (in this case, a lot), the attributes of the rule file are visible as rule parameters of the lot. The Rule item in the Source column shows that the value is taken from the rule file.
  6. Click the Rule File link in the Inspector to open the assigned rule file.
  7. At the top, the attribute height is defined:
  8. This value is used in the rule file to define the height of the building.

    // height value
    attr height = 80
  9. Reselect the lot, and generate the building.
    The result is a building 80 meters high.
  10. Change the height value in the Inspector from 80 to 150.
    Note that the Rule item disappeared. The rule parameter height for this building is now overruled by the value you set. All other untouched lots still use the height value from the rule file.
  11. Regenerate the building, and note how the building height has changed.
  12. Your scene should look similar to the following screen shot:
    Screen shot of the building
    Alternatively, you can open the mapcontrolTutorial_02.cej scene to see the building result.

Control the skyline

In this section, you'll learn how to use maps to control the parameters of your cities.

Open the mapcontrolTutorial_02.cej scene if it's not already open.

Skyline map

If you generated some buildings with the current settings, your city would look similar to the following:

Screen shot of generated buildings

This is not very convincing, since all the buildings are the same height at 80 meters. A nice skyline would improve the look. Instead of setting the CGA shape attributes manually as you did in the first section, you'll use the red skyline map below to control the height of all buildings at once. The smaller image is the topography map, which indicates a lake in blue.

Red skyline map
Topography map

The red channel of the skyline map represents the height of the buildings in this area. When you compare the skyline map to the topography map, you'll see that a skyline forms around the corner of the lake. There is also a second center in the city at the top left.

  1. Create a new map layer by clicking Layer > New Map Layer > Mapping.
  2. Choose skylineMap.png as the mapping file.
  3. Deselect the Keep ratio button.
  4. Change the X and Z sizes to 3000.
  5. Make sure the alignment is set to centered (clicking the alignment icon changes the mode).
  6. Right-click in the Mappings area and click Add Row.
    Mapping dialog box
  7. Name the attribute skylineValue.
  8. You want the red channel of the image to be interpreted as height, so select red from the Channel drop-down list.
  9. Building heights should vary between 20 (0% red) and 200 (100% red) meters.
  10. Type 20 for the minimum value and 200 for the maximum value.
  11. Click Finish, and rename the created layer Skyline Map in the Inspector.
    Mapping dialog box
  12. Adjust the elevation offset of your new map layer in the Inspector to bring it to a position with better visibility. Use the alpha value to give it some transparency.
  13. You can now control the rule parameter height with your skyline map.
  14. Reassign the rule file to your lots so CityEngine can complete the connection.
  15. Select all lots.
  16. Click the dropdown in the height attribute and select Connect Attribute in the Inspector.
    Connect attribute

    The Attribute Connection Editor dialog box appears.

  17. Click Layer attribute, and choose the new Skyline Map layer and the skylineValue attribute.
    Attribute Connection Editor dialog box
    After setting the source to the skyline layer, the value shows a ? character This means that the selection contains different values (all lot shapes have different height values).
  18. Select a single lot, and review its attributes in the Inspector view. The value of height is now connected to the skyline map.
    Inspector view showing height value
  19. Select some lots around the Bay area and generate the buildings.
    Screen shot of buildings
  20. The skyline is looking better, but you'll improve it using more advanced CGA techniques.
    Open the simpleBuildingShells_02.cga rule file, and look for the following function:

    Height calculation with variation

    // calc height with variation
    getHeight(area) = 
    	case area > 600 : rand(0,40)+height
    	case area > 200 : rand(0,40)+height/2
    	else: rand(15,30)
    Rather than mapping the height value from the skyline map directly to the building height, the above user-defined function is used. (See the CGA Shape Grammar Tutorial for details about CGA rules and functions). This function does the following two things:
    • Tall buildings will be created on large-area lots only.
    • A random value is added to the incoming map value to give the skyline some variation.
  21. Select the lot layer in the Scene Editor.
  22. Assign the simpleBuildingShells_02.cga rule by right-clicking the layer > Assign Rule File.

    Your scene should look similar to the following screen shot:

    Screen shot of resulting scene

Control land-use types

You'll now learn how to use maps to control the land-use type of each building.

Open the mapcontrolTutorial_03.cej scene if it's not already open.

Control land-use types for buildings

A city often has areas of specific land-use types. This section describes how to set the attributes for three different land-use types. The following map shows commercial areas in blue, industrial areas in red, and residential areas in green:

Land-use map
Topographic map

Create the land-use map layer

Before creating the map layer, review the simpleBuildingShells_03.cga rule file, and look for the three CGA attributes:

CGA land use types attributes

// land use types
attr t_industrial = 0
attr t_commercial = 0
attr t_residential = 0

The map layer you create needs to have attributes with matching names.

  1. Hide the Skyline Layer by clicking the eye symbol in the Scene Editor.
  2. Create a new map layer by clicking Layer > New Map Layer > Mapping.
  3. Choose areatypes.png as the mapping file.
  4. Deselect the Keep ratio button.
  5. Change the X and Z sizes to 3000.
  6. Make sure the alignment is set to centered (clicking the alignment icon changes the mode).
  7. Right-click in the Mappings area and click Add Row.
  8. Add the following three mapping attributes:
    • t_industrial > red channel
    • t_commercial > green channel
    • t_residential > blue channel
    You'll evaluate these parameters in the rule file again, so the default minimum and maximum values of 0 and 1 are good generic mapping values for future use.
  9. Click Finish to create the map layer.
    Mapping dialog box
  10. Rename the layer LandUse Map in the Inspector.
  11. Adjust the display offset of your new map layer in the Inspector view to bring it to a position with better visibility. Use the alpha value to give it some transparency.
  12. Select the new map layer, and check its attributes in the Inspector.
  13. Check that the three CGA attributes have correct mappings. To change some of the settings of the map layer later, use the Inspector.
    Inspector windows showing layer attributes

    Now you'll evaluate the values of the land-use map.

  14. Select all lots.
  15. Assign the simpleBuildingShells_03.cga rule file.
  16. Set the source of the land-use parameters to the LandUse Map layer.
    Inspector window showing land-use parameters
  17. Before you generate the final building scene, review the simpleBuildingShells_03.cga rule file, and locate the color declarations and functionlanduseTypeColor.

    Color type for land use.

    // color declarations
    red = "#ffaaaa"
    green = "#aaffaa"
    blue = "#aaaaff"
    white = "#ffffff"

    Color type for land use.

    // Functions
    landuseTypeColor = 
    	case t_industrial > t_commercial && t_industrial > t_residential : red
    	case t_commercial > t_industrial && t_commercial > t_residential : blue
    	case t_residential > t_industrial && t_residential > t_commercial : green
    	else : white
    This function analyzes the values coming from the land-use map and returns a color accordingly. If the color channel value coming from the map is greater than the value of the other channels, the corresponding land-use color is returned: red for industrial, blue for commercial, and green for residential buildings. By using the color operation color(landuseTypeColor) that is calling the land-use color function, the color is applied to the generated buildings.
  18. Generate all buildings in the scene.
  19. Alternatively, you can open the mapcontrolTutorial_04.cej scene and generate the buildings.
    Final generated building scene