예제 #1
0
def ogr_wktempty_test_partial_empty_geoms():

    # Multipoint with a valid point and an empty point
    wkt = 'MULTIPOINT (1 1)'
    geom = ogr.CreateGeometryFromWkt(wkt)
    geom.AddGeometry(ogr.Geometry( type = ogr.wkbPoint ))
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason( 'WKT is wrong: ' + geom.ExportToWkt() + '. Expected value is: ' + wkt )
        return 'fail'

    # Multipoint with an empty point and a valid point
    geom = ogr.CreateGeometryFromWkt('MULTIPOINT EMPTY')
    geom.AddGeometry(ogr.Geometry( type = ogr.wkbPoint ))
    geom.AddGeometry(ogr.CreateGeometryFromWkt('POINT (1 1)'))
    wkt = 'MULTIPOINT (1 1)'
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason( 'WKT is wrong: ' + geom.ExportToWkt() + '. Expected value is: ' + wkt )
        return 'fail'

    # Multilinestring with a valid string and an empty linestring
    wkt = 'MULTILINESTRING ((0 1,2 3,4 5,0 1))'
    geom = ogr.CreateGeometryFromWkt(wkt)
    geom.AddGeometry(ogr.Geometry( type = ogr.wkbLineString ))
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason( 'WKT is wrong: ' + geom.ExportToWkt() + '. Expected value is: ' + wkt )
        return 'fail'

    # Multilinestring with an empty linestring and a valid linestring
    geom = ogr.CreateGeometryFromWkt('MULTILINESTRING EMPTY')
    geom.AddGeometry(ogr.Geometry( type = ogr.wkbLineString ))
    geom.AddGeometry(ogr.CreateGeometryFromWkt('LINESTRING (0 1,2 3,4 5,0 1)'))
    wkt = 'MULTILINESTRING ((0 1,2 3,4 5,0 1))'
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason( 'WKT is wrong: ' + geom.ExportToWkt() + '. Expected value is: ' + wkt )
        return 'fail'

    # Polygon with a valid external ring and an empty internal ring
    wkt = 'POLYGON ((100 0,100 10,110 10,100 0))'
    geom = ogr.CreateGeometryFromWkt(wkt)
    geom.AddGeometry(ogr.Geometry( type = ogr.wkbLinearRing ))
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason( 'WKT is wrong: ' + geom.ExportToWkt() + '. Expected value is: ' + wkt )
        return 'fail'

    # Polygon with an empty external ring and a valid internal ring
    wkt = 'POLYGON EMPTY'
    geom = ogr.CreateGeometryFromWkt(wkt)
    geom.AddGeometry(ogr.Geometry( type = ogr.wkbLinearRing ))
    ring = ogr.Geometry( type = ogr.wkbLinearRing )
    ring.AddPoint_2D( 0, 0)
    ring.AddPoint_2D( 10, 0)
    ring.AddPoint_2D( 10, 10)
    ring.AddPoint_2D( 0, 10)
    ring.AddPoint_2D( 0, 0)
    geom.AddGeometry(ring)
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason( 'WKT is wrong: ' + geom.ExportToWkt() + '. Expected value is: ' + wkt )
        return 'fail'

    # Multipolygon with a valid polygon and an empty polygon
    wkt = 'MULTIPOLYGON (((0 0,0 10,10 10,0 0)))'
    geom = ogr.CreateGeometryFromWkt(wkt)
    geom.AddGeometry(ogr.Geometry( type = ogr.wkbPolygon ))
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason( 'WKT is wrong: ' + geom.ExportToWkt() + '. Expected value is: ' + wkt )
        return 'fail'

    # Multipolygon with an empty polygon and a valid polygon
    geom = ogr.CreateGeometryFromWkt('MULTIPOLYGON EMPTY')
    geom.AddGeometry(ogr.Geometry( type = ogr.wkbPolygon ))
    geom.AddGeometry(ogr.CreateGeometryFromWkt('POLYGON ((100 0,100 10,110 10,100 0))'))
    wkt = 'MULTIPOLYGON (((100 0,100 10,110 10,100 0)))'
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason( 'WKT is wrong: ' + geom.ExportToWkt() + '. Expected value is: ' + wkt )
        return 'fail'

    return 'success'
예제 #2
0
for i in range(in_lyr.GetFeatureCount()):

    feat = in_lyr.GetFeature(i)
    geom = feat.geometry()

    vertdict = geom.ExportToJson()
    verts = json.loads(vertdict)
    vs = np.array(verts["coordinates"][0])

    while len(vs.shape) > 2:
        vs = vs[0]
    print vs

    c = centroid(vs)
    print c

    # create a new feature to add to the output layer
    newfeat = ogr.Feature(out_feats)

    cpt = ogr.Geometry(ogr.wkbPoint)
    cpt.SetPoint_2D(0, c[0], c[1])

    newfeat.SetGeometry(cpt)

    # now get the tract's unique identifier
    id = float(feat.GetField("CTLabel"))

    newfeat.SetField("id", id)

    out_lyr.CreateFeature(newfeat)
예제 #3
0
# Use a better method to count the cities within 16,000 meters of a volcano,
# because it doesn't double-count stuff.

import ogr

# Open the layers as in listing 7.1 instead of reset reading.
shp_ds = ogr.Open(r'D:\osgeopy-data\US')
volcano_lyr = shp_ds.GetLayer('us_volcanos_albers')
cities_lyr = shp_ds.GetLayer('cities_albers')

# This time, add each buffer to a multipolygon instead of a temporary later.
multipoly = ogr.Geometry(ogr.wkbMultiPolygon)
for volcano_feat in volcano_lyr:
    buff_geom = volcano_feat.geometry().Buffer(16000)
    multipoly.AddGeometry(buff_geom)

# Then union all of the buffers together to get one polygon which you can use
# as a spatial filter.
cities_lyr.SetSpatialFilter(multipoly.UnionCascaded())
print('Cities: {}'.format(cities_lyr.GetFeatureCount()))
def main(ingpxfile, outpntsshapefile, secdiff):
    ## infile = "GPSTrack_D5T4.gpx"
    ## outfile = "GPSTrack_D5T4_utm6_with_segments.shp"

    ## Read in time data for each ASD file.
    ## they are stored in a pre-made list with the ASD file name and the date/Time in each row
    csvtablefile = "/Carnegie/DGE/caodata/Scratch/dknapp/ASD/Spectroscopy/list_spec_time.txt"
    specrows = []
    with open(csvtablefile, 'r') as csvfile:
        csvreader = csv.reader(csvfile)
        for row in csvreader:
            specrows.append(row)

    transroot = os.path.splitext(ingpxfile)[0][-4:] + "*.asd.ref"

    ## Create a list with the date/times as datetime objects.
    spectimedates = []
    for row in specrows:
        ## uggh.  The format for timezone offset (%z) does not include a colon (:),
        ## so we have to skip that colon character.
        temp = row[1][0:22] + row[1][23:]
        trydate = datetime.datetime.strptime(temp, "%Y-%m-%dT%H:%M:%S%z")
        spectimedates.append(trydate)

    ## CReate output spatial reference for Moorea (UTM Zone 6 South)
    spatialReference = osr.SpatialReference()
    spatialReference.ImportFromEPSG(32706)

    ## Create output data file
    drv = ogr.GetDriverByName("ESRI Shapefile")
    outDS = drv.CreateDataSource(outpntsshapefile)
    ##  outlayer = outDS.CreateLayer('moorea', spatialReference, ogr.wkbLineString)
    outlayer = outDS.CreateLayer('moorea', spatialReference, ogr.wkbPoint)
    outlayerDefn = outlayer.GetLayerDefn()
    tnameDefn = ogr.FieldDefn('specname', ogr.OFTString)
    timepntDefn = ogr.FieldDefn('timepnt', ogr.OFTString)
    outlayer.CreateField(tnameDefn)
    outlayer.CreateField(timepntDefn)

    ## Get input data layer (track_points)
    inDS = ogr.Open(ingpxfile)
    lyr = inDS.GetLayerByName('track_points')
    lyrdefn = lyr.GetLayerDefn()
    numpnts = lyr.GetFeatureCount()
    fldcnt = lyrdefn.GetFieldCount()

    projutm6s = pyproj.Proj("+init=EPSG:32706")

    pntutm = []
    times = []
    azimuths = []

    lyr.ResetReading()

    ## create utc and french polynesia timezone objects
    utc = datetime.timezone.utc
    fptz = datetime.timezone(datetime.timedelta(hours=-10))

    for k in range(0, numpnts):
        feat = lyr.GetFeature(k)
        mytime = feat.GetFieldAsDateTime('time')
        print(mytime)
        mydatetime = datetime.datetime(mytime[0], mytime[1], mytime[2], mytime[3], \
          mytime[4], int(mytime[5]), tzinfo=utc)
        geom = feat.GetGeometryRef()
        lon = geom.GetX()
        lat = geom.GetY()
        temputm = projutm6s(lon, lat)
        pntutm.append(temputm)
        times.append(mydatetime)

    for j in np.arange(0, (len(pntutm) - 1)):
        pnt1 = pntutm[j]
        pnt2 = pntutm[j + 1]
        diffx = pnt2[0] - pnt1[0]
        diffy = pnt2[1] - pnt1[1]
        initial_azimuth = math.degrees(math.atan2(diffx, diffy))
        azimuth = (initial_azimuth + 360) % 360
        azimuths.append(azimuth)
        time1 = times[j]
        time2 = times[j + 1]
        segtimediff = (time2 - time1).total_seconds()
        segdist = math.sqrt(math.pow(diffx, 2) + math.pow(diffy, 2))
        ## Find the spectra that are between these 2 points
        myregex = fnmatch.translate(transroot)
        asdobj = re.compile(myregex)

        for i, asdrow in enumerate(specrows):
            gotit = asdobj.match(asdrow[0])
            if gotit is not None:
                spectime = spectimedates[i] + datetime.timedelta(
                    seconds=secdiff)
                #  is it between these 2 segment points?
                if (spectime > time1) and (spectime < time2):
                    propo = ((spectime -
                              time1).total_seconds()) / float(segtimediff)
                    azrad = azimuth * (math.pi / 180.0)
                    xlen = math.sin(azrad) * (propo * segdist)
                    ylen = math.cos(azrad) * (propo * segdist)
                    xnewpnt = pnt1[0] + xlen
                    ynewpnt = pnt1[1] + ylen
                    feature = ogr.Feature(outlayerDefn)
                    pnt = ogr.Geometry(ogr.wkbPoint)
                    pnt.AddPoint(xnewpnt, ynewpnt)
                    feature.SetGeometry(pnt)
                    feature.SetFID(i)
                    feature.SetField('specname', asdrow[0])
                    timestr = spectime.astimezone(utc).strftime(
                        "%Y-%m-%dT%H:%M:%SZ")
                    feature.SetField('timepnt', timestr)
                    outlayer.CreateFeature(feature)

    inDS, outDS = None, None
예제 #5
0
def extract_all_features(ds,
                         cs_gt_spatial_ref,
                         cs_gt_dict,
                         win_size=np.array((20, 20)),
                         win_origin='TL',
                         win_rotation=27.,
                         plotFigures=False):  # , axis_signs=[1, 1]):
    # win_sizes - the OL and other window sizes in pixels
    # win_origin - the corner of the plot where the CS plot gps loc was recorded (BL,TL,TR,BR = SW,NW,NE,SE)
    #              the image (and any numpy matrix) origin is TL and increasing pixels L->R is W->E and T->B is N->S
    #              so eg if the plot origin is BL, one would want to start reading at TL to end at BL
    # win_rotation - the rotation of the plots (magnetic N offset from TN)
    # Note         A way around all this confusion may to be to layout plot co-ords in world co-ords and then convert to
    #              to pixel co-ords using world2pixel.  this would avoid all the fiddling and confusion in pixel space
    win_cnrs = np.array([[0, 0], [0, 1], [1, 1], [1,
                                                  0]])  # TL origin by default

    if win_origin == 'BL':
        win_cnrs[:, 1] = win_cnrs[:, 1] - 1
    elif win_origin == 'TL':
        # win_cnrs[:,1] = win_cnrs[:,1]-1
        win_cnrs = win_cnrs
    elif win_origin == 'TR':
        win_cnrs[:, 0] = win_cnrs[:, 0] - 1
        # win_cnrs[:,1] = win_cnrs[:,1]-1
    elif win_origin == 'BR':
        win_cnrs[:, 0] = win_cnrs[:, 0] - 1
        win_cnrs[:, 1] = win_cnrs[:, 1] - 1
    else:
        raise Exception('Unknown win_origin')

    # where is the right place to apply this?
    # win_cnrs[:, 0] = win_cnrs[:, 0] * axis_signs[0]
    # win_cnrs[:, 1] = win_cnrs[:, 1] * axis_signs[1]

    win_coord = win_cnrs.copy()

    theta = -np.radians(
        win_rotation)  # -ve angle for inverted y axis (valid for rotation<90)
    c, s = np.cos(theta), np.sin(theta)
    R = np.array([[c, -s], [s, c]])
    # win_coords_ = [[], []]
    win_coord = win_cnrs * np.tile(win_size, [4, 1])  # note -1
    win_coord_ = np.dot(win_coord, R.transpose())
    # where is the right place to apply this?  also, it can be incorporated into R more neatly
    # win_coords[i][:, 0] = win_coords[i][:, 0] * axis_signs[0]
    # win_coords[i][:, 1] = win_coords[i][:, 1] * axis_signs[1]
    # win_coords_[i][:, 0] = win_coords_[i][:, 0] * axis_signs[0]
    # win_coords_[i][:, 1] = win_coords_[i][:, 1] * axis_signs[1]

    if plotFigures:
        f = pylab.figure()
        p = pylab.plot(win_coord[:, 0], win_coord[:, 1])
        pylab.plot(win_coord_[:, 0], win_coord_[:, 1], 'r')
        pylab.axis('equal')
        f.gca().invert_yaxis()

    win_mask = np.zeros(win_size)
    win_mask_size = []
    if win_rotation is not None:
        print 'rotating'

        # for win_mask, win_rotation, win_size in izip(win_masks, win_rotations, win_sizes):
        if True:
            mn = (np.min(win_coord_, 0))
            mx = (np.max(win_coord_, 0))
            win_size_r = np.int32(np.ceil(mx - mn))

            img = Image.fromarray(np.zeros(win_size_r))

            # Draw a rotated rectangle on the image.
            draw = ImageDraw.Draw(img)
            # rect = get_rect(x=120, y=80, width=100, height=40, angle=30.0)
            draw.polygon([tuple((p - mn)) for p in win_coord_], fill=1)
            # Convert the Image data to a numpy array.
            win_mask = np.asarray(img)
            # win_masks[i] = np.flipud(win_masks[i])  # ??? why is this necessary
        else:
            win_mask = ndimage.rotate(win_mask, win_rotation)
            # if axis_signs[0] < 0:
            #     win_masks[i] = np.fliplr(win_masks[i])
            # if axis_signs[1] < 0:
            #     win_masks[i] = np.flipud(win_masks[i])

            win_mask_size = win_mask.shape

    if plotFigures:
        pylab.figure()
        pylab.imshow(np.bool8(win_mask))

    geotransform = ds.GetGeoTransform()
    transform = osr.CoordinateTransformation(
        cs_gt_spatial_ref, osr.SpatialReference(ds.GetProjection()))
    i = 0
    plot_dict = {}
    # plotTagcDict = {}
    # class_labels = ['Pristine', 'Moderate', 'Severe']
    max_im_vals = np.zeros((4))
    for plot in cs_gt_dict.values():
        point = ogr.Geometry(ogr.wkbPoint)
        point.AddPoint(plot['X'], plot['Y'])
        point.Transform(transform)  # xform into im projection
        (pixel, line) = world2Pixel(geotransform, point.GetX(), point.GetY())
        # not all the point fall inside the image

        # win_size = win_sizes[0]
        # win_mask = win_masks[0]
        win_coord = win_coord_

        # ci = class_labels.index(plot['DegrClass'])

        # think about window size for co-ord xform
        mn = (np.min(win_coord, 0))
        mx = (np.max(win_coord, 0))
        win_size_r = np.int32(np.ceil(mx - mn))

        if pixel >= 0 and line >= 0 and pixel < ds.RasterXSize and line < ds.RasterYSize and \
            pixel + win_size_r[0]  < ds.RasterXSize and line + win_size_r[1]  < ds.RasterYSize:
            imbuf = np.zeros((win_size_r[1], win_size_r[0], 4), dtype=float)

            for b in range(1, 5):
                pixel_start = np.int(np.round((pixel + mn[0])))
                line_start = np.int(np.round((line + mn[1])))
                imbuf[:, :, b - 1] = ds.GetRasterBand(b).ReadAsArray(
                    pixel_start, line_start, win_size_r[0], win_size_r[1])

            # imbuf[:, :, 3] = imbuf[:, :, 3] / 2  # hack for NGI XCALIB
            if np.all(imbuf == 0):
                print plot['ID'] + ": imbuf zero, assume NODATA ommitting"
            else:
                # for b in range(0, 4):
                #     imbuf[:, :, b] = imbuf[:, :, b] / max_im_vals_[b]
                if not win_mask.shape == imbuf.shape[0:2]:
                    print "error - mask and buf different sizes"
                    raise Exception("error - mask and buf different sizes")
                feat = extract_patch_features(imbuf.copy(), win_mask)
                # feat['classi'] = ci
                # feat['class'] = class_labels[ci]
                # fields = ['TAGC', 'Z_P_AFRA', 'ALLOMETRY', 'HERB', 'LITTER']
                fields = plot.keys()
                for f in fields:
                    feat[f] = cs_gt_dict[plot['ID']][f]
                feat['Xp'] = point.GetX()  # projected co-ords
                feat['Yp'] = point.GetY()

                feat['thumbnail'] = np.float32(imbuf.copy())
                plot_dict[plot['ID']] = feat
                # plotTagcDict[plot['PLOT']] = cs_gt_dict[plot['PLOT']]['TAGC']
                tmp = np.reshape(feat['thumbnail'], (np.prod(win_size_r), 4))
                # max_tmp = tmp.max(axis=0)
                max_tmp = np.percentile(tmp, 98., axis=0)
                max_im_vals[max_tmp > max_im_vals] = max_tmp[
                    max_tmp > max_im_vals]
                # print plot['PLOT']
                i = i + 1
        else:
            print "x-" + plot['ID']

    print i
    for k, v in plot_dict.iteritems():
        thumb = v['thumbnail']
        max_im_vals[1] = max_im_vals[1]
        for b in range(0, 4):
            thumb[:, :, b] = thumb[:, :, b] / max_im_vals[b]
            thumb[:, :, b][thumb[:, :, b] > 1.] = 1.
        # thumb[:, :, 0] = thumb[:, :, 0] / 1.5
        # thumb[:, :, 1] = thumb[:, :, 1] * 1.2
        # thumb[:, :, 1][thumb[:, :, 1] > 1.] = 1.
        plot_dict[k]['thumbnail'] = thumb

    return plot_dict
예제 #6
0
파일: QpalsWSM.py 프로젝트: lwiniwar/qpals
    def export(self, progress):
        axis_x, axis_y = self.createAxisModel()
        aoi_coll = ogr.Geometry(type=ogr.wkbMultiPolygon)
        for sec in self.sections:
            aoi_coll.AddGeometry(sec.aoi_as_ogr())
        coll = aoi_coll.GetEnvelope()
        x_loc = np.arange(coll[0], coll[1], self.dX)
        y_loc = np.arange(coll[3], coll[2], -self.dY)

        # rasterize areas of interest to speed up processing
        coll_buf = aoi_coll.Buffer(2)
        drv = ogr.GetDriverByName("MEMORY")
        temp_vector_ds = drv.CreateDataSource("mem_vec")
        temp_layer = temp_vector_ds.CreateLayer("mem_vec",
                                                geom_type=ogr.wkbMultiPolygon)
        temp_feat = ogr.Feature(ogr.FeatureDefn())
        temp_feat.SetGeometry(coll_buf)
        temp_layer.CreateFeature(temp_feat)

        temp_ds = gdal.GetDriverByName("MEM").Create("mem_ras", len(x_loc),
                                                     len(y_loc), gdal.GDT_Byte)
        temp_ds.SetGeoTransform((coll[0] - self.dX / 2, self.dX, 0,
                                 coll[3] + self.dY / 2, 0, -self.dY))
        band = temp_ds.GetRasterBand(1)

        gdal.RasterizeLayer(temp_ds, [1], temp_layer, burn_values=[1])
        mask = np.copy(band.ReadAsArray())

        # free resources
        temp_vector_ds = None
        temp_ds = None

        results = np.empty((len(x_loc), len(y_loc)), dtype=np.float32)
        results.fill(np.NaN)
        valid_yidx, valid_xidx = np.nonzero(mask == 1)
        tot = len(valid_xidx)
        curr = 0

        for xidx, yidx in zip(valid_xidx, valid_yidx):
            if True:
                x = x_loc[xidx]
                y = y_loc[yidx]
                curr += 1
                s, q = self.findAxisCoordinates(axis_x, axis_y,
                                                np.array([x, y]))
                progress.emit(100 * curr / tot)
                try:
                    prevSec = [
                        sec for sec in self.sections
                        if sec.station < s and sec.status > 0
                    ][-1]
                    nextSec = [
                        sec for sec in self.sections
                        if sec.station >= s and sec.status > 0
                    ][0]
                    dStat = nextSec.station - prevSec.station
                    facS = (s - prevSec.station) / dStat
                    prevH_center = (abs(prevSec.left_h*prevSec.left_x)+abs(prevSec.right_h*prevSec.right_x)) \
                                   / (abs(prevSec.left_x)+abs(prevSec.right_x))
                    nextH_center = (abs(nextSec.left_h*nextSec.left_x)+abs(nextSec.right_h*nextSec.right_x)) \
                                   / (abs(nextSec.left_x)+abs(nextSec.right_x))
                    if q < 0:  # left river side
                        prevQ = prevSec.left_x
                        nextQ = nextSec.left_x
                        prevH = prevSec.left_h
                        nextH = nextSec.left_h
                    else:  # right river side
                        prevQ = prevSec.right_x
                        nextQ = nextSec.right_x
                        prevH = prevSec.right_h
                        nextH = nextSec.right_h
                    dQ = abs(nextQ) - abs(prevQ)
                    currQ = prevQ + facS * dQ
                    facQ = abs(q / currQ)
                    if facQ > 1:  # outside of section concave hull
                        results[xidx, yidx] = np.NaN
                    else:
                        prevX = prevH_center + (
                            prevH - prevH_center
                        ) * facQ  # linear interpolation prev section
                        nextX = nextH_center + (
                            nextH - nextH_center
                        ) * facQ  # linear interpolation next section
                        X = prevX + (
                            nextX - prevX
                        ) * facS  # linear interpolation between sections
                        results[xidx, yidx] = X

                except Exception as e:
                    # e.g. no prev section found
                    results[xidx, yidx] = np.NaN

        return results, coll
예제 #7
0
        fieldDefn25 = ogr.FieldDefn('in/out', ogr.OFTReal)
        fieldDefn26 = ogr.FieldDefn('out/in', ogr.OFTReal)
        fieldDefn27 = ogr.FieldDefn('indicator', ogr.OFTReal)
        layer2.CreateField(fieldDefn20)
        layer2.CreateField(fieldDefn21)
        layer2.CreateField(fieldDefn22)
        layer2.CreateField(fieldDefn23)
        layer2.CreateField(fieldDefn24)
        layer2.CreateField(fieldDefn25)
        layer2.CreateField(fieldDefn26)
        layer2.CreateField(fieldDefn27)

        counterA = 0
        sumincoming = 0
        while counterA < numberofnodes:
            linester2 = ogr.Geometry(ogr.wkbPoint)
            linester2.SetPoint(0, mypoints[counterA][0], mypoints[counterA][1])
            featureDefn2 = layer2.GetLayerDefn()
            feature2 = ogr.Feature(featureDefn2)
            feature2.SetGeometry(linester2)

            #set node names/start
            if IncludeNodeNames == 1:
                name = str(mypointnames[counterA])
                name = name[2:-2]
                feature2.SetField('name', name)
            elif IncludeNodeNames == 0:
                name = "Node_" + str(counterA + 1)
                feature2.SetField('name', name)
            #set node names/end
예제 #8
0
def ExtractAllDemFeatures(ds, csGtSpatialRef, csGtDict, plotFigures=False):  # , axis_signs=[1, 1]):
    # Note         A way around all this confusion may to be to layout plot co-ords in world co-ords and then convert to
    #              to pixel co-ords using world2pixel.  this would avoid all the fiddling and confusion in pixel space

    geotransform = ds.GetGeoTransform()
    transform = osr.CoordinateTransformation(csGtSpatialRef, osr.SpatialReference(ds.GetProjection()))
    i = 0
    plot_dict = {}
    # plotTagcDict = {}
    # class_labels = ['Pristine', 'Moderate', 'Severe']
    max_im_vals = np.zeros((ds.RasterCount))
    for plot in csGtDict.values():
        # transform plot corners into ds pixel space
        plotCnrsWorld = plot['points']
        plotCnrsPixel = []
        for cnr in plotCnrsWorld:
            point = ogr.Geometry(ogr.wkbPoint)
            point.AddPoint(cnr[0], cnr[1])
            point.Transform(transform)  # xform into im projection
            (pixel, line) = World2Pixel(geotransform, point.GetX(), point.GetY())
            plotCnrsPixel.append((pixel,line))

        plotCnrsPixel = np.array(plotCnrsPixel)

        # not all the point fall inside the image
        if np.all(plotCnrsPixel >=0) and  np.all(plotCnrsPixel[:,0] < ds.RasterXSize) \
                and np.all(plotCnrsPixel[:,1] < ds.RasterYSize) and plot.has_key('Yc') and plot['Yc'] > 0.:

            # get winddow extents
            ulCnr = np.floor(np.min(plotCnrsPixel, 0))
            lrCnr = np.ceil(np.max(plotCnrsPixel, 0))
            plotSizePixel = np.int32(lrCnr - ulCnr)

            # make a mask for this plot
            img = Image.fromarray(np.zeros((plotSizePixel[1], plotSizePixel[0])))

            # Draw a rotated rectangle on the image.
            draw = ImageDraw.Draw(img)
            # rect = get_rect(x=120, y=80, width=100, height=40, angle=30.0)
            draw.polygon([tuple((p - ulCnr)) for p in plotCnrsPixel], fill=1)
            # Convert the Image data to a numpy array.
            plotMask = np.asarray(img)

            # adjust yc if it exists
            if plot.has_key('Yc') and plot['Yc']>0:
                plot['YcPp'] = plot['Yc'] / plotMask.sum()  # the average per pixel in the mask
                plot['YcPm2'] = plot['Yc'] / (plot['Size']**2)  # the average per m2 in the theoretical plot size
            else:
                print '%s - no yc' % (plot['ID'])
                # plot['YcPp'] = 0.
                # plot['YcPm2'] = 0.

            # extract image patch with mask
            imbuf = np.zeros((plotSizePixel[1], plotSizePixel[0], ds.RasterCount), dtype=float)
            for b in range(1, ds.RasterCount+1):
                imbuf[:, :, b - 1] = ds.GetRasterBand(b).ReadAsArray(ulCnr[0], ulCnr[1], plotSizePixel[0],
                                                                     plotSizePixel[1])

            # imbuf[:, :, 3] = imbuf[:, :, 3] / 2  # hack for NGI XCALIB
            if np.all(imbuf == 0):
                print plot['ID'] + ": imbuf zero, assume NODATA ommitting"
                break
            # for b in range(0, 4):
            #     imbuf[:, :, b] = imbuf[:, :, b] / max_im_vals_[b]
            if not plotMask.shape == imbuf.shape[0:2]:
                print "error - mask and buf different sizes"
                raise Exception("error - mask and buf different sizes")
            feat = ExtractDemPatchFeatures(imbuf.copy(), plotMask)

            fields = plot.keys()
            for f in fields:
                feat[f] = csGtDict[plot['ID']][f]
            feat['thumbnail'] = np.float32(imbuf.copy())
            plot_dict[plot['ID']] = feat
            # plotTagcDict[plot['PLOT']] = csGtDict[plot['PLOT']]['TAGC']
            tmp = np.reshape(feat['thumbnail'], (np.prod(plotSizePixel), ds.RasterCount))
            # max_tmp = tmp.max(axis=0)
            max_tmp = np.percentile(tmp, 98., axis=0)
            max_im_vals[max_tmp > max_im_vals] = max_tmp[max_tmp > max_im_vals]
            # print plot['PLOT']
            i = i + 1
        else:
            print "x-" + plot['ID']

    print i
    for k, v in plot_dict.iteritems():
        thumb = v['thumbnail']
        # max_im_vals[1] = max_im_vals[1]
        for b in range(0, ds.RasterCount):
            thumb[:, :, b] = thumb[:, :, b] / max_im_vals[b]
            thumb[:, :, b][thumb[:, :, b] > 1.] = 1.
        # thumb[:, :, 0] = thumb[:, :, 0] / 1.5
        # thumb[:, :, 1] = thumb[:, :, 1] * 1.2
        # thumb[:, :, 1][thumb[:, :, 1] > 1.] = 1.
        plot_dict[k]['thumbnail'] = thumb.squeeze()

    return plot_dict
            feat = wuchs_lyr.GetFeature(i)
            geom = feat.GetGeometryRef()
            geom_wkt = geom.ExportToWkt()

            wuchs_name = feat.GetField('Name')
            wuchs_name = wuchs_name.replace("/","-")
            wuchs_id = feat.GetField('ID')
            print(wuchs_id, wuchs_name)

            # create memory layer for rasterization
            driver_mem = ogr.GetDriverByName('Memory')
            ogr_ds = driver_mem.CreateDataSource('wrk')
            ogr_lyr = ogr_ds.CreateLayer('poly', srs=sr)

            feat_mem = ogr.Feature(ogr_lyr.GetLayerDefn())
            feat_mem.SetGeometryDirectly(ogr.Geometry(wkt=geom_wkt))

            ogr_lyr.CreateFeature(feat_mem)

            ras_pth = r'O:\Student_Data\CJaenicke\00_MA\data\phenology\{0}_{1}_3035.tif'.format(abr, year)
            ras = gdal.Open(ras_pth)

            # rasterize geom
            col = ras.RasterXSize
            row = ras.RasterYSize

            gt = ras.GetGeoTransform()

            target_ds = gdal.GetDriverByName('MEM').Create('', col, row, 1, gdal.GDT_Byte)
            target_ds.SetGeoTransform(gt)
예제 #10
0
fd = ogr.FieldDefn('Id', ogr.OFTString)
layer.CreateField(fd)

fd = ogr.FieldDefn('Info', ogr.OFTString)
layer.CreateField(fd)

# ----------------------------------------------------------------------------
# Write GCPs.
# ----------------------------------------------------------------------------

for gcp in gcps:

    feat = ogr.Feature(layer.GetLayerDefn())

    geom = ogr.Geometry(ogr.wkbPoint25D)

    if pixel_out == 0:
        feat.SetField('Pixel', gcp.GCPPixel)
        feat.SetField('Line', gcp.GCPLine)
        geom.SetPoint(0, gcp.GCPX, gcp.GCPY, gcp.GCPZ)
    else:
        feat.SetField('X', gcp.GCPX)
        feat.SetField('Y', gcp.GCPY)
        geom.SetPoint(0, gcp.GCPPixel, gcp.GCPLine)

    feat.SetField('Id', gcp.Id)
    feat.SetField('Info', gcp.Info)

    feat.SetGeometryDirectly(geom)
    layer.CreateFeature(feat)
예제 #11
0
def Raster_to_Points(input_tiff, output_shp):
    """
    Converts a raster to a point shapefile
    """
    # Input
    inp_lyr = gdal.Open(input_tiff)
    inp_srs = inp_lyr.GetProjection()
    transform = inp_lyr.GetGeoTransform()
    inp_band = inp_lyr.GetRasterBand(1)

    top_left_x = transform[0]
    cellsize_x = transform[1]
    top_left_y = transform[3]
    cellsize_y = transform[5]
    NoData_value = inp_band.GetNoDataValue()

    x_tot_n = inp_lyr.RasterXSize
    y_tot_n = inp_lyr.RasterYSize

    top_left_x_center = top_left_x + cellsize_x / 2.0
    top_left_y_center = top_left_y + cellsize_y / 2.0

    # Read array
    array = inp_lyr.ReadAsArray(0, 0, x_tot_n, y_tot_n)  # .astype(pd.np.float)
    array[pd.np.isclose(array, NoData_value)] = pd.np.nan

    # Output
    out_srs = osr.SpatialReference()
    out_srs.ImportFromWkt(inp_srs)
    out_name = os.path.splitext(os.path.basename(output_shp))[0]
    out_driver = ogr.GetDriverByName('ESRI Shapefile')
    if os.path.exists(output_shp):
        out_driver.DeleteDataSource(output_shp)
    out_source = out_driver.CreateDataSource(output_shp)

    out_lyr = out_source.CreateLayer(out_name, out_srs, ogr.wkbPoint)
    ogr_field_type = ogrtype_from_dtype(array.dtype)
    Add_Field(out_lyr, "RASTERVALU", ogr_field_type)
    out_lyr_defn = out_lyr.GetLayerDefn()

    # Add features
    for xi in range(x_tot_n):
        for yi in range(y_tot_n):
            value = array[yi, xi]
            if ~pd.np.isnan(value):
                feature_out = ogr.Feature(out_lyr_defn)

                feature_out.SetField2(0, value)

                point = ogr.Geometry(ogr.wkbPoint)
                point.AddPoint(top_left_x_center + xi * cellsize_x,
                               top_left_y_center + yi * cellsize_y)

                feature_out.SetGeometry(point)
                out_lyr.CreateFeature(feature_out)

                feature_out = None

    # Save and/or close the data sources
    inp_lyr = None
    out_source = None

    # Return
    return output_shp
예제 #12
0
#in this case wgs84
srs.ImportFromEPSG(4326)

#create shapefile layer as line data with wgs84 as spatial reference
layer = data_source.CreateLayer(layer_name, srs, ogr.wkbLineString)

#create "Name" column for attribute table and set type as string
field_name = ogr.FieldDefn("Name", ogr.OFTString)
field_name.SetWidth(24)
layer.CreateField(field_name)

#list of lines geometry
lines = []

#define first line geometry
line_1 = ogr.Geometry(ogr.wkbLineString)
#add points into first line geometry
line_1.AddPoint(longitude[0], latitude[0])
line_1.AddPoint(longitude[1], latitude[1])
line_1.AddPoint(longitude[2], latitude[2])
lines.append(line_1)

#define second line geometry
line_2 = ogr.Geometry(ogr.wkbLineString)
#add points into second line geometry
line_2.AddPoint(longitude[0], latitude[1])
line_2.AddPoint(longitude[1], latitude[2])
line_2.AddPoint(longitude[2], latitude[1])
lines.append(line_2)

for i in range(len(lines)):
예제 #13
0
def create_buffer(inputfn, output_bufferfn, buffer_dist, field_name):
    driver = ogr.GetDriverByName('ESRI Shapefile')
    inputds = driver.Open(inputfn, 0)
    if inputds is None:
        raise RuntimeError('Could not open %s' % (inputfn))
    inputlyr = inputds.GetLayer()
    featureCount = inputlyr.GetFeatureCount()
    print('\tNumber of features in %s: %d' %
          (os.path.basename(inputfn), featureCount))
    inputdef = inputlyr.GetLayerDefn()
    for i in range(inputdef.GetFieldCount()):
        inputfName = inputdef.GetFieldDefn(i).GetName()
        inputfTypeCode = inputdef.GetFieldDefn(i).GetType()
        inputfTypeName = inputdef.GetFieldDefn(i).GetFieldTypeName(
            inputfTypeCode)
        inputfWidth = inputdef.GetFieldDefn(i).GetWidth()
        inputfPrecision = inputdef.GetFieldDefn(i).GetPrecision()
        print('\t', i, inputfName, inputfTypeCode, inputfTypeName, inputfWidth,
              inputfPrecision)

    # prepare environment
    shpdriver = ogr.GetDriverByName('ESRI Shapefile')
    if os.path.exists(output_bufferfn):
        # os.remove(output_bufferfn)
        shpdriver.DeleteDataSource(output_bufferfn)

    # create file
    outputBufferds = shpdriver.CreateDataSource(output_bufferfn)

    # create layer
    bufferlyr = outputBufferds.CreateLayer(output_bufferfn,
                                           geom_type=ogr.wkbPolygon)

    # access layer
    featureDefn = bufferlyr.GetLayerDefn()

    # load features in all layers
    outFeature = None
    # for feature in inputlyr:
    #     print('\t{}'.format(feature.GetField(field_name)))
    #     ingeom = feature.GetGeometryRef()
    #
    #     # buffer features
    #     geomBuffer = ingeom.Buffer(buffer_dist)
    #
    #     # create features
    #     outFeature = ogr.Feature(featureDefn)
    #     outFeature.SetGeometry(geomBuffer)
    #     bufferlyr.CreateFeature(outFeature)
    #
    #     # deallocated features
    #     outFeature = None

    minX, maxX, minY, maxY = float("infinity"), -float("infinity"), float(
        "infinity"), -float("infinity")
    for feature in inputlyr:
        ingeom = feature.GetGeometryRef()
        (tmp_minX, tmp_maxX, tmp_minY, tmp_maxY) = ingeom.GetEnvelope()
        if tmp_minX < minX:
            minX = tmp_minX
        if tmp_maxX > maxX:
            maxX = tmp_maxX
        if tmp_minY < minY:
            minY = tmp_minY
        if tmp_maxY > maxY:
            maxY = tmp_maxY
    inputlyr.ResetReading()
    minX = minX - buffer_dist
    maxX = maxX + buffer_dist
    minY = minY - buffer_dist
    maxY = maxY + buffer_dist

    outFeature = ogr.Feature(featureDefn)
    ring = ogr.Geometry(ogr.wkbLinearRing)
    ring.AddPoint(minX, maxY)
    ring.AddPoint(maxX, maxY)
    ring.AddPoint(maxX, minY)
    ring.AddPoint(minX, minY)
    ring.AddPoint(minX, maxY)
    poly = ogr.Geometry(ogr.wkbPolygon)
    poly.AddGeometry(ring)
    outFeature.SetGeometry(poly)
    bufferlyr.CreateFeature(outFeature)
    outFeature = None
예제 #14
0
def points_in_polygon(inputfn, pointcoords, dir_fig):
    # load features
    # 0 means read-only. 1 means writeable.
    driver = ogr.GetDriverByName('ESRI Shapefile')
    inputds = driver.Open(inputfn, 0)

    if inputds is None:
        raise RuntimeError('Could not open %s' % (inputfn))
    inputlyr = inputds.GetLayer()

    featureCount = inputlyr.GetFeatureCount()
    print('\tNumber of features in %s: %d' %
          (os.path.basename(inputfn), featureCount))

    inputdef = inputlyr.GetLayerDefn()
    for i in range(inputdef.GetFieldCount()):
        inputfName = inputdef.GetFieldDefn(i).GetName()
        inputfTypeCode = inputdef.GetFieldDefn(i).GetType()
        inputfTypeName = inputdef.GetFieldDefn(i).GetFieldTypeName(
            inputfTypeCode)
        inputfWidth = inputdef.GetFieldDefn(i).GetWidth()
        inputfPrecision = inputdef.GetFieldDefn(i).GetPrecision()
        print('\t', i, inputfName, inputfTypeCode, inputfTypeName, inputfWidth,
              inputfPrecision)

    minX, maxX, minY, maxY = float("infinity"), -float("infinity"), float(
        "infinity"), -float("infinity")
    for feature in inputlyr:
        ingeom = feature.GetGeometryRef()
        (tmp_minX, tmp_maxX, tmp_minY, tmp_maxY) = ingeom.GetEnvelope()
        if tmp_minX < minX:
            minX = tmp_minX
        if tmp_maxX > maxX:
            maxX = tmp_maxX
        if tmp_minY < minY:
            minY = tmp_minY
        if tmp_maxY > maxY:
            maxY = tmp_maxY
    inputlyr.ResetReading()

    # load points
    # check if polygon contains point
    icoord = 0
    in_poly = []
    in_bbox = {'id': [], 'x': [], 'y': []}
    for coord in pointcoords:
        # print('\t', icoord)
        is_contain = False

        if np.logical_and(minX <= coord[0] <= maxX, minY <= coord[1] <= maxY):
            print('\tMascon id={}, xy={}'.format(icoord, coord))
            in_bbox['id'].append(icoord)
            in_bbox['x'].append(coord[0])
            in_bbox['y'].append(coord[1])

            for feature in inputlyr:
                ingeom = feature.GetGeometryRef()
                (tmp_minX, tmp_maxX, tmp_minY, tmp_maxY) = ingeom.GetEnvelope()

                if np.logical_and(tmp_minX <= coord[0] <= tmp_maxX,
                                  tmp_minY <= coord[1] <= tmp_maxY):
                    point = ogr.Geometry(ogr.wkbPoint)
                    point.AddPoint(coord[0], coord[1])

                    ingeomTypeName = ingeom.GetGeometryName()
                    # print('\t{}'.format(feature.GetFID()), ingeomTypeName)
                    # print('\t{}'.format(feature.GetFID()), feature.keys())
                    # print('\t{}'.format(feature.GetFID()), feature.items())
                    # print('\t{}'.format(feature.GetFID()), feature.geometry())

                    ipoly = 0
                    data = {'x': [], 'y': [], 'c': [], 'd': []}
                    if ingeomTypeName == 'MULTIPOLYGON':
                        for polygon in ingeom:
                            is_contain = polygon.Contains(point)

                            fig, ax = plt.subplots(1)
                            if is_contain:
                                title = 'Mascon id={} xy={}, Poly id={} FID={}'.format(
                                    icoord, coord, ipoly, feature.GetFID())
                                # print(title)

                                plt.title(title)
                                for ring in polygon:
                                    # print('\t', ipoly, ingeomTypeName, polygon.GetGeometryName(), ring.GetGeometryName())
                                    # if int(feature.GetFID()) > 0:
                                    #     title = 'Mascon id={}, xy={}; Area id={}, FID={}'.format(icoord, coord, ipoly, feature.GetFID())
                                    #     print(title)
                                    data['x'] = [
                                        ring.GetPoint(ipt)[0] for ipt in range(
                                            0, ring.GetPointCount())
                                    ]
                                    data['y'] = [
                                        ring.GetPoint(ipt)[1] for ipt in range(
                                            0, ring.GetPointCount())
                                    ]
                                    data['c'] = [
                                        ipt for ipt in range(
                                            0, ring.GetPointCount())
                                    ]
                                    data['d'] = [
                                        1 for ipt in range(
                                            0, ring.GetPointCount())
                                    ]

                                    plt.scatter(coord[0], coord[1], marker='o')
                                    plt.scatter(x='x',
                                                y='y',
                                                c='c',
                                                s='d',
                                                marker='.',
                                                data=data)
                                plt.axis("auto")
                                # plt.show()
                                plt.savefig('{}.jpg'.format(
                                    os.path.join(dir_fig, title)))
                                plt.cla()

                                in_poly.append(icoord)

                            plt.close(fig)
                            ipoly += 1
                    elif ingeomTypeName == 'POLYGON':
                        is_contain = ingeom.Contains(point)

                        fig, ax = plt.subplots(1)
                        if is_contain:
                            title = 'Mascon id={} xy={}, Poly id={} FID={}'.format(
                                icoord, coord, ipoly, feature.GetFID())
                            # print(title)

                            plt.title(title)
                            for ring in ingeom:
                                # print('\t', ipoly, ingeomTypeName, ring.GetGeometryName())
                                data['x'] = [
                                    ring.GetPoint(ipt)[0]
                                    for ipt in range(0, ring.GetPointCount())
                                ]
                                data['y'] = [
                                    ring.GetPoint(ipt)[1]
                                    for ipt in range(0, ring.GetPointCount())
                                ]
                                data['c'] = [
                                    ipt
                                    for ipt in range(0, ring.GetPointCount())
                                ]
                                data['d'] = [
                                    1 for ipt in range(0, ring.GetPointCount())
                                ]

                                plt.scatter(coord[0], coord[1], marker='o')
                                plt.scatter(x='x',
                                            y='y',
                                            c='c',
                                            s='d',
                                            marker='.',
                                            data=data)
                            plt.axis("auto")
                            # plt.show()
                            plt.savefig('{}.jpg'.format(
                                os.path.join(dir_fig, title)))
                            plt.cla()

                            in_poly.append(icoord)

                        plt.close(fig)
                        ipoly += 1
                    else:
                        # print('\t', ipoly, ingeomTypeName)
                        # ipoly += 1
                        pass

            # reset the read position to the start
            inputlyr.ResetReading()

        icoord += 1

    fig, ax = plt.subplots(1)
    title = 'Mascon_points_bbox'
    plt.title(title)

    for feature in inputlyr:
        ingeom = feature.GetGeometryRef()
        data = {'x': [], 'y': [], 'c': [], 'd': []}
        if ingeomTypeName == 'MULTIPOLYGON':
            for polygon in ingeom:
                for ring in polygon:
                    data['x'] = [
                        ring.GetPoint(ipt)[0]
                        for ipt in range(0, ring.GetPointCount())
                    ]
                    data['y'] = [
                        ring.GetPoint(ipt)[1]
                        for ipt in range(0, ring.GetPointCount())
                    ]
                    data['c'] = [ipt for ipt in range(0, ring.GetPointCount())]
                    data['d'] = [1 for ipt in range(0, ring.GetPointCount())]
                    plt.scatter(x='x',
                                y='y',
                                c='c',
                                s='d',
                                marker='.',
                                data=data)
    inputlyr.ResetReading()

    plt.scatter(x='x', y='y', marker='o', data=in_bbox)
    for i in range(len(in_bbox['id'])):
        plt.text(x=in_bbox['x'][i], y=in_bbox['y'][i], s=in_bbox['id'][i])
    # plt.axis("auto")
    plt.xlim(minX, maxX)
    plt.ylim(minY, maxY)
    # plt.show()
    plt.savefig('{}.jpg'.format(os.path.join(dir_fig, title)))
    plt.cla()
    plt.close(fig)

    return np.unique(np.array(in_poly))
def createPatches(img,
                  geotImg,
                  x,
                  y,
                  outputWindowSize,
                  shapefiles,
                  imgName,
                  datasetPath,
                  isTrain=False,
                  createMask=False,
                  cropCount=0):
    '''
    Create a crop of img with center x,y and total size 'outputWindowSize'
    if needed, can generate a mask for the crop based on the shapefiles
    '''

    # get info from the geoTransform of the image
    xOrigin = geotImg[0]
    yOrigin = geotImg[3]
    pixelWidth = geotImg[1]
    offsetX, offsetY = 0, 0
    # convert central coordinate to pixel
    centralX, centralY = utils.CoordinateToPixel(geotImg, (x, y))

    # get initial pixel - upper left
    pX, pY = centralX - int(outputWindowSize / 2), centralY - int(
        outputWindowSize / 2)
    if pX < 0:
        offsetX = 0 - pX
        pX = 0
    if pY < 0:
        offsetY = 0 - pY
        pY = 0

    # get final pixel - right down
    pXSize, pYSize = centralX + int(
        outputWindowSize / 2) + offsetX, centralY + int(
            outputWindowSize / 2) + offsetY

    # transform pixels back to coordinates
    xBegin, yBegin = utils.PixelToCoordinate(geotImg, (pX, pY))
    xFinal, yFinal = utils.PixelToCoordinate(geotImg, (pXSize, pYSize))

    # create polygon (or patch) based on the coordinates
    poly = ogr.Geometry(ogr.wkbPolygon)
    ring = ogr.Geometry(ogr.wkbLinearRing)
    ring.AddPoint(xBegin, yBegin)
    ring.AddPoint(xBegin, yFinal)
    ring.AddPoint(xFinal, yFinal)
    ring.AddPoint(xFinal, yBegin)
    ring.AddPoint(xBegin, yBegin)
    ring.CloseRings()
    poly.AddGeometry(ring)

    # create patch array
    xoff = int((xBegin - xOrigin) / pixelWidth)
    yoff = int((yOrigin - yBegin) / pixelWidth)
    # xcount = int(np.round(abs(xFinal - xBegin) / pixelWidth))
    # ycount = int(np.round(abs(yFinal - yBegin) / pixelWidth))
    xcount = outputWindowSize
    ycount = outputWindowSize
    # print('xoff_v', xoff, yoff, xcount, ycount, pixelWidth)

    npImageArray = np.moveaxis(img.ReadAsArray(xoff, yoff, xcount, ycount), 0,
                               -1)[:, :, 0:3]
    # print('shape', npImageArray.shape)

    imgref = osr.SpatialReference(wkt=img.GetProjectionRef())
    xoffCoord, yoffCoord = utils.PixelToCoordinate(geotImg, (xoff, yoff))
    geotCoord = (xoffCoord, geotImg[1], geotImg[2], yoffCoord, geotImg[4],
                 geotImg[5])

    npMask = None
    if isTrain and createMask:
        npMask = createGroundTruth(shapefiles,
                                   npImageArray,
                                   imgref,
                                   geotCoord,
                                   imgName,
                                   datasetPath,
                                   cropCount=cropCount)

    return poly, npImageArray, npMask
예제 #16
0
 else:
     is_paleo = False
 cdftime = utime(time_units, time_calendar)
 for k, t in enumerate(time):
     if is_paleo:
         timestamp = "1-1-1"
         my_year = k
     else:
         timestamp = cdftime.num2date(t)
         my_year = 0
     print(("Processing {}".format(timestamp)))
     for level in contour_levels:
         contour_var = np.array(ppt.permute(nc.variables[varname], var_order), order="C")[k, Ellipsis]
         contour_points = get_contours(contour_var, x, y, nc_projection, level)
         # For each contour
         polygon = ogr.Geometry(ogr.wkbPolygon)
         for point in range(0, len(contour_points)):
             geoLocations = contour_points[point]
             ring = ogr.Geometry(ogr.wkbLinearRing)
             # For each point,
             for pointIndex, geoLocation in enumerate(geoLocations):
                 ring.AddPoint(geoLocation[0], geoLocation[1])
             ring.CloseRings()
             polygon.AddGeometry(ring)
         # Create feature
         featureDefn = layer.GetLayerDefn()
         feature = ogr.Feature(featureDefn)
         feature.SetGeometry(polygon)
         feature.SetFID(k)
         i = feature.GetFieldIndex("level")
         feature.SetField(i, level)
        feature2 = layer.GetFeature(j)
        if feature1.GetField('id') != feature2.GetField('id'):
            geometry1 = feature1.GetGeometryRef()
            geometry2 = feature2.GetGeometryRef()
            mpoint = geometry1.Intersection(geometry2)
            if mpoint.IsEmpty() != True:
                intersection.append(mpoint)
                #print mpoint.GetGeometryName()
                #print mpoint.GetPointCount()
                print[(mpoint.GetX(), mpoint.GetY())
                      for j in range(0, mpoint.GetPointCount())]
                print mpoint.GetPointCount()

layer.ResetReading()
print len(intersection)
point = ogr.Geometry(ogr.wkbMultiPoint)
[point.AddGeometryDirectly(geom) for geom in intersection]
#print point.ExportToKML()
createShp(point)  #create output shape file

#graph from nodes
G = nx.DiGraph()
nodeList = range(0, len(intersection))

G.add_nodes_from(nodeList)
print G.nodes()
for l in range(0, len(intersection)):
    G.node[l]['name'] = intersection[l].GetPoint()

#print G.nodes()
#for p in range(1,len(intersection)):
예제 #18
0
def write_topography_multidems_ptshp(fileName, multidems_headers, dem_names,
                                     multiprofile_dem_data, sr):

    shape_driver_name = "ESRI Shapefile"
    shape_driver = ogr.GetDriverByName(shape_driver_name)
    if shape_driver is None:
        return False, "%s driver is not available" % shape_driver_name

    try:
        datasource = shape_driver.CreateDataSource(unicode(fileName))
    except TypeError:
        datasource = shape_driver.CreateDataSource(str(fileName))

    if datasource is None:
        return False, "Creation of %s shapefile failed" % os.path.split(
            fileName)[1]

    layer = datasource.CreateLayer('profile', sr, geom_type=ogr.wkbPoint)
    if layer is None:
        return False, "Output layer creation failed"

    # creates required fields
    layer.CreateField(ogr.FieldDefn(multidems_headers[0], ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn(multidems_headers[1], ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn(multidems_headers[2], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(multidems_headers[3], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(multidems_headers[4], ogr.OFTReal))

    for dem_ndx in range(len(dem_names)):
        layer.CreateField(
            ogr.FieldDefn(multidems_headers[4 + dem_ndx * 3 + 1], ogr.OFTReal))
        layer.CreateField(
            ogr.FieldDefn(multidems_headers[4 + dem_ndx * 3 + 2], ogr.OFTReal))
        layer.CreateField(
            ogr.FieldDefn(multidems_headers[4 + dem_ndx * 3 + 3], ogr.OFTReal))

    featureDefn = layer.GetLayerDefn()

    field_names = []
    for i in range(featureDefn.GetFieldCount()):
        field_names.append(featureDefn.GetFieldDefn(i).GetName())

    assert len(multidems_headers) == len(field_names)

    # loops through output records

    for prof_ndx, profile_data in enumerate(multiprofile_dem_data):

        for rec in profile_data:

            pt_feature = ogr.Feature(featureDefn)

            pt = ogr.Geometry(ogr.wkbPoint)
            pt.SetPoint(0, rec[1], rec[2])
            pt_feature.SetGeometry(pt)

            pt_feature.SetField(field_names[0], prof_ndx)
            pt_feature.SetField(field_names[1], rec[0])
            pt_feature.SetField(field_names[2], rec[1])
            pt_feature.SetField(field_names[3], rec[2])
            pt_feature.SetField(field_names[4], rec[3])
            for dem_ndx in range(len(dem_names)):
                dem_height = rec[3 + dem_ndx * 3 + 1]
                if dem_height != '':
                    pt_feature.SetField(field_names[4 + dem_ndx * 3 + 1],
                                        dem_height)
                cum3ddist = rec[3 + dem_ndx * 3 + 2]
                if cum3ddist != '':
                    pt_feature.SetField(field_names[4 + dem_ndx * 3 + 2],
                                        cum3ddist)
                slope = rec[3 + dem_ndx * 3 + 3]
                if slope != '':
                    pt_feature.SetField(field_names[4 + dem_ndx * 3 + 3],
                                        slope)

            layer.CreateFeature(pt_feature)

            pt_feature.Destroy()

    datasource.Destroy()

    return True, "Done"
예제 #19
0
def get_node_network(stream_dataset, stream_id_column, output_node_shp,
                     output_table_path, output_table_rev_path, tolerance):
    global printer
    if not printer:
        printer = PassPrint()

    printer.msg("Opening dataset..")
    streams_ds = feature_utils.getFeatureDataset(stream_dataset)
    streams_layer = streams_ds.GetLayer()
    streams_srs = streams_layer.GetSpatialRef()
    streams_defn = streams_layer.GetLayerDefn()

    _check_srs_units(srs=streams_srs)

    # get field indices
    id_field_index = streams_defn.GetFieldIndex(stream_id_column)
    if id_field_index < 0:
        raise Exception(
            "Could not find stream ID column ({0})".format(stream_id_column))
    id_field_defn = streams_defn.GetFieldDefn(id_field_index)

    all_nodes = []
    all_stream_ids = []
    # all_feats = []
    current_id = 1

    # process all nodes at end points of all lines
    printer.msg("Creating nodes..")
    stream_feat = streams_layer.GetNextFeature()
    while stream_feat:
        stream_id = feature_utils.getFieldValue(stream_feat, id_field_defn)
        all_stream_ids.append(stream_id)

        geom = stream_feat.GetGeometryRef()
        points = geom.GetPoints()

        first_node = Node(current_id)
        current_id += 1
        first_node.coords = points[0]
        first_node.segments.append(stream_id)
        all_nodes.append(first_node)

        last_node = Node(current_id)
        current_id += 1
        last_node.coords = points[-1]
        last_node.segments.append(stream_id)
        all_nodes.append(last_node)

        # all_feats.append(stream_feat)
        stream_feat = streams_layer.GetNextFeature()

    # at this point no longer need dataset
    streams_defn = None
    streams_layer = None
    streams_ds = None

    printer.msg("Finding node intersections..")
    tolerance2 = tolerance * tolerance
    filtered_nodes = []
    connected_node_ids = []

    # outer loop on every node
    start_index = 0
    count_nodes = len(all_nodes)
    while start_index < count_nodes - 1:
        this_node = all_nodes[start_index]
        start_index += 1
        # skip if it's already been connected
        if this_node.id in connected_node_ids:
            continue
        filtered_nodes.append(this_node)
        # inner loop on every node pair
        for i in range(start_index, count_nodes, 1):
            this_coords = this_node.coords
            that_coords = all_nodes[i].coords
            # check overlap
            distance_x = abs(this_coords[0] - that_coords[0])
            if distance_x <= tolerance:
                distance_y = abs(this_coords[1] - that_coords[1])
                if distance_y <= tolerance:
                    distance = distance_x * distance_x + distance_y * distance_y
                    if distance <= tolerance2:
                        # overlaps, consolidate nodes
                        this_node.segments += all_nodes[i].segments
                        connected_node_ids.append(all_nodes[i].id)

    printer.msg("Finalizing network..")
    # reverse mapping by stream id
    network_map = {}
    for sid in all_stream_ids:
        network_map[sid] = []
    # reassign node ids while we go
    current_id = 1
    for node in filtered_nodes:
        node.id = current_id
        current_id += 1
        for sid in node.segments:
            network_map[sid].append(node.id)

    printer.msg("Saving node network table..")
    if output_table_path:
        with open(output_table_path, 'wb') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(["NODE", "STREAM_IDS"])
            for node in filtered_nodes:
                writer.writerow(
                    [node.id, ",".join([str(sid) for sid in node.segments])])
    if output_table_rev_path:
        with open(output_table_rev_path, 'wb') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(["STREAM_ID", "NODES"])
            for sid, nids in network_map.iteritems():
                writer.writerow([sid, ",".join([str(nid) for nid in nids])])

    printer.msg("Saving node shapefile..")
    fields = [{
        'name': "NODE",
        'type': int
    }, {
        'name': "STREAM_IDS",
        'type': basestring
    }, {
        'name': "END_POINT",
        'type': int
    }, {
        'name': "DRAINAGE",
        'type': int
    }]
    node_ds = feature_utils.createFeatureDataset(output_node_shp,
                                                 "nodes",
                                                 streams_srs,
                                                 ogr.wkbPoint,
                                                 fields=fields,
                                                 overwrite=True)
    node_layer = node_ds.GetLayer()
    defn = node_layer.GetLayerDefn()
    for node in filtered_nodes:
        feat = ogr.Feature(defn)
        pt = ogr.Geometry(ogr.wkbPoint)
        pt.AddPoint(node.coords[0], node.coords[1])
        streams = node.segments
        feat.SetGeometry(pt)
        feat.SetField(fields[0]['name'], node.id)
        feat.SetField(fields[1]['name'],
                      ",".join([str(sid) for sid in streams]))
        feat.SetField(fields[2]['name'], 1 if len(streams) <= 1 else 0)
        feat.SetField(fields[3]['name'], 0)
        node_layer.CreateFeature(feat)
        feat = None
    defn = None
    node_layer = None
    node_ds = None
예제 #20
0
def write_topography_gpx_ptshp(output_filepath, header_list,
                               gpx_parsed_results, sr):

    shape_driver_name = "ESRI Shapefile"
    shape_driver = ogr.GetDriverByName(shape_driver_name)
    if shape_driver is None:
        return False, "%s driver is not available" % shape_driver_name

    try:
        datasource = shape_driver.CreateDataSource(unicode(output_filepath))
    except TypeError:
        datasource = shape_driver.CreateDataSource(str(output_filepath))

    if datasource is None:
        return False, "Creation of %s shapefile failed" % os.path.split(
            output_filepath)[1]

    layer = datasource.CreateLayer('profile', sr, geom_type=ogr.wkbPoint)
    if layer is None:
        return False, "Point layer creation failed"

        # creates required fields
    layer.CreateField(ogr.FieldDefn(header_list[0], ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn(header_list[1], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[2], ogr.OFTReal))
    time_field = ogr.FieldDefn(header_list[3], ogr.OFTString)
    time_field.SetWidth(20)
    layer.CreateField(time_field)
    layer.CreateField(ogr.FieldDefn(header_list[4], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[5], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[6], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[7], ogr.OFTReal))

    featureDefn = layer.GetLayerDefn()

    # loops through output records
    for rec in gpx_parsed_results:

        pt_feature = ogr.Feature(featureDefn)

        pt = ogr.Geometry(ogr.wkbPoint)
        pt.SetPoint(0, rec[2], rec[1])
        pt_feature.SetGeometry(pt)

        pt_feature.SetField(header_list[0], rec[0])
        pt_feature.SetField(header_list[1], rec[1])
        pt_feature.SetField(header_list[2], rec[2])

        pt_feature.SetField(header_list[3], str(rec[3]))
        if rec[4] != '':
            pt_feature.SetField(header_list[4], str(rec[4]))
        pt_feature.SetField(header_list[5], rec[5])
        if rec[6] != '':
            pt_feature.SetField(header_list[6], rec[6])
        if rec[7] != '':
            pt_feature.SetField(header_list[7], rec[7])

        layer.CreateFeature(pt_feature)

        pt_feature.Destroy()

    datasource.Destroy()

    return True, "done"
    def extract_from_dataset(self, input_path, cfg):
        """ """
        if 'product_name' not in cfg['input_options']:
            raise Exception('You must specify a product name with --input-options product_name="XXXXX")')

        if 'dataset_duration' not in cfg['input_options']:
            raise Exception('You must specify a duration (in seconds) for the dataset with --input-options dataset_duration=XXXXX')

        date_fmt = '%Y-%m-%d %H:%M:%S'

        filename = os.path.basename(input_path)
        dataset_name = os.path.splitext(filename)[0]
        if 'dataset_suffix' in cfg['input_options']:
            dataset_name = '{}{}'.format(dataset_name, cfg['input_options']['dataset_suffix'])
        product_name = cfg['input_options'].get('product_name', None)
        dataset_duration = int(cfg['input_options']['dataset_duration'])
        dataset = netCDF4.Dataset(input_path, 'r')

        # Valid extent
        valid_extent = cfg['input_options'].get('valid_extent', None)
        if valid_extent is not None:
            valid_extent = map(str.strip, valid_extent.split(','))
            valid_extent = map(float, valid_extent)
            if 4 < len(valid_extent):
                logger.warn('valid_extent ignored because it contains less than 4 values.') 
                valid_extent = None
            else:
                geometry = ogr.Geometry(ogr.wkbLinearRing)
                geometry.AddPoint(valid_extent[0], valid_extent[1])
                geometry.AddPoint(valid_extent[2], valid_extent[1])
                geometry.AddPoint(valid_extent[2], valid_extent[3])
                geometry.AddPoint(valid_extent[0], valid_extent[3])
                geometry.AddPoint(valid_extent[0], valid_extent[1])
                polygon = ogr.Geometry(ogr.wkbPolygon)
                polygon.AddGeometry(geometry)
                valid_extent = polygon

        timestamp = dataset.variables['DateTime'][0]
        data = []
        n = len(dataset.dimensions['segment'])
        for i in xrange(0,n):
            seg_length = dataset.variables['segment_length'][i]
            if 0 >= seg_length:
                # no data
                continue

            # -1 because it seems segment_start refers to a 1-indexed array
            seg_start = dataset.variables['segment_start'][i] - 1

            lats = dataset.variables['latitude'][seg_start:seg_start+seg_length]
            lons = dataset.variables['longitude'][seg_start:seg_start+seg_length]
            sst_diff = dataset.variables['sst_difference'][seg_start:seg_start+seg_length]

            values = zip(lats, lons, sst_diff)
            values = filter(lambda x: None not in x and numpy.ma.masked not in x, values)

            if valid_extent is not None:
                p = ogr.Geometry(ogr.wkbPoint)
                p.AddPoint_2D(float(values[0][1]), float(values[0][0]))
                if not p.Within(valid_extent):
                    # Arbitrary policy: fronts belong to the BBOX that contain
                    # their first point.
                    # First point is not in the BBOX => skip the front entirely
                    continue

            if 0 >= len(values):
                # All values have been filtered, nothing left
                continue
            logger.error(values)
            segment_data = map(lambda x: {'lat': x[0], 'lon': x[1], 'fields': {'sst_difference': abs(x[2])}}, values)
            data.extend(segment_data)

            # Mark the end of the front
            data.append({'lat': None, 'lon': None})
        dataset.close()
        
        start = datetime.fromtimestamp(timestamp - 0.5 * dataset_duration)
        stop = datetime.fromtimestamp(timestamp + 0.5 * dataset_duration)
        metadata = { 'begin_datetime': start.strftime(date_fmt)
                   , 'end_datetime': stop.strftime(date_fmt)
                   , 'product': product_name
                   , 'dataset': dataset_name
                   }
        yield(metadata, data)
예제 #22
0
def write_geological_attitudes_ptshp(fileName, parsed_crosssect_results, sr):

    shape_driver_name = "ESRI Shapefile"
    shape_driver = ogr.GetDriverByName(shape_driver_name)
    if shape_driver is None:
        return False, "%s driver is not available" % shape_driver_name

    try:
        datasource = shape_driver.CreateDataSource(unicode(fileName))
    except TypeError:
        datasource = shape_driver.CreateDataSource(str(fileName))

    if datasource is None:
        return False, "Creation of %s shapefile failed" % os.path.split(
            fileName)[1]

    layer = datasource.CreateLayer('profile', sr, geom_type=ogr.wkbPoint25D)
    if layer is None:
        return False, "Output layer creation failed"

    # creates required fields
    layer.CreateField(ogr.FieldDefn('id', ogr.OFTString))
    layer.CreateField(ogr.FieldDefn('or_pt_x', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('or_pt_y', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('or_pt_z', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('prj_pt_x', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('prj_pt_y', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('prj_pt_z', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('s', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('or_dpdir', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('or_dpang', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('tr_dpang', ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn('tr_dpdir', ogr.OFTString))

    featureDefn = layer.GetLayerDefn()

    # loops through output records
    for rec in parsed_crosssect_results:
        pt_id, or_pt_x, or_pt_y, or_pt_z, pr_pt_x, pr_pt_y, pr_pt_z, s, or_dipdir, or_dipangle, tr_dipangle, tr_dipdir = rec

        pt_feature = ogr.Feature(featureDefn)

        pt = ogr.Geometry(ogr.wkbPoint25D)
        pt.SetPoint(0, pr_pt_x, pr_pt_y, pr_pt_z)
        pt_feature.SetGeometry(pt)

        pt_feature.SetField('id', str(pt_id))
        pt_feature.SetField('or_pt_x', or_pt_x)
        pt_feature.SetField('or_pt_y', or_pt_y)
        pt_feature.SetField('or_pt_z', or_pt_z)
        pt_feature.SetField('prj_pt_x', pr_pt_x)
        pt_feature.SetField('prj_pt_y', pr_pt_y)
        pt_feature.SetField('prj_pt_z', pr_pt_z)
        pt_feature.SetField('s', s)
        pt_feature.SetField('or_dpdir', or_dipdir)
        pt_feature.SetField('or_dpang', or_dipangle)
        pt_feature.SetField('tr_dpang', tr_dipangle)
        pt_feature.SetField('tr_dpdir', str(tr_dipdir))

        layer.CreateFeature(pt_feature)

        pt_feature.Destroy()

    datasource.Destroy()

    return True, "done"
예제 #23
0
def writeShp(outFileName):
    # 为了支持中文路径,请添加下面这句代码
    # gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO")
    # # 为了使属性表字段支持中文,请添加下面这句
    # gdal.SetConfigOption("SHAPE_ENCODING", "")
    gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
    gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
    # 1.创建输出文件
    srs = osr.SpatialReference()  # 创建空间参考
    srs.ImportFromEPSG(4490)  # 定义地理坐标系CGCS2000
    outdriver = ogr.GetDriverByName('ESRI Shapefile')
    if os.path.exists(outFileName):
        outdriver.DeleteDataSource(outFileName)
    outds = outdriver.CreateDataSource(outFileName)
    oLayer = outds.CreateLayer(outFileName, srs, geom_type=ogr.wkbPoint)
    if oLayer == None:
        print("图层创建失败!\n")
        return

    # 2.下面创建属性表
    # 2.1 创建一个叫FieldID的整型属性
    fruitId = ogr.FieldDefn("fruitId", ogr.OFTInteger)
    oLayer.CreateField(fruitId, 1)
    #  2.2 创建一个叫坐标X的浮点属性
    x_coord = ogr.FieldDefn("x_coord", ogr.OFTString)
    oLayer.CreateField(x_coord, 1)
    #  2.3 创建一个叫坐标y的浮点属性
    y_coord = ogr.FieldDefn("y_coord", ogr.OFTString)
    oLayer.CreateField(y_coord, 1)
    #  2.4 创建一个叫fruitCategoryId的整型属性
    CategoryId = ogr.FieldDefn("CategoryId", ogr.OFTInteger)
    oLayer.CreateField(CategoryId, 1)

    #  2.5 创建一个叫pointName的字符型属性
    pointName = ogr.FieldDefn("pointName", ogr.OFTString)
    #oFieldName.SetWidth(100)
    oLayer.CreateField(pointName, 1)

    #  2.6 创建一个叫pointLevel的字符型属性
    pointLevel = ogr.FieldDefn("pointLevel", ogr.OFTString)
    oLayer.CreateField(pointLevel, 1)

    #   2.7 创建一个叫geoDatum的字符型属性
    highDatum = ogr.FieldDefn("highDatum", ogr.OFTString)
    oLayer.CreateField(highDatum, 1)

    #   2.8 创建一个叫producDate的字符型属性
    producDate = ogr.FieldDefn("producDate", ogr.OFTString)
    oLayer.CreateField(producDate, 1)

    #3.写入属性
    outfielddefn = oLayer.GetLayerDefn()
    if len(locationList) == len(attributeList):
        row = 0
        while row < len(locationList):
            # 按行处理
            dic = locationList[row]
            #str = attributeList[row].get('geoDatum')
            str = attributeList[row]['highDatum']
            #print(row,str)
            if str == '1985国家高程基准':
                attribute = attributeList[row]
                for key in attribute:
                    if dic.get(key):
                        pass
                    else:
                        dic[key] = attribute[key]
                # 每一行的每一个元素
                #print(dic)
                # 创建点属性
                outfeat = ogr.Feature(outfielddefn)
                #添加字段属性
                col = 0
                for jj in dic.values():
                    outfeat.SetField(col, jj)
                    col += 1
                    # 创建要素,写入多边形
                point = ogr.Geometry(ogr.wkbPoint)
                # 构建几何类型:点
                point_x = float(dic['x'])
                point_y = float(dic['y'])
                point.AddPoint(point_x, point_y)  # 创建点

                outfeat.SetGeometry(point)
                # 写入图层
                oLayer.CreateFeature(outfeat)
                #oFeaturePint = None
            row += 1
예제 #24
0
def write_topography_singledem_ptshp(out_file_path, header_list,
                                     multiprofile_dem_data, current_dem_ndx,
                                     sr):

    shape_driver_name = "ESRI Shapefile"
    shape_driver = ogr.GetDriverByName(shape_driver_name)
    if shape_driver is None:
        return False, "%s driver is not available" % shape_driver_name

    try:
        datasource = shape_driver.CreateDataSource(unicode(out_file_path))
    except TypeError:
        datasource = shape_driver.CreateDataSource(str(out_file_path))

    if datasource is None:
        return False, "Creation of %s shapefile failed" % os.path.split(
            out_file_path)[1]

    layer = datasource.CreateLayer('profile', sr, geom_type=ogr.wkbPoint)
    if layer is None:
        return False, "Output layer creation failed"

        # creates required fields
    layer.CreateField(ogr.FieldDefn(header_list[0], ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn(header_list[1], ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn(header_list[2], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[3], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[4], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[5], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[6], ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn(header_list[7], ogr.OFTReal))

    featureDefn = layer.GetLayerDefn()

    # loops through geoprofiles and output records

    for prof_ndx, profile_data in enumerate(multiprofile_dem_data):
        for rec in profile_data:
            rec_id, x, y, cumdist2D = rec[0], rec[1], rec[2], rec[3]
            z = rec[3 + current_dem_ndx * 3 + 1]
            cumdist3D = rec[3 + current_dem_ndx * 3 + 2]
            slopedegr = rec[3 + current_dem_ndx * 3 + 3]

            if z == "":
                continue

            pt_feature = ogr.Feature(featureDefn)

            pt = ogr.Geometry(ogr.wkbPoint25D)
            pt.SetPoint(0, x, y, z)
            pt_feature.SetGeometry(pt)

            pt_feature.SetField(header_list[0], prof_ndx)
            pt_feature.SetField(header_list[1], rec_id)
            pt_feature.SetField(header_list[2], x)
            pt_feature.SetField(header_list[3], y)
            pt_feature.SetField(header_list[4], cumdist2D)
            pt_feature.SetField(header_list[5], z)
            if cumdist3D != '':
                pt_feature.SetField(header_list[6], cumdist3D)
            if slopedegr != '':
                pt_feature.SetField(header_list[7], slopedegr)

            layer.CreateFeature(pt_feature)

            pt_feature.Destroy()

    datasource.Destroy()

    return True, "done"
        shp_layer = datasource.GetLayerByIndex(layer)
        #get spatial reference of input shapefile
        srs = shp_layer.GetSpatialRef()

        #create convex hull layer
        convex_hull_layer = convex_hull_datasource.CreateLayer(
            layer_name, srs, ogr.wkbPolygon)

        #get number of features of input shapefile
        shp_feature_count = shp_layer.GetFeatureCount()

        #define convex hull feature
        convex_hull_feature = ogr.Feature(convex_hull_layer.GetLayerDefn())

        #define multipoint geometry to store all points
        multipoint = ogr.Geometry(ogr.wkbMultiPoint)

        for each_feature in range(shp_feature_count):
            #get feature from input shapefile
            shp_feature = shp_layer.GetFeature(each_feature)
            #get input feature geometry
            feature_geom = shp_feature.GetGeometryRef()
            #if geometry is MULTIPOLYGON then need to get
            #   POLYGON then LINEARRING to be able to get points
            if feature_geom.GetGeometryName() == 'MULTIPOLYGON':
                for polygon in feature_geom:
                    for linearring in polygon:
                        points = linearring.GetPoints()
                        for point in points:
                            point_geom = ogr.Geometry(ogr.wkbPoint)
                            point_geom.AddPoint(point[0], point[1])
예제 #26
0
    def export_profiles_metadata(self, project_name, output_folder, ogr_format=GdalAux.ogr_formats['ESRI Shapefile'],
                                 filter_fields=None):
        self.filter_fields = filter_fields
        if self.filter_fields is None:
            self.filter_fields = ExportDbFields()

        GdalAux()
        output = os.path.join(self.export_folder(output_folder=output_folder), project_name)

        # create the data source
        try:
            ds = GdalAux.create_ogr_data_source(ogr_format=ogr_format, output_path=output)
            lyr = self._create_ogr_lyr_and_fields(ds)

        except RuntimeError as e:
            logger.error("%s" % e)
            return

        rows = self.db.list_profiles()
        if rows is None:
            raise RuntimeError("Unable to retrieve profiles. Empty database?")
        if len(rows) == 0:
            raise RuntimeError("Unable to retrieve profiles. Empty database?")

        for row in rows:

            ft = ogr.Feature(lyr.GetLayerDefn())

            if self.filter_fields.fields['pk']:
                ft.SetField('pk', int(row[0]))

            if self.filter_fields.fields['datetime']:
                ft.SetField('datetime', row[1].isoformat())

            if self.filter_fields.fields['sensor']:
                ft.SetField('sensor', Dicts.first_match(Dicts.sensor_types, row[3]))

            if self.filter_fields.fields['probe']:
                ft.SetField('probe', Dicts.first_match(Dicts.probe_types, row[4]))

            if self.filter_fields.fields['path']:
                ft.SetField('path', row[5])

            if self.filter_fields.fields['agency']:
                if row[6]:
                    ft.SetField('agency', row[6])

            if self.filter_fields.fields['survey']:
                if row[7]:
                    ft.SetField('survey', row[7])

            if self.filter_fields.fields['vessel']:
                if row[8]:
                    ft.SetField('vessel', row[8])

            if self.filter_fields.fields['sn']:
                if row[9]:
                    ft.SetField('sn', row[9])

            if self.filter_fields.fields['proc_time']:
                ft.SetField('proc_time', row[10].isoformat())

            if self.filter_fields.fields['proc_info']:
                ft.SetField('proc_info', row[11])

            if self.filter_fields.fields['surveyline']:
                if row[12]:
                    ft.SetField('surveyline', row[12])

            if self.filter_fields.fields['comments']:
                if row[13]:
                    ft.SetField('comments', row[13])

            if self.filter_fields.fields['press_uom']:
                if row[14]:
                    ft.SetField('press_uom', row[14])

            if self.filter_fields.fields['depth_uom']:
                if row[15]:
                    ft.SetField('depth_uom', row[15])

            if self.filter_fields.fields['ss_uom']:
                if row[16]:
                    ft.SetField('ss_uom', row[16])

            if self.filter_fields.fields['temp_uom']:
                if row[17]:
                    ft.SetField('temp_uom', row[17])

            if self.filter_fields.fields['cond_uom']:
                if row[18]:
                    ft.SetField('cond_uom', row[18])

            if self.filter_fields.fields['sal_uom']:
                if row[19]:
                    ft.SetField('sal_uom', row[19])

            if self.filter_fields.fields['ss_at_mind']:
                if row[20]:
                    ft.SetField('ss_at_mind', row[20])

            if self.filter_fields.fields['min_depth']:
                if row[21]:
                    ft.SetField('min_depth', row[21])

            if self.filter_fields.fields['max_depth']:
                if row[22]:
                    ft.SetField('max_depth', row[22])

            if self.filter_fields.fields['max_raw_d']:
                if row[23]:
                    ft.SetField('max_raw_d', row[23])

            pt = ogr.Geometry(ogr.wkbPoint)
            lat = row[2].y
            lon = row[2].x
            if lon > 180.0:  # Go back to negative longitude
                lon -= 360.0
            pt.SetPoint_2D(0, lon, lat)

            if self.filter_fields.fields['POINT_X']:
                ft.SetField('POINT_X', lon)

            if self.filter_fields.fields['POINT_Y']:
                ft.SetField('POINT_Y', lat)

            try:
                ft.SetGeometry(pt)

            except Exception as e:
                RuntimeError("%s > pt: %s, %s" % (e, lon, lat))

            if lyr.CreateFeature(ft) != 0:
                raise RuntimeError("Unable to create feature")
            ft.Destroy()

        ds = None
        return True
layer.CreateField(PercBleachDefn)
CompassDefn = ogr.FieldDefn('Compass', ogr.OFTReal)
CompassDefn.SetWidth(5)
CompassDefn.SetPrecision(0)
layer.CreateField(CompassDefn)

featureIndex = 0

for i, thisLine in df.iterrows():

    startlat = thisLine['YStart']
    startlon = thisLine['XStart']
    endlat = thisLine['YStop']
    endlon = thisLine['XStop']

    line = ogr.Geometry(ogr.wkbLineString)

    line.AddPoint_2D(startlon, startlat)
    line.AddPoint_2D(endlon, endlat)

    ##now lets write this into our layer/shape file:
    feature = ogr.Feature(layer_defn)
    feature.SetGeometry(line)
    feature.SetFID(featureIndex)
    feature.SetField('Site', str(thisLine[0]))
    feature.SetField('Date', str(thisLine[1]))
    feature.SetField('Transect', int(thisLine[2]))
    feature.SetField('Depth', float(thisLine[3]))
    feature.SetField('LiveCoral', float(thisLine[4]))
    feature.SetField('DeadAlgal', float(thisLine[5]))
    feature.SetField('Sand', float(thisLine[6]))
    def checkgeometry(self, aoi):
        if self.ds == None:
            return aoi
        layerGeometry = self._getLayerGeometry()
        gt = self.ds.GetLayer().GetGeomType()
        if layerGeometry:
            if not ((layerGeometry == "P" and gt == 1) or
                    (layerGeometry == "L" and gt == 2) or
                    (layerGeometry == "A" and gt == 3)):
                #check fature by feature
                self.__checkGeometryByFeature(layerGeometry)

            #check if the layer is within the AOI
            numDigitsForAoi = 6
            extent = self.ds.GetLayer().GetExtent()
            # Create a Polygon from the extent of the layer
            ring = ogr.Geometry(ogr.wkbLinearRing)
            ring.AddPoint(round(extent[0], numDigitsForAoi),
                          round(extent[2], numDigitsForAoi))
            ring.AddPoint(round(extent[1], numDigitsForAoi),
                          round(extent[2], numDigitsForAoi))
            ring.AddPoint(round(extent[1], numDigitsForAoi),
                          round(extent[3], numDigitsForAoi))
            ring.AddPoint(round(extent[0], numDigitsForAoi),
                          round(extent[3], numDigitsForAoi))
            ring.AddPoint(round(extent[0], numDigitsForAoi),
                          round(extent[2], numDigitsForAoi))
            poly = ogr.Geometry(ogr.wkbPolygon)
            poly.AddGeometry(ring)

            if 'areaofinterest' in self.layer.lower():
                #get the geometry of the area of interest if none has been defined
                if aoi == None:
                    return poly
                else:
                    #compare if all AOIs are the same
                    if not poly.Equal(aoi):

                        err = self.logs_text["geometryName"][
                            "not_equal_AOI"].copy()
                        initial_err = self.activ_cod + "|" + self.splitroot(
                            self.root, self.activ_cod
                        ) + "|" + self.layer + "|" + self.logFile.getCatValue(
                            self.conf_param['VectorFormats'][
                                self.type]['not_equal_AOI']
                        ) + "|" + self.logFile.getIssueValue(
                            self.conf_param['VectorFormats'][
                                self.type]['not_equal_AOI']) + "|"
                        err.insert(0, initial_err)
                        self.logFile.writelogs(err,
                                               self.conf_param["root_path"])

            else:
                #check if the current layer´s extent in within AOI of the product
                if aoi != None:
                    if not poly.Within(aoi):
                        err = self.logs_text["geometryName"][
                            "not_in_AOI"].copy()
                        initial_err = self.activ_cod + "|" + self.splitroot(
                            self.root, self.activ_cod
                        ) + "|" + self.layer + "|" + self.logFile.getCatValue(
                            self.conf_param['VectorFormats'][self.type]
                            ['not_in_AOI']) + "|" + self.logFile.getIssueValue(
                                self.conf_param['VectorFormats'][
                                    self.type]['not_in_AOI']) + "|"
                        err.insert(0, initial_err)
                        self.logFile.writelogs(err,
                                               self.conf_param["root_path"])

        else:
            err = self.logs_text["geometryName"]["wrong_name"].copy()
            err.insert(0, self.root + "\\" + self.file)
            self.logFile.writelogs(err, self.conf_param["root_path"])
        return aoi
예제 #29
0
def reprojectXY(_x, _y, frmEPSG=26917, toEPSG=4326):
    # create a geometry from coordinates
    point = ogr.Geometry(ogr.wkbPoint)
    point.AddPoint(_x, _y)
    return reprojectPoint(point, frmEPSG, toEPSG)
예제 #30
0
def Write_Dict_To_Shapefile_osgeo(totalList, shapefileName, EPSG):
    '''
    Adapted from
    https://github.com/GeoscienceAustralia/LidarProcessingScripts/RasterIndexTool_GDAL.py
    This function takes a list of dictionaries where each row in the list
    is a feature consisting of the X/Y geometries in the dictionary for each
    item in the list.

    Arguments:
    totalList     -- List of dictionaries
    shapefileName -- Name of the shapefile to be created/overwritten


    '''
    gdal.UseExceptions()

    shapePath = os.path.join(workspace, shapefileName)

    # Get driver
    driver = ogr.GetDriverByName('ESRI Shapefile')

    # Create shapeData, overwrite the data if it exists
    os.chdir(workspace)
    if os.path.exists(shapefileName):
        print('Shapefile exists and will be deleted')
        driver.DeleteDataSource(shapePath)
        assert not os.path.exists(shapePath)

    shapeData = driver.CreateDataSource(shapefileName)

    # Create spatialReference for output
    outputspatialRef = osr.SpatialReference()

    # Set coordinate reference system
    outputspatialRef.ImportFromEPSG(EPSG)

    # Create layer
    layer = shapeData.CreateLayer(shapePath,
                                  srs=outputspatialRef,
                                  geom_type=ogr.wkbLineString)

    # add fields
    fieldNames = ["Date", "Time"]
    for n in range(0, len(fieldNames)):

        # add short text fields - convoluted method but was more extensive
        # in Jonah's script to capture more fields and various field types.
        if fieldNames[n] in ["Date", "Time"]:
            fieldstring = str(fieldNames[n])
            field_name = ogr.FieldDefn(fieldstring, ogr.OFTString)
            field_name.SetWidth(24)
            layer.CreateField(field_name)

    # Create polyline object
    line = ogr.Geometry(ogr.wkbLineString)
    count = 0
    for entry in totalList:
        logAndprint('Row {0} being processed'.format(count))
        dictCounter = 0
        #        while dictCounter < 10:
        logAndprint('Dictionary count {0}'.format(len(totalList[count])))
        for key, value in totalList[count].iteritems():
            logAndprint('\n{0} row, {1} vertex being created'.format(
                count, dictCounter))
            logAndprint('\tKey: {0}, value: {1}'.format(key, value))
            #        print(count)
            if dictCounter < 2:
                logAndprint('\tRow passed')
                pass
                dictCounter += 1

            else:
                line.AddPoint(float(key), float(value))
                logAndprint('\t\t{0} and {1} vertex added to line'.format(
                    key, value))
                dictCounter += 1


#        poly = ogr.Geometry(ogr.wkbLineString)
#        logAndprint('Adding geometry')
#        poly.AddGeometry(line)

# Create feature
        layerDefinition = layer.GetLayerDefn()
        feature = ogr.Feature(layerDefinition)
        feature.SetGeometry(line)
        # Set the FID field to the count
        feature.SetFID(count)

        # Calculate fields
        logAndprint('Calculating "Date" & "Time" fields')

        # Date is supplied in YYYY/MM/DD
        date = str(totalList[count].keys()[1].split()[0])
        # For animation in ArcGIS the date needs to be in the form
        # DD/MM/YYYY
        reformattedDate = (date.split('/')[2] + '/' + date.split('/')[1] +
                           '/' + date.split('/')[0])
        logAndprint('reformatted date:' + reformattedDate)
        feature.SetField('Date', str(reformattedDate))
        feature.SetField('Time', str(totalList[count].keys()[1].split()[1]))

        # Save feature
        layer.CreateFeature(feature)

        logAndprint('{0} rows processed'.format(count))
        count += 1

        # Empty the ring otherwise the vertices are accumulatied
        line.Empty()

    # Cleanup
    #poly.Destroy()
    feature.Destroy()

    # Cleanup
    shapeData.Destroy()
    # Return
    return shapePath