def createChannelUnitsJSON(topoDataFolder, visitID, jsonFilePath):

    topo = TopoData(topoDataFolder, visitID)
    topo.loadlayers()
    shpCU = Shapefile(topo.ChannelUnits)

    dUnits = {}
    feats = shpCU.featuresToShapely()
    for aFeat in feats:
        origTier1 = aFeat['fields']['Tier1']
        origTier2 = aFeat['fields']['Tier2']
        UtNum = aFeat['fields']['UnitNumber']

        finalTier1 = None
        for tier1 in dUnitDefs.keys():
            if tier1.lower() == origTier1.lower():
                finalTier1 = tier1
                break

        if not finalTier1:
            print('No Tier 1 channel unit type found')

        dUnits[UtNum] = (finalTier1, origTier2, 1)

    writeChannelUnitsToJSON(jsonFilePath, dUnits)
Beispiel #2
0
def vectorToCSV(shapefilePath, outputCSVPath):

    print "Writing vector {0} to {1}".format(os.path.basename(shapefilePath),
                                             outputCSVPath)

    clShp = Shapefile(shapefilePath)
    clList = clShp.featuresToShapely()

    with open(outputCSVPath, 'w') as csvfile:
        # write the header row
        csvfile.write('x,y\n')

        for aLine in clList:
            for aPoint in aLine['geometry'].coords:
                csvfile.write('{0},{1}\n'.format(aPoint[0], aPoint[1]))
Beispiel #3
0
def processSurveyExtent(projects, outputFolder):

    spatialRef = None
    unionPoly = None
    intersectPoly = None

    for proj in projects:
        extentPath = proj['project'].getpath('SurveyExtent')
        vid = proj['visit']
        if not os.path.isfile(extentPath):
            print "Warning: Missing Survey Extent ShapeFile for Visit {0}".format(
                vid)
            continue

        seShp = Shapefile(extentPath)
        seList = seShp.featuresToShapely()
        spatialRef = seShp.spatialRef

        for sePolygon in seList:

            if unionPoly:
                unionPoly = unionPoly.union(sePolygon['geometry'])
            else:
                unionPoly = sePolygon['geometry']

            if intersectPoly:
                intersectPoly = intersectPoly.intersection(
                    sePolygon['geometry'])
            else:
                intersectPoly = sePolygon['geometry']

    if not unionPoly:
        raise DataException(
            "No survey extent union polygon derived from visits")

    if not intersectPoly:
        raise DataException(
            "No survey extent intersect polygon derived from visits")

    unionPath = os.path.join(outputFolder, "SurveyExtentUnion.shp")
    print "Writing survey extent union to {0}".format(unionPath)
    writeShapeToShapeFile(unionPath, unionPoly, spatialRef)

    intersectPath = os.path.join(outputFolder, "SurveyExtentIntersect.shp")
    print "Writing survey extent intersection to {0}".format(intersectPath)
    writeShapeToShapeFile(intersectPath, intersectPoly, spatialRef)
Beispiel #4
0
def writeShapeToShapeFile(outputPath, aPolygon, spatialRef):

    # Write the polygon to ShapeFile (note: ShapeFile handles deleting existing file)
    outShape = Shapefile()
    outShape.create(outputPath, spatialRef, geoType=ogr.wkbPolygon)
    outShape.createField("ID", ogr.OFTInteger)

    # The main centerline gets written first
    featureDefn = outShape.layer.GetLayerDefn()
    outFeature = ogr.Feature(featureDefn)
    ogrPolygon = ogr.CreateGeometryFromJson(json.dumps(mapping(aPolygon)))
    outFeature.SetGeometry(ogrPolygon)
    outFeature.SetField('ID', 1)
    outShape.layer.CreateFeature(outFeature)
def export_cad_files(project_xml, out_path):
    """exports dxf files containing tin components of topo tin and Topographic Survey Points, Lines and Survey Extent"""

    log = Logger("CADExport")

    # Load Topo project
    log.info("Load Topo project")
    project = topoproject.TopoProject(project_xml)

    # TIN stuff
    log.info("Beginning TIN Work")
    tin = TIN(project.getpath("TopoTin"))
    dict_tinlayers = {}
    dict_tinlayers["tin_points"] = {"layer_type":"POINT", "Features":[feat for feat in tin.nodes.values()]}
    dict_tinlayers["tin_lines"] = {"layer_type":"POLYLINE", "Features":[feat['geometry'] for feat in tin.breaklines.values()]}#, "linetype_field":"LineType"}
    dict_tinlayers["tin_area"] = {"layer_type":"POLYGON", "Features":[feat for feat in tin.hull_polygons.values()]}

    out_tin_dxf = export_as_dxf(dict_tinlayers, os.path.join(out_path, "TopoTin.dxf"))

    # Topo Stuff
    log.info("Beginning Topo Work")
    shpTopo = Shapefile(project.getpath("Topo_Points"))
    shpEOW = Shapefile(project.getpath("EdgeofWater_Points"))
    shpCP = Shapefile(project.getpath("Control_Points"))
    shpBL = Shapefile(project.getpath("Breaklines")) if project.layer_exists("Breaklines") else None
    shpExtent = Shapefile(project.getpath("Survey_Extent"))
    dict_topolayers = {}
    dict_topolayers["Topo_Points"] = {"layer_type":"POINT", "Features":[feat['geometry'] for feat in shpTopo.featuresToShapely()]}
    dict_topolayers["EdgeofWater_Points"] = {"layer_type":"POINT", "Features":[feat['geometry'] for feat in shpEOW.featuresToShapely()]}
    dict_topolayers["Control_Points"] = {"layer_type":"POINT", "Features":[feat['geometry'] for feat in shpCP.featuresToShapely()]}
    dict_topolayers["Breaklines"] = {"layer_type":"POLYLINE", "Features":[feat['geometry'] for feat in shpBL.featuresToShapely()]} if shpBL else None
    dict_topolayers["Survey_Extent"] = {"layer_type":"POLYGON", "Features":[feat['geometry'] for feat in shpExtent.featuresToShapely()]}

    out_topo_dxf = export_as_dxf(dict_topolayers, os.path.join(out_path, "SurveyTopography.dxf"))

    out_topo_csv = exportAsCSV(shpTopo.featuresToShapely() + shpEOW.featuresToShapely(), os.path.join(out_path, "SurveyTopographyPoints.csv"))
    out_control_csv = exportAsCSV(shpCP.featuresToShapely(), os.path.join(out_path, "ControlNetworkPoints.csv"))

    topo_rs_project = riverscapes.Project(project_xml)

    out_project = riverscapes.Project()
    out_project.create("CHaMP_Survey_CAD_Export", "CAD_Export", __version__)
    out_project.addProjectMetadata("Watershed", topo_rs_project.ProjectMetadata["Watershed"])

    #  find previous meta tags
    for tagname, tags in {"Site": ["Site", "SiteName"], "Visit": ["Visit", "VisitID"], "Year": ["Year", "FieldSeason"], "Watershed": ["Watershed", "Watershed"]}.iteritems():
        if tags[0] in topo_rs_project.ProjectMetadata or tags[1] in topo_rs_project.ProjectMetadata:
            out_project.addProjectMetadata(tagname, topo_rs_project.ProjectMetadata[tags[0]] if tags[0] in topo_rs_project.ProjectMetadata else topo_rs_project.ProjectMetadata[tags[1]])
        else:
            raise DataException("Missing project metadata")

    out_realization = riverscapes.Realization("CAD_Export")
    out_realization.name = "CHaMP Survey CAD Export"
    out_realization.productVersion = out_project.projectVersion
    ds = []
    ds.append(out_project.addInputDataset("TopoTin", "tin", None, None, "TIN", project.get_guid("TopoTin")))
    ds.append(out_project.addInputDataset("Topo_Points", "topo_points", None, guid=project.get_guid("Topo_Points")))
    ds.append(out_project.addInputDataset("EdgeofWater_Points", "eow_points", None, guid=project.get_guid("EdgeofWater_Points")))
    ds.append(out_project.addInputDataset("Control_Points", "control_ponts", None, guid=project.get_guid("Control_Points")))
    if shpBL:
        ds.append(out_project.addInputDataset("Breaklines", "breaklines", None, guid=project.get_guid("Breaklines")))
    ds.append(out_project.addInputDataset("Survey_Extent", "survey_extent", None, guid=project.get_guid("Survey_Extent")))
    for inputds in ds:
        out_realization.inputs[inputds.name] = inputds.id

    ds_tin_dxf = riverscapes.Dataset()
    ds_tin_dxf.create("TIN_DXF", "TopoTin.dxf")
    ds_tin_dxf.id = 'tin_dxf'
    ds_topo_dxf = riverscapes.Dataset()
    ds_topo_dxf.create("Topo_DXF", "SurveyTopography.dxf")
    ds_topo_dxf.id = 'topo_dxf'
    ds_topo_csv = riverscapes.Dataset()
    ds_topo_csv.create("Topo_CSV", "SurveyTopographyPoints.csv", "CSV")
    ds_topo_csv.id = 'topo_csv'
    ds_con_csv = riverscapes.Dataset()
    ds_con_csv.create("Control_CSV", "ControlNetworkPoints.csv", "CSV")
    ds_con_csv.id = 'control_csv'
    out_realization.outputs.update({"TIN_DXF": ds_tin_dxf,
                                    "Topo_DXF": ds_topo_dxf,
                                    "Topo_CSV": ds_topo_csv,
                                    "Control_CSV": ds_con_csv})

    out_project.addRealization(out_realization)
    out_project.writeProjectXML(os.path.join(out_path, "project.rs.xml"))

    return 0
def channelUnitScraper(outputDir, watersheds):

    visitData = {}
    for watershed, apiName in watersheds.iteritems():
        visitData[watershed] = {}

        loadVisitData(watershed, apiName, visitData[watershed])

    for watershed, visits in visitData.iteritems():
        outPath = os.path.join(outputDir, watershed.replace(' ', '') + ".shp")
        outShape = None

        featureID = 0
        for visitID, visit in visits.iteritems():

            if len(visit['ChannelUnits']) < 1:
                continue

            try:
                dirpath = tempfile.mkdtemp()

                # Open the visit channel unit shapefile.
                # Need the spatial reference from one of the visits to create the output watershed shapefile
                try:
                    fileJSON, projPath = downloadUnzipTopo(visitID, dirpath)
                    topo = TopoProject(projPath)
                    cuPath = topo.getpath('ChannelUnits')
                except (DataException, MissingException), e:
                    print "Error retrieving channel units ShapeFile for visit {0}".format(
                        visitID)
                    continue

                try:
                    shpCU = Shapefile(cuPath)
                except Exception, e:
                    print "Error OGR opening channel unit ShapeFile for visit {0}".format(
                        visitID)
                    continue

                if not outShape:
                    # Create new ShapeFile for this watershed
                    outShape = Shapefile()
                    outShape.create(outPath,
                                    shpCU.spatialRef,
                                    geoType=ogr.wkbPolygon)

                    outShape.createField("ID", ogr.OFTInteger)
                    outShape.createField("Watershed", ogr.OFTString)
                    outShape.createField("Site", ogr.OFTString)
                    outShape.createField("VisitID", ogr.OFTInteger)
                    outShape.createField("SampleYear", ogr.OFTInteger)
                    outShape.createField("Org", ogr.OFTString)
                    outShape.createField("UnitNumber", ogr.OFTInteger)
                    outShape.createField("UnitArea", ogr.OFTReal)
                    outShape.createField("Tier1", ogr.OFTString)
                    outShape.createField("Tier2", ogr.OFTString)
                    outShape.createField("AvgSiteWid", ogr.OFTReal)
                    outShape.createField("ReachLen", ogr.OFTReal)

                # Loop over all channel unit polygons for this visit

                feats = shpCU.featuresToShapely()
                for aFeat in feats:
                    featureID += 1
                    cuNumber = aFeat['fields']['UnitNumber']

                    featureDefn = outShape.layer.GetLayerDefn()
                    outFeature = ogr.Feature(featureDefn)
                    outFeature.SetField('ID', featureID)
                    outFeature.SetField('Watershed', visit['Watershed'])
                    outFeature.SetField('Site', visit['Site'])
                    outFeature.SetField('VisitID', visitID)
                    outFeature.SetField('SampleYear', visit['SampleYear'])
                    outFeature.SetField('Org', visit['Organization'])
                    outFeature.SetField('AvgSiteWid',
                                        visit['AverageSiteWidth'])
                    outFeature.SetField('ReachLen', visit['TotalReachLength'])
                    outFeature.SetField('UnitNumber', cuNumber)
                    outFeature.SetField(
                        'Tier1', visit['ChannelUnits'][cuNumber]['Tier1'])
                    outFeature.SetField(
                        'Tier2', visit['ChannelUnits'][cuNumber]['Tier2'])
                    outFeature.SetField('UnitArea', aFeat['geometry'].area)
                    outFeature.SetGeometry(
                        ogr.CreateGeometryFromJson(
                            json.dumps(mapping(aFeat['geometry']))))
                    outShape.layer.CreateFeature(outFeature)

            finally:
    def calc(self, sThalwegshp, sDepthRaster, sWaterSurfaceRaster, fDist,
             visitMetrics):

        if not path.isfile(sThalwegshp):
            raise MissingException("Thalweg shapefile missing")
        if not path.isfile(sDepthRaster):
            raise MissingException("Depth raster missing")
        if not path.isfile(sWaterSurfaceRaster):
            raise MissingException("Surface raster missing")

        wettedMainstemLength = visitMetrics['Wetted']['Centerline'][
            'MainstemLength']

        if wettedMainstemLength is None:
            raise MissingException(
                "No wetted mainstem length found in visit metrics")

        sfile = Shapefile(sThalwegshp).featuresToShapely()

        if len(sfile) < 1:
            raise DataException("Thalweg shapefile has no features")

        thalweg = sfile[0]['geometry']
        depthRaster = Raster(sDepthRaster)
        waterSurfaceRaster = Raster(sWaterSurfaceRaster)
        samplepts = ThalwegMetrics.interpolateRasterAlongLine(thalweg, fDist)
        results = ThalwegMetrics.lookupRasterValues(samplepts,
                                                    depthRaster)['values']

        # Get the elevation at the first (downstream) point on the Thalweg
        dsElev = waterSurfaceRaster.getPixelVal(thalweg.coords[0])
        usElev = waterSurfaceRaster.getPixelVal(thalweg.coords[-1])

        if (np.isnan(dsElev)):
            raise DataException(
                'nodata detected in the raster for downstream point on the thalweg'
            )
        elif np.isnan(usElev):
            raise DataException(
                'nodata detected in the raster for upstream point on the thalweg'
            )

        waterSurfaceGradientRatio = (usElev - dsElev) / thalweg.length
        waterSurfaceGradientPC = waterSurfaceGradientRatio * 100.0

        # Thalweg straight length and sinuosity
        firstPoint = Point(thalweg.coords[0])
        lastPoint = Point(thalweg.coords[-1])
        straightLength = firstPoint.distance(lastPoint)
        sinuosity = thalweg.length / straightLength

        self.metrics = {
            'Min': np.nanmin(results),
            'Max': np.nanmax(results),
            'Mean': np.mean(results),
            'StDev': np.std(results),
            'Count': np.count_nonzero(results),
            'Length': thalweg.length,
            'WSGradientRatio': waterSurfaceGradientRatio,
            'WSGradientPC': waterSurfaceGradientPC,
            'Sinuosity': sinuosity,
            'CV': 0.0,
            'ThalwegToCenterlineRatio': thalweg.length / wettedMainstemLength
            #, 'Values': results.data
        }
        if self.metrics['StDev'] != 0 and self.metrics['Mean'] != 0:
            self.metrics['CV'] = self.metrics['StDev'] / self.metrics['Mean']
 def get_shapefile(self):
     return Shapefile(self.filename)
Beispiel #9
0
def concatenateControlPoints(projects, outputFolder):

    spatialRef = None
    cpList = []

    for proj in projects:
        controlPointsPath = proj['project'].getpath('Control_Points')
        vid = proj['visit']

        cpShp = Shapefile(controlPointsPath)
        visitCPList = cpShp.featuresToShapely()
        spatialRef = cpShp.spatialRef

        for aPoint in visitCPList:
            cpItems = {}
            cpItems['geometry'] = aPoint['geometry']
            cpItems['fields'] = {}

            cpItems['fields']['VisitID'] = int(vid)
            cpItems['fields']['PointNum'] = getFieldValue(
                aPoint['fields'], [
                    'Point_Numb', 'POINT_NUMB', 'PointNumb', 'POINT_NUM',
                    'POINT'
                ])
            cpItems['fields']['Code'] = getFieldValue(
                aPoint['fields'], ['DESCRIPTIO', 'Descriptio', 'Code'])
            cpItems['fields']['Type'] = getFieldValue(aPoint['fields'],
                                                      ['Type', 'TYPE'])

            cpList.append(cpItems)

    outputPath = os.path.join(outputFolder, "AllVisitControlPoints.shp")
    print "Writing combined control points to {0}".format(outputPath)
    outShape = Shapefile()
    outShape.create(outputPath, spatialRef, geoType=ogr.wkbPointZM)

    outShape.createField("ID", ogr.OFTInteger)
    outShape.createField("VisitID", ogr.OFTInteger)
    outShape.createField("PointNum", ogr.OFTString)
    outShape.createField("Code", ogr.OFTString)
    outShape.createField("Type", ogr.OFTString)

    id = 1
    for aCP in cpList:

        featureDefn = outShape.layer.GetLayerDefn()
        outFeature = ogr.Feature(featureDefn)
        ogrPolygon = ogr.CreateGeometryFromJson(
            json.dumps(mapping(aCP['geometry'])))
        outFeature.SetGeometry(ogrPolygon)
        outFeature.SetField('ID', id)
        id += 1

        for fieldName, fieldValue in aCP['fields'].iteritems():
            outFeature.SetField(fieldName, fieldValue)

        outShape.layer.CreateFeature(outFeature)