ST_AsX3D
Prev | 8.9.3. Other Formats | Next

## Name

ST_AsX3D — Returns a Geometry in X3D xml node element format: ISO-IEC-19776-1.2-X3DEncodings-XML

## Synopsis

`text ST_AsX3D(`geometry g1, integer maxdecimaldigits=15, integer options=0`)`;

## Description

Returns a geometry as an X3D xml formatted node element http://www.web3d.org/standards/number/19776-1. If `maxdecimaldigits` (precision) is not specified then defaults to 15.

There are various options for translating PostGIS geometries to X3D since X3D geometry types don't map directly to PostGIS geometry types and some newer X3D types that might be better mappings we have avoided since most rendering tools don't currently support them. These are the mappings we have settled on. Feel free to post a bug ticket if you have thoughts on the idea or ways we can allow people to denote their preferred mappings. Below is how we currently map PostGIS 2D/3D types to X3D types

The 'options' argument is a bitfield. For PostGIS 2.2+, this is used to denote whether to represent coordinates with X3D GeoCoordinates Geospatial node and also whether to flip the x/y axis. By default, `ST_AsX3D` outputs in database form (long,lat or X,Y), but X3D default of lat/lon, y/x may be preferred.

- 0: X/Y in database order (e.g. long/lat = X,Y is standard database order), default value, and non-spatial coordinates (just regular old Coordinate tag).

- 1: Flip X and Y. If used in conjunction with the GeoCoordinate option switch, then output will be default "latitude_first" and coordinates will be flipped as well.

- 2: Output coordinates in GeoSpatial GeoCoordinates. This option will throw an error if geometries are not in WGS 84 long lat (srid: 4326). This is currently the only GeoCoordinate type supported. Refer to X3D specs specifying a spatial reference system.. Default output will be `GeoCoordinate geoSystem='"GD" "WE" "longitude_first"'`. If you prefer the X3D default of `GeoCoordinate geoSystem='"GD" "WE" "latitude_first"'` use `(2 + 1)` = `3`

PostGIS Type | 2D X3D Type | 3D X3D Type
--- | --- | ---
LINESTRING | not yet implemented - will be PolyLine2D | LineSet
MULTILINESTRING | not yet implemented - will be PolyLine2D | IndexedLineSet
MULTIPOINT | Polypoint2D | PointSet
POINT | outputs the space delimited coordinates | outputs the space delimited coordinates
(MULTI) POLYGON, POLYHEDRALSURFACE | Invalid X3D markup | IndexedFaceSet (inner rings currently output as another faceset)
TIN | TriangleSet2D (Not Yet Implemented) | IndexedTriangleSet

2D geometry support not yet complete. Inner rings currently just drawn as separate polygons. We are working on these.

Lots of advancements happening in 3D space particularly with X3D Integration with HTML5

There is also a nice open source X3D viewer you can use to view rendered geometries. Free Wrl http://freewrl.sourceforge.net/ binaries available for Mac, Linux, and Windows. Use the FreeWRL_Launcher packaged to view the geometries.

Also check out PostGIS minimalist X3D viewer that utilizes this function and x3dDom html/js open source toolkit.

Availability: 2.0.0: ISO-IEC-19776-1.2-X3DEncodings-XML

Enhanced: 2.2.0: Support for GeoCoordinates and axis (x/y, long/lat) flipping. Look at options for details.

This function supports 3d and will not drop the z-index.

This function supports Polyhedral surfaces.

This function supports Triangles and Triangulated Irregular Network Surfaces (TIN).

## Example: Create a fully functional X3D document - This will generate a cube that is viewable in FreeWrl and other X3D viewers.

    SELECT '<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d-3.0.dtd">
    <X3D>
      <Scene>
        <Transform>
          <Shape>
           <Appearance>
                <Material emissiveColor=''0 0 1''/>
           </Appearance> ' ||
           ST_AsX3D( ST_GeomFromEWKT('POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)),
    ((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)), ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),
    ((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)),
    ((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)), ((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )')) ||
          '</Shape>
        </Transform>
      </Scene>
    </X3D>' As x3ddoc;

    		x3ddoc
    		--------
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d-3.0.dtd">
    <X3D>
      <Scene>
        <Transform>
          <Shape>
           <Appearance>
                <Material emissiveColor='0 0 1'/>
           </Appearance>
           <IndexedFaceSet  coordIndex='0 1 2 3 -1 4 5 6 7 -1 8 9 10 11 -1 12 13 14 15 -1 16 17 18 19 -1 20 21 22 23'>
                <Coordinate point='0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1' />
          </IndexedFaceSet>
          </Shape>
        </Transform>
      </Scene>
    </X3D>

## PostGIS buildings

Copy and paste the output of this query to x3d scene viewer and click Show

    SELECT string_agg('<Shape>' || ST_AsX3D(ST_Extrude(geom, 0,0, i*0.5)) ||
        '<Appearance>
              <Material diffuseColor="' || (0.01*i)::text || ' 0.8 0.2" specularColor="' || (0.05*i)::text || ' 0 0.5"/>
            </Appearance>
        </Shape>', '')
    FROM ST_Subdivide(ST_Letters('PostGIS'),20) WITH ORDINALITY AS f(geom,i);

Buildings formed by subdividing PostGIS and extrusion

## Example: An Octagon elevated 3 Units and decimal precision of 6

    SELECT ST_AsX3D(
    ST_Translate(
        ST_Force_3d(
            ST_Buffer(ST_Point(10,10),5, 'quad_segs=2')), 0,0,
        3)
      ,6) As x3dfrag;

    x3dfrag
    --------
    <IndexedFaceSet coordIndex="0 1 2 3 4 5 6 7">
        <Coordinate point="15 10 3 13.535534 6.464466 3 10 5 3 6.464466 6.464466 3 5 10 3 6.464466 13.535534 3 10 15 3 13.535534 13.535534 3 " />
    </IndexedFaceSet>

## Example: TIN

    SELECT ST_AsX3D(ST_GeomFromEWKT('TIN (((
                    0 0 0,
                    0 0 1,
                    0 1 0,
                    0 0 0
                )), ((
                    0 0 0,
                    0 1 0,
                    1 1 0,
                    0 0 0
                ))
                )')) As x3dfrag;

    		x3dfrag
    		--------
    <IndexedTriangleSet  index='0 1 2 3 4 5'><Coordinate point='0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0'/></IndexedTriangleSet>

## Example: Closed multilinestring (the boundary of a polygon with holes)

    SELECT ST_AsX3D(
    		    ST_GeomFromEWKT('MULTILINESTRING((20 0 10,16 -12 10,0 -16 10,-12 -12 10,-20 0 10,-12 16 10,0 24 10,16 16 10,20 0 10),
      (12 0 10,8 8 10,0 12 10,-8 8 10,-8 0 10,-8 -4 10,0 -8 10,8 -4 10,12 0 10))')
    ) As x3dfrag;

    		x3dfrag
    		--------
    <IndexedLineSet  coordIndex='0 1 2 3 4 5 6 7 0 -1 8 9 10 11 12 13 14 15 8'>
        <Coordinate point='20 0 10 16 -12 10 0 -16 10 -12 -12 10 -20 0 10 -12 16 10 0 24 10 16 16 10 12 0 10 8 8 10 0 12 10 -8 8 10 -8 0 10 -8 -4 10 0 -8 10 8 -4 10 ' />
     </IndexedLineSet>

Prev | Up | Next
ST_AsTWKB | Home | ST_GeoHash