Exemplo n.º 1
0
def makeTerrainJSON(layer, terrainData):
    # this should create a different json type. How about
    # 'MESH'? It will also need to iterate through the
    # geometry and take out Zs and triangulate
    if not HAS_SCIPY:
        print '''NumPy and SciPy must be installed in order to triangulate
        terrain. Please ensure that both are installed and available on
        sys.path.'''
        return
    from triforce import triangulate
    layerDict = {'type': 'Layer', 'name':layer.name}
    geoJSONDict = {'type': 'FeatureCollection', 'features':[]}
    points2d = []
    points = []
    pointAttributes = [] # a list of attribute values for each point
    for row in terrainData: # each row will contain a point
        rawJSON, columnData = row[0], row[1:]
        geomJSON = json.loads(rawJSON)
        # here is where I should triangulate it.
        point = geomJSON['coordinates']
        if len(point) == 3:
            pointZ = point[2]
        elif layer.zColumn:
            pointZ = columnData[layer.cols.index(layer.zColumn)]
        else:
            pointZ = 0.0
        pointX, pointY = point[0], point[1]
        points2d.append((pointX, pointY))
        points.append((pointX, pointY, pointZ))
        # this creates one list for each point
        pointAttributes.append(columnData)
    # now triangulate the points2d
    tris = []
    for array in triangulate(points2d):
        face = [int(n) for n in array]
        tris.append(face)
    geomJSON = {'type': 'Mesh'} # a new geoJSON type!
    geomJSON['coordinates'] = points
    geomJSON['faces'] = tris
    transposedAttributes = zip(*pointAttributes) #omg hack!
    attributeDictionary = dict(zip(layer.cols, transposedAttributes)) # I think this should work
    featureDict = {'type':'Feature'}
    featureDict['geometry'] = geomJSON
    featureDict['properties'] = attributeDictionary
    geoJSONDict['features'].append(featureDict)
    layerDict['contents'] = geoJSONDict
    if layer.color:
        layerDict['color'] = layer.color
    return layerDict
Exemplo n.º 2
0
def queryToMesh(connection, sql):
    """This function takes an open database connection
    (a psycopg2 object, refer to psycopg2 docs and the db
    module for more info), an sql statement
    and uses these things to construct
    a mesh in Maya. The return value is the name of the 
    created mesh."""
    data = db.runopen(connection, sql)
    points = []
    twoDpoints = []
    Zs = []
    for rowTuple in data:
        ewkt = rowTuple[0]
        cleaned_ewkt = db.removeSRID(ewkt)
        p = shapely.wkt.loads(cleaned_ewkt)
        point = (p.x, p.y, p.z)
        xy = (p.x,p.y) 
        points.append(point)
        twoDpoints.append(xy)
        Zs.append(p.z)
    # build a structure from delaunay triangulation
    triStructure = triforce.triangulate(twoDpoints)
    # now I need to turn the points and structure into a mesh
    return makeMesh(points, triStructure)
Exemplo n.º 3
0
def queryToMesh(connection, sql):
    """This function takes an open database connection
    (a psycopg2 object, refer to psycopg2 docs and the db
    module for more info), an sql statement
    and uses these things to construct
    a mesh in Maya. The return value is the name of the 
    created mesh."""
    data = db.runopen(connection, sql)
    points = []
    twoDpoints = []
    Zs = []
    for rowTuple in data:
        ewkt = rowTuple[0]
        cleaned_ewkt = db.removeSRID(ewkt)
        p = shapely.wkt.loads(cleaned_ewkt)
        point = (p.x, p.y, p.z)
        xy = (p.x, p.y)
        points.append(point)
        twoDpoints.append(xy)
        Zs.append(p.z)
    # build a structure from delaunay triangulation
    triStructure = triforce.triangulate(twoDpoints)
    # now I need to turn the points and structure into a mesh
    return makeMesh(points, triStructure)