Exemple #1
0
def project(p1, p2, p3):
    """Project a Point, p3 onto a line between Points p1 and p2.

    Uses Shapely and GEOS functions, which set distance to zero for all negative distances.

    Parameters:
        p1 (Point) : point at zero distance on line between p1 and p2.
        p2 (Point) : endpoint of line.
        p3 (Point) : the point to project.

    Returns:
        result (dict) : the projected Point, disctance along line, offset from line, and fractional distance along line.

    """
    line = LineString([(p1.x, p1.y),(p2.x, p2.y)])
    u = line.project(p3, normalized=True)
    d = line.project(p3, normalized=False)
    pt_xy = line.interpolate(d)
    pt = Point([pt_xy.x, pt_xy.y, p3.z])

    # calculate the offset distance of p3 from the line
    if (p1.y - p2.y) * (p3.x - p2.x) - (p1.x - p2.x) * (p3.y - p2.y) < 0:
        offset = -pt.distance(p3) # the point is offset left of the line
    else:
        offset = pt.distance(p3) # the point is offset right of the line

    result = {'pt':pt, 'd':d, 'o':offset, 'u':u}
    return result
def pymol_select_memb_old(pdb: MyPDB) -> set():
    """
    print a pymol selection line for all residues that are in the membrane
    !!! this assumes that cntr is at 0 0 0 and norm at 15 0 0 !!!
    """
    from shapely.geometry import LineString, Point
    # create Points from center & thickness
    cntr_pnt = Point(pdb.memb_res.cntr.x, pdb.memb_res.cntr.y, pdb.memb_res.cntr.z)
    thkn_m_pnt = Point(-pdb.memb_res.thkn.x, pdb.memb_res.thkn.y, pdb.memb_res.thkn.z)
    thkn_pnt = Point(pdb.memb_res.thkn.x, pdb.memb_res.thkn.y, pdb.memb_res.thkn.z)

    # define the line between center and thickness
    line = LineString([thkn_m_pnt, thkn_pnt])
    thickness = cntr_pnt.distance(thkn_pnt)

    result = set()
    # iterate over all CAs in the pdb
    for cid in sorted(pdb.chains.keys()):
        for rid in sorted(pdb[cid].residues.keys()):
            atom = pdb[cid][rid]['CA']

            # the atom as a Point
            p = Point(atom.xyz.x, atom.xyz.y, atom.xyz.z)

            # projection of the CA atom on the center-thickness line
            np = line.interpolate(line.project(p))

            # if the distance on the center-thickness line is smaller than 15, than this is in the membrane
            if cntr_pnt.distance(np) < thickness-0.1:
                result.add(atom.res_seq_num)

    return result
Exemple #3
0
 def GetIntersectionDistance(self, l1, l2):
     # l1 should be just two coordinate positions
     # get its starting coorinate
     pt1 = Point(l1.coords[0])
     x = l1.intersection(l2)
     # intersections can return a lot of things
     d = -1
     if x.wkt == 'GEOMETRYCOLLECTION EMPTY':
         d = -1
         # print "nothing"
     elif re.match('^POINT', x.wkt): 
         # print "point"
         pt2 = Point(x.coords[0])
         d = pt1.distance(pt2)
     elif re.match('^MULTI', x.wkt): 
         # print "mpoint"
         # this will return the minimum distance
         pt2 = Point(x[0].coords[0])
         d = pt1.distance(pt2) 
         for pt2 in x:
             pt2 = Point(pt2)
             if d < pt1.distance(pt2):
                 d = pt1.distance(pt2)
     else:
         print 'dunno what intersection passed me'
     return d
def getNosePoints(line,prevPoint):
        start=Point(line.coords[0])
        end=Point(line.coords[-1])
        if start.distance(prevPoint)<end.distance(prevPoint):
            return start
        else:
            return end
Exemple #5
0
    def compute_repulsive_ws(self, control_point, obstacle):
        point, _, eta = control_point
        # need to unpack obstacle tuple into polygon and threshold
        obstacle_poly = obstacle[0]
        obstacle_thresh = obstacle[1]

        d2obstacle = point.distance(obstacle_poly)
        # this is straight from book
        if d2obstacle > obstacle_thresh:
            return Point(0, 0)
        else:
            # scalar is length of vector that points away from closest obstacle
            # point
            scalar = eta * ((obstacle_thresh ** -1) - (d2obstacle ** -1)) * (d2obstacle ** -2)
            # construct gradient vector

            # find closest point, you can ignore the details Yinan
            pol_ext = obstacle_poly
            if obstacle_poly.geom_type != "LineString":
                pol_ext = LinearRing(obstacle_poly.exterior.coords)
            d = pol_ext.project(point)
            p = pol_ext.interpolate(d)
            # closest point
            c = Point(list(p.coords)[0])
            dqc = c.distance(point)
            # from book, formula for delta vector
            delta_d_i = Point(((point.x - c.x) / dqc, (point.y - c.y) / dqc))

            return Point((-1 * delta_d_i.x * scalar, -1 * delta_d_i.y * scalar))
    def find_edge_nodes(fargs):
        """ Find nodes are near the edge of the hull"""
        cluster, cluster_hull, nodes = fargs
        # There is no hull for this community, it's been deleted.
        if cluster_hull is None:
            log.error("Missing hull, keeping all nodes in cluster %i",
                      cluster)
            return len(nodes), nodes

        characteristic_size = math.sqrt(cluster_hull.area)
        allowed_distance = characteristic_size * args.within
        boundary = cluster_hull.boundary

        output = []
        for node in nodes:
            # check if it is an interior node
            point = Point((node.lon, node.lat))
            keep = False
            if random.random() < args.keep:
                keep = True
            elif point.distance(boundary) < allowed_distance:
                keep = True
            if keep:
                output.append(node)
        return len(nodes), output
def fuse_point_to_polygon(point, polygon):
    """

    :param point:
    :param polygon:
    """
    p = Point(point)
    min_distance = float("inf")
    min_index = -1
    polygon = polygon[::-1]  ##FIXME the received polygon is wrong. This is POG
    # Identify the nearest line segment.
    for i in range(len(polygon)):
        seg = polygon[i:i + 2]
        if len(seg) < 2:
            # close the polygon
            seg = [polygon[-1]] + [polygon[0]]

        line_segment = LineString(seg)
        dist = p.distance(line_segment)
        # print seg, dist
        if dist < min_distance:
            min_distance = dist
            min_index = i

    # print min_distance, min_index
    fused_polygon = polygon[min_index + 1:] + polygon[:min_index + 1] + [point]
    # fused_polygon = polygon[min_index + 1:] +  [point]
    # aa = identify_first_point_in_polygon(fused_polygon)
    # print "FUSING", aa

    # print point, polygon, fused_polygon
    return fused_polygon
def project_tracks_to_road(tracks, 
                           road_segments):
    """
        Compute tracks that fall into each road segment.
            Args:
                - tracks
                - road_segments
            Return:
                - track_on_road: a dictionary, each key is the index of a road segment. Each 
                                 value is a set of indices of the tracks fall onto this road.
    """
    track_on_road = {}
    for seg_idx in np.arange(len(road_segments)):
        track_on_road[seg_idx] = set([])

    simplified_tracks = []
    for track in tracks:
        line = LineString([(pt[0], pt[1]) for pt in track.utm])
        simplified_track = line.simplify(10.0)
        simplified_tracks.append(simplified_track)
   
    # Compute road segment linestrings
    road_segment_linestrings = []
    for r_seg in road_segments:
        r_start = r_seg.center - r_seg.half_length*r_seg.direction
        r_end = r_seg.center + r_seg.half_length*r_seg.direction
        r_linestring = LineString([r_start, r_end])
        road_segment_linestrings.append(r_linestring)

    for seg_idx in np.arange(len(road_segments)):
        print seg_idx
        for track_idx in np.arange(len(tracks)):
            if road_segment_linestrings[seg_idx].distance(simplified_tracks[track_idx]) > 1.2*road_segments[seg_idx].half_width:
                continue
            track = tracks[track_idx]
            if len(track.utm) <= 1:
                continue
            for utm_idx in np.arange(len(track.utm)):
                utm = track.utm[utm_idx]
                if utm_idx == 0:
                    direction = np.array([track.utm[utm_idx+1][0], track.utm[utm_idx+1][1]]) - \
                                np.array([track.utm[utm_idx][0], track.utm[utm_idx][1]])
                elif utm_idx == len(track.utm) - 1:
                    direction = np.array([track.utm[utm_idx][0], track.utm[utm_idx][1]]) - \
                                np.array([track.utm[utm_idx-1][0], track.utm[utm_idx-1][1]])
                else:
                    direction = np.array([track.utm[utm_idx+1][0], track.utm[utm_idx+1][1]]) - \
                                np.array([track.utm[utm_idx-1][0], track.utm[utm_idx-1][1]])

                direction /= np.linalg.norm(direction)
                if np.dot(direction, road_segments[seg_idx].direction) < np.cos(np.pi/4.0):
                    continue

                pt = Point(utm[0], utm[1])
                if pt.distance(road_segment_linestrings[seg_idx]) < 1.2*road_segments[seg_idx].half_width:
                    track_on_road[seg_idx].add(track_idx)
                    break

    return track_on_road
Exemple #9
0
    def test_operations(self):
        point = Point(0.0, 0.0)

        # General geometry
        self.assertEqual(point.area, 0.0)
        self.assertEqual(point.length, 0.0)
        self.assertAlmostEqual(point.distance(Point(-1.0, -1.0)),
                               1.4142135623730951)

        # Topology operations

        # Envelope
        self.assertIsInstance(point.envelope, Point)

        # Intersection
        self.assertIsInstance(point.intersection(Point(-1, -1)),
                              GeometryCollection)

        # Buffer
        self.assertIsInstance(point.buffer(10.0), Polygon)
        self.assertIsInstance(point.buffer(10.0, 32), Polygon)

        # Simplify
        p = loads('POLYGON ((120 120, 121 121, 122 122, 220 120, 180 199, '
                  '160 200, 140 199, 120 120))')
        expected = loads('POLYGON ((120 120, 140 199, 160 200, 180 199, '
                         '220 120, 120 120))')
        s = p.simplify(10.0, preserve_topology=False)
        self.assertTrue(s.equals_exact(expected, 0.001))

        p = loads('POLYGON ((80 200, 240 200, 240 60, 80 60, 80 200),'
                  '(120 120, 220 120, 180 199, 160 200, 140 199, 120 120))')
        expected = loads(
            'POLYGON ((80 200, 240 200, 240 60, 80 60, 80 200),'
            '(120 120, 220 120, 180 199, 160 200, 140 199, 120 120))')
        s = p.simplify(10.0, preserve_topology=True)
        self.assertTrue(s.equals_exact(expected, 0.001))

        # Convex Hull
        self.assertIsInstance(point.convex_hull, Point)

        # Differences
        self.assertIsInstance(point.difference(Point(-1, 1)), Point)

        self.assertIsInstance(point.symmetric_difference(Point(-1, 1)),
                              MultiPoint)

        # Boundary
        self.assertIsInstance(point.boundary, GeometryCollection)

        # Union
        self.assertIsInstance(point.union(Point(-1, 1)), MultiPoint)

        self.assertIsInstance(point.representative_point(), Point)

        self.assertIsInstance(point.centroid, Point)

        # Relate
        self.assertEqual(point.relate(Point(-1, -1)), 'FF0FFF0F2')
Exemple #10
0
def getclosestcity(lon,lat):
	qp = Point(lon,lat)
	cities = meta.Session.query(City).filter(cities_table.c.lon!=None).filter(cities_table.c.lat!=None).all()
	if not len(cities): return None
	distances = sorted([(qp.distance(Point(ct.lon,ct.lat)),ct.name) for ct in cities])
	#distances_num = sorted([(qp.distance(Point(ct.lon,ct.lat)),ct.id) for ct in cities]) ; raise Exception('distance from (%s,%s) - %s'%(lon,lat,distances_num))
	rt = distances[0][1]
	return rt
Exemple #11
0
def test_point_point():
    p1 = Point(0,0)
    p2 = Point(1,1)

    ok_(p1.geom_type=='Point', p1.geom_type)
    assert_ae(p1.coords[:][0], (0,0))

    d = p1.distance(p2)
    assert_eq(d, sqrt(2))
Exemple #12
0
def cartesiandistance(origin, destination):
    lon1, lat1 = origin
    lon2, lat2 = destination
    proj = Proj(init="epsg:3785") # spherical mercator, should work anywhere
    point1 = proj(lon1, lat1)
    point2 = proj(lon2, lat2)
    point1_cart = Point(point1)
    point2_cart = Point(point2)
    return point1_cart.distance(point2_cart) #meters
Exemple #13
0
def project2(p1, p2, p3):
    """Project a Point, p3 onto a line intersecting Points p1 and p2.

    Adapted from tutorial by Paul Bourke: http://paulbourke.net/geometry/pointline/
    This projection function allows for points at negative distances.

    Parameters:
        p1 (Point) : point at zero distance on line between p1 and p2.
        p2 (Point) : endpoint on line.
        p3 (Point) : the point to project.

    Returns:
        result (dict) : the projected Point, distance along line, offset from line, and fractional distance along line.

    """
    x_delta = p2.x - p1.x
    y_delta = p2.y - p1.y

    if x_delta == 0 and y_delta == 0:
        logger.warning("p1 and p2 cannot be the same point")
        return

    u = ((p3.x - p1.x) * x_delta + (p3.y - p1.y) * y_delta) / (x_delta * x_delta + y_delta * y_delta)
    pt = Point(p1.x + u * x_delta, p1.y + u * y_delta, p3.z)

    # calculate distance along the line from p1
    if u < 0:
        d = -pt.distance(p1)
    else:
        d = pt.distance(p1)

    # calculate the offset distance of p3 from the line
    if (p1.y - p2.y) * (p3.x -  p2.x) - (p1.x - p2.x) * (p3.y - p2.y) < 0:
        offset = -pt.distance(p3) # the point is offset left of the line
    else:
        offset = pt.distance(p3) # the point is offset right of the line

    result = {'pt':pt, 'd':d, 'o':offset, 'u':u}
    return result
def distance_point_to_polygon(point, poly):
    """
    Compute the minimum distance from a point to a polygon
    :param point:
    :param poly:
    :return: the distance
    """
    p = Point(point)
    pol = Polygon(poly)
    if not pol.is_valid:
        pol = MultiPoint(poly).convex_hull

    return p.distance(pol)
Exemple #15
0
 def score(ob):
     g = geom(ob)
     r = region
     if r is None:
         if len(coords) == 2:
             r = Point(coords)
         else:
             r = asShape(box2poly(coords))
     # local scaling might be better, degree units are implicit
     # in this value
     k = 0.2
     d = r.distance(g)
     return int(math.exp(-d/k) * 1000)
Exemple #16
0
    def process_exterior_segment(startInd, endInd, airportInd):
        
        print("Processing exterior segment...")

        airport = airports[airportInd]
        startPt = Point(lineX[startInd], lineY[startInd])

        # The probability the the crash happens in this segment
        segCrashProb = abs(endInd - startInd) / float(nLinePts)
        
        # The line that is just this segment
        segLine = LineString([(lineX[startInd], lineY[startInd]), \
                              (lineX[endInd-1]  , lineY[endInd-1]  )])
   

        # Reflected point, used for clipping end
        dX = lineX[endInd-1] - lineX[startInd]
        dY = lineY[endInd-1] - lineY[startInd]
        refPt = Point(lineX[endInd-1] + dX, lineY[endInd-1] + dY)

        for i in range(dim.x_res):
            for j in range(dim.y_res):
                
                # Coordinates of this point
                pX = p_y[i]
                pY = p_x[j]
                pt = Point(pX, pY)

                # Skip points which are off the end of this line
                if(pt.distance(refPt) < pt.distance(startPt)):
                    continue

                dist = pt.distance(segLine)

                prob = segCrashProb * (dAlongLine / segLine.length) * \
                    (distribSmall.cdf(dist + 0.5 * dPerpLine) - distribSmall.cdf(dist - 0.5 * dPerpLine))

                probs[i,j] += prob
    def is_on_surface(self, moveable_obj, edge_index):
        edge = self.vertices_of_edges[edge_index]

        center_x, center_y = moveable_obj.position
        gravity_vec = Line([moveable_obj.position, (center_x, 0)])
        edge_line = Line(edge).buffer(1)

        radius = moveable_obj.fixtures[0].shape.radius
        center = Point(center_x, center_y)
        circle = center.buffer(radius)
        if circle.intersects(edge_line):
            if self.is_edge_surface[edge_index] == 1 and gravity_vec.intersects(edge_line):
                return True, 0
        return False, center.distance(edge_line)   
def find_nearest(obs_lat,obs_lon, lats,lons):    
    point1 = Point(obs_lon,obs_lat)
    dist = 999999999
    index_i = -1
    index_j = -1    
    for i in range(0,len(lats)):
        for j in range(0,len(lons)):
            point2 = Point(lons[j], lats[i])
            val = point1.distance(point2)
            if val < dist:
                index_i = i
                index_j = j
                dist = val    
    return [index_i,index_j,dist]
Exemple #19
0
def getJSONs(route,direction):
  trip_ids = amtrak.trips[(amtrak.trips['route_id'].astype(str) == str(route).replace('%20', ' ')) & (amtrak.trips['direction_id'] == int(direction))]['trip_id']
  stops = pandas.merge(pandas.DataFrame(trip_ids),amtrak.stop_times)
#  trip_ids = amtrak.trips.loc[str(route).replace('%20', ' ')]['trip_id']
#  stops = pandas.merge(pandas.DataFrame(trip_ids),amtrak.stop_times.loc[str(direction).replace('%20',' ')])
  trains = stops.groupby('trip_id')
  stop_ids = []
  for index, train in trains:
    train = train.sort('stop_sequence')
    stop_ids = combine(stop_ids,list(train['stop_id']))
  previousStop = False
  distances = []
  stopLocations = []
  lineStrings = []
  for stop in stop_ids:
    if previousStop:
      currentStop = Point(float(amtrak.stops.loc[stop]['stop_lon']),float(amtrak.stops.loc[stop]['stop_lat']))
      distanceToPreviousStop = 65.93*currentStop.distance(previousStop)
      distances.append(distanceToPreviousStop)
      lineStrings.append(LineString([list(currentStop.coords)[0],list(previousStop.coords)[0]]))
    previousStop = Point(float(amtrak.stops.loc[stop]['stop_lon']),float(amtrak.stops.loc[stop]['stop_lat']))
    stopLocations.append(previousStop)
  times = []
  for index, train in trains:
    time = []
    departureTime = False
    for stop in stop_ids:
      if stop in list(train['stop_id']):
	if departureTime:
	  h, m, s = map(int,list(train[train['stop_id'] == stop]['arrival_time'])[0].split(':'))
	  arrivalTime = timedelta(hours=h, minutes=m, seconds=s)
	  delta = arrivalTime - departureTime
	  time.append(delta.total_seconds()/60/60)
	h, m, s = map(int,list(train[train['stop_id'] == stop]['departure_time'])[0].split(':'))
	departureTime = timedelta(hours=h, minutes=m, seconds=s)
      else:
	time.append(0)
    times.append(time)
  stopsGDF = geopandas.GeoDataFrame(stopLocations,stop_ids)
  stopsGDF.columns = ['geometry']
  distancesS = pandas.Series(distances)
  timesDF = pandas.DataFrame(times)
  timesDF = timesDF.replace(0.0, numpy.nan)
#  speedS = distancesS / timesDF.max()
#  speedS = distancesS / timesDF.min()
  speedS = distancesS / timesDF.mean()
  speedS = speedS.round(2)
  speedGDF = geopandas.GeoDataFrame(lineStrings,speedS)
  speedGDF.columns = ['geometry']
  return [stopsGDF.to_json(), speedGDF.to_json()]
def graph_from_voronoi(vor, geometry):
    """
    Creates a networkx graph out of all Voronoi ridge vertices which are inside
    the original geometry.
    """
    graph = nx.Graph()
    for i in vor.ridge_vertices:
        if i[0]>-1 and i[1]>-1:
            point1 = Point(vor.vertices[i][0])
            point2 = Point(vor.vertices[i][1])
            # Eliminate all points outside our geometry.
            if point1.within(geometry) and point2.within(geometry):
                dist = point1.distance(point2)
                graph.add_nodes_from([i[0], i[1]])
                graph.add_edge(i[0], i[1], weight=dist)
    return graph
Exemple #21
0
def findSFOIndexs(lats,lons,lat_lon_list):   
    index_list = []
    dist_list = []
    for val in lat_lon_list:
        point1 = Point(val[1],val[0])
        dist = 999999999
        index = -1  
        for i in range(0,len(lats)):       
            point2 = Point(lons[i], lats[i])
            val = point1.distance(point2)
            if val < dist:
                index = i
                dist = val   
        index_list.append(index)
        dist_list.append(dist)
    return index_list,dist_list
Exemple #22
0
def closestCellFromPt(lon, lat, lonArray, latArray, obsArray, obsFillValue, landMaskArray):
  latlon = Point(lon,lat)
  lastDistance = None
  n,m = np.shape(lonArray)
  cellPoint = None
  for i in range(0,n):
    for j in range(0,m):
      #We have to be in the water, and the cell has to have a valid observation values.
      if(landMaskArray[i,j] == 1 and obsArray[i,j] != obsFillValue):
        gridPt = Point(lonArray[i,j],latArray[i,j])
        curDist = latlon.distance(gridPt)      
        if(lastDistance == None or curDist < lastDistance):
          lastDistance = curDist
          cellPoint = Point(i,j)
      
  return(cellPoint)  
Exemple #23
0
    def nodes_for_point(self, point, all_nodes):
        point = Point(point.x, point.y)

        nodes = {}
        if self.nodes:
            for node in self.nodes:
                node = all_nodes[node]
                line = LineString([(node.x, node.y), (point.x, point.y)])
                if line.length < 10 and not self.clear_geometry_prep.intersects(line):
                    nodes[node.i] = (None, None)
            if not nodes:
                nearest_node = min(tuple(all_nodes[node] for node in self.nodes),
                                   key=lambda node: point.distance(node.point))
                nodes[nearest_node.i] = (None, None)
        else:
            nodes = self.fallback_nodes
        return nodes
Exemple #24
0
def match(societies, regions):
    for society in societies:
        point = Point(float(society['Long']), float(society['Lat']))

        mindist, nearestregion = None, None
        for region in regions:
            polygon = shape(region['geometry'])
            if polygon.contains(point):
                yield society, region
                break
            dist = point.distance(polygon)
            if mindist is None or mindist > dist:
                mindist, nearestregion = dist, region
        else:
            assert mindist is not None
            print society['soc_id'], nearestregion['properties']['REGION_NAM'], mindist
            yield society, nearestregion
def _enumerate_close(lon, lat, shapes, th=None):
    """
    Enumerate close streets from a starting point.

    @param      lon     longitude
    @param      lat     latitude
    @param      shapes  list of streets
    @param      th      threshold or None for all
    @return             iterator on *tuple(distance, i)*
    """
    from shapely.geometry import Point, LineString
    p = Point(lon, lat)
    for i, shape in enumerate(shapes):
        obj = LineString(shape.points)
        d = p.distance(obj)
        if th is None or d <= th:
            yield d, i
Exemple #26
0
def findSFOIndexs(lats, lons, lat_lon_list):
    index_list, dist_list = [], []
    for val in lat_lon_list:
        point1 = Point(val[1], val[0])
        dist = 999999999
        index = -1
        for k in range(0, len(lats)):
            point2 = Point(lons[k], lats[k])
            val = point1.distance(point2)
            if val < dist:
                index = k
                dist = val
        index_list.append(index)
        dist_list.append(dist)
    sorted_list = sorted(zip(index_list, dist_list))
    index_list = [x[0] for x in sorted_list]
    dist_list = [x[1] for x in sorted_list]
    return index_list, dist_list
Exemple #27
0
def find_nearest(obs_lat, obs_lon, lats, lons):
    """
    Find the closest point, distance in degrees
    (coordinates of the points in degrees).

    """
    point1 = Point(obs_lon, obs_lat)
    dist = 999999999
    index_i, index_j = -1, -1
    for i in range(0, len(lats)):
        for j in range(0, len(lons)):
            point2 = Point(lons[j], lats[i])
            val = point1.distance(point2)
            if val < dist:
                index_i = i
                index_j = j
                dist = val
    return [index_i, index_j, dist]
Exemple #28
0
def populate_school_geo():
	# wow, this is a hard find: Baidu's geocode is not correct on Google map!
	# So we use Baidu's as a reference, and iterate through all available Google's geocode
	# results by distance match, and find the closest one to use!
	for idx, s in enumerate(MySchool.objects.all()):
		if not (s.google_geocode and s.lat and s.lng): continue
		print idx, s.name
		ref = Point(s.lat,s.lng)
		min_dist = 1000000
		min_pair = None
		for g in filter(lambda x: x.has_key('geometry'), s.google_geocode):
			lat,lng = Decimal(g[u'geometry'][u'location'][u'lat']), Decimal(g[u'geometry'][u'location'][u'lng'])
			dist = ref.distance(Point(lat,lng))
			if dist < min_dist: 
				min_dist = dist
				min_pair = (lat,lng)
		if min_pair:
			s.lat, s.lng = min_pair
			s.save()
			print idx, 'updated...... next >'
Exemple #29
0
def dist_to_layer(elon, elat, geom):
    """
    Return the distance from a point to the polygon(s) in a layer; zero if
    the point is inside the polygon. If the nearest edge of the polygon is
    greater than 5000 km from the point, the point cannot be inside the
    polygon and the distance reported will be the distance to the nearest
    edge. So don't make polygons too big.

    Args:
        elon (float): The longitude of the reference point.
        elat (float): The latitude of the reference point.
        geom (Polygon or MultiPolygon): An instance of a shapely Polygon
            or MultiPolygon.

    Returns:
        float: The distance (in km) from the reference point to the
        nearest polygon in the layer. The distance will be zero if the
        point lies inside the polygon.
    """
    if isinstance(geom, Polygon):
        plist = [geom]
    elif isinstance(geom, MultiPolygon):
        plist = list(geom.geoms)
    else:
        raise TypeError('Invalid geometry type in layer: %s' % type(geom))

    project = partial(
        pyproj.transform,
        pyproj.Proj(proj='latlong'),
        pyproj.Proj(proj='aeqd  +lat_0=%f +lon_0=%f +R=6371' % (elat, elon)))
    ep = Point(0.0, 0.0)
    min_dist = 99999.
    for poly in plist:
        nearest = nearest_edge(elon, elat, poly)
        if nearest < 5000:
            nearest = ep.distance(transform(project, poly))
        if nearest < min_dist:
            min_dist = nearest
        if min_dist == 0:
            break
    return min_dist
    def removebadfacets(self, base, doverh=.1):
        """
        remove the facets that cannot support stable placements

        :param: doverh: d is the distance of mproj to supportfacet boundary, h is the height of com
                when fh>dmg, the object tends to fall over. setting doverh to 0.033 means
                when f>0.1mg, the object is judged to be unstable
        :return:

        author: weiwei
        date: 20161213
        """
        self.tpsmat4s = []

        for i in range(len(self.ocfacets)):
            geom = pg.packpandageom(self.objtrimeshconv.vertices,
                                           self.objtrimeshconv.face_normals[self.ocfacets[i]],
                                           self.objtrimeshconv.faces[self.ocfacets[i]])
            geombullnode = cd.genCollisionMeshGeom(geom)
            self.bulletworldray.attachRigidBody(geombullnode)
            pFrom = Point3(self.objcom[0], self.objcom[1], self.objcom[2])
            pTo = self.objcom+self.ocfacetnormals[i]*99999
            pTo = Point3(pTo[0], pTo[1], pTo[2])
            result = self.bulletworldray.rayTestClosest(pFrom, pTo)
            self.bulletworldray.removeRigidBody(geombullnode)
            if result.hasHit():
                hitpos = result.getHitPos()

                facetinterpnt = np.array([hitpos[0],hitpos[1],hitpos[2]])
                facetnormal = np.array(self.ocfacetnormals[i])
                bdverts3d, bdverts2d, facetmat4 = pg.facetboundary(self.objtrimeshconv, self.ocfacets[i],
                                                                     facetinterpnt, facetnormal)
                facetp = Polygon(bdverts2d)
                facetinterpnt2d = rm.transformmat4(facetmat4, facetinterpnt)[:2]
                apntpnt = Point(facetinterpnt2d[0], facetinterpnt2d[1])
                dist2p = apntpnt.distance(facetp.exterior)
                dist2c = np.linalg.norm(np.array([hitpos[0],hitpos[1],hitpos[2]])-np.array([pFrom[0],pFrom[1],pFrom[2]]))
                if dist2p/dist2c >= doverh:
                    # hit and stable
                    self.tpsmat4s.append(pg.cvtMat4np4(facetmat4))
Exemple #31
0
    def infer_reward_state(self, steering_angle, speed):
        try:
            self.set_next_state()
        except Exception as ex:
            utils.json_format_logger(
                "Unable to retrieve image from queue: {}".format(ex),
                **utils.build_system_error_dict(
                    utils.SIMAPP_ENVIRONMENT_EXCEPTION,
                    utils.SIMAPP_EVENT_ERROR_CODE_500))

        # Read model state from Gazebo
        model_state = utils.gazebo_service_call(self.get_model_state,
                                                'racecar', '')

        model_orientation = Rotation.from_quat([
            model_state.pose.orientation.x, model_state.pose.orientation.y,
            model_state.pose.orientation.z, model_state.pose.orientation.w
        ])
        model_location = np.array([
            model_state.pose.position.x,
            model_state.pose.position.y,
            model_state.pose.position.z]) + \
            model_orientation.apply(RELATIVE_POSITION_OF_FRONT_OF_CAR)
        model_point = Point(model_location[0], model_location[1])
        model_heading = model_orientation.as_euler('zyx')[0]

        # Read the wheel locations from Gazebo
        left_rear_wheel_state = utils.gazebo_service_call(
            self.get_link_state, 'racecar::left_rear_wheel', '')
        left_front_wheel_state = utils.gazebo_service_call(
            self.get_link_state, 'racecar::left_front_wheel', '')
        right_rear_wheel_state = utils.gazebo_service_call(
            self.get_link_state, 'racecar::right_rear_wheel', '')
        right_front_wheel_state = utils.gazebo_service_call(
            self.get_link_state, 'racecar::right_front_wheel', '')
        wheel_points = [
            Point(left_rear_wheel_state.link_state.pose.position.x,
                  left_rear_wheel_state.link_state.pose.position.y),
            Point(left_front_wheel_state.link_state.pose.position.x,
                  left_front_wheel_state.link_state.pose.position.y),
            Point(right_rear_wheel_state.link_state.pose.position.x,
                  right_rear_wheel_state.link_state.pose.position.y),
            Point(right_front_wheel_state.link_state.pose.position.x,
                  right_front_wheel_state.link_state.pose.position.y)
        ]

        # Project the current location onto the center line and find nearest points
        current_ndist = self.center_line.project(model_point, normalized=True)
        prev_index, next_index = self.find_prev_next_waypoints(current_ndist)
        distance_from_prev = model_point.distance(
            Point(self.center_line.coords[prev_index]))
        distance_from_next = model_point.distance(
            Point(self.center_line.coords[next_index]))
        closest_waypoint_index = (
            prev_index, next_index)[distance_from_next < distance_from_prev]

        # Compute distance from center and road width
        nearest_point_center = self.center_line.interpolate(current_ndist,
                                                            normalized=True)
        nearest_point_inner = self.inner_border.interpolate(
            self.inner_border.project(nearest_point_center))
        nearest_point_outer = self.outer_border.interpolate(
            self.outer_border.project(nearest_point_center))
        distance_from_center = nearest_point_center.distance(model_point)
        distance_from_inner = nearest_point_inner.distance(model_point)
        distance_from_outer = nearest_point_outer.distance(model_point)
        track_width = nearest_point_inner.distance(nearest_point_outer)
        is_left_of_center = (distance_from_outer < distance_from_inner) if self.reverse_dir \
            else (distance_from_inner < distance_from_outer)

        # Convert current progress to be [0,100] starting at the initial waypoint
        if self.reverse_dir:
            current_progress = self.start_ndist - current_ndist
        else:
            current_progress = current_ndist - self.start_ndist
        if current_progress < 0.0: current_progress = current_progress + 1.0
        current_progress = 100 * current_progress
        if current_progress < self.prev_progress:
            # Either: (1) we wrapped around and have finished the track,
            delta1 = current_progress + 100 - self.prev_progress
            # or (2) for some reason the car went backwards (this should be rare)
            delta2 = self.prev_progress - current_progress
            current_progress = (self.prev_progress, 100)[delta1 < delta2]

        # Car is off track if all wheels are outside the borders
        wheel_on_track = [self.road_poly.contains(p) for p in wheel_points]
        all_wheels_on_track = all(wheel_on_track)
        any_wheels_on_track = any(wheel_on_track)

        # Simulation elapsed time, which may be faster or slower than wall time
        simulation_time = rospy.get_rostime()

        # Compute the reward
        reward = 0.0
        if any_wheels_on_track:
            done = False
            params = {
                'all_wheels_on_track': all_wheels_on_track,
                'x': model_point.x,
                'y': model_point.y,
                'heading': model_heading * 180.0 / math.pi,
                'distance_from_center': distance_from_center,
                'progress': current_progress,
                'steps': self.steps,
                'speed': speed,
                'steering_angle': steering_angle * 180.0 / math.pi,
                'track_width': track_width,
                'waypoints': list(self.center_line.coords),
                'closest_waypoints': [prev_index, next_index],
                'is_left_of_center': is_left_of_center,
                'is_reversed': self.reverse_dir,
                'action': self.action_taken
            }
            try:
                reward = float(self.reward_function(params))
            except Exception as e:
                utils.json_format_logger(
                    "Error in the reward function: {}".format(e),
                    **utils.build_user_error_dict(
                        utils.SIMAPP_SIMULATION_WORKER_EXCEPTION,
                        utils.SIMAPP_EVENT_ERROR_CODE_400))
                traceback.print_exc()
                utils.simapp_exit_gracefully()
        else:
            done = True
            reward = CRASHED

        # Reset if the car position hasn't changed in the last 2 steps
        prev_pnt_dist = min(model_point.distance(self.prev_point),
                            model_point.distance(self.prev_point_2))

        if prev_pnt_dist <= 0.0001 and self.steps % NUM_STEPS_TO_CHECK_STUCK == 0:
            done = True
            reward = CRASHED  # stuck

        # Simulation jobs are done when progress reaches 100
        if current_progress >= 100:
            done = True

        # Keep data from the previous step around
        self.prev_point_2 = self.prev_point
        self.prev_point = model_point
        self.prev_progress = current_progress

        # Set the reward and done flag
        self.reward = reward
        self.reward_in_episode += reward
        self.done = done

        # Trace logs to help us debug and visualize the training runs
        # btown TODO: This should be written to S3, not to CWL.
        logger.info(
            'SIM_TRACE_LOG:%d,%d,%.4f,%.4f,%.4f,%.2f,%.2f,%d,%.4f,%s,%s,%.4f,%d,%.2f,%s,%.4f\n'
            %
            (self.episodes, self.steps, model_location[0], model_location[1],
             model_heading, self.steering_angle, self.speed, self.action_taken,
             self.reward, self.done, all_wheels_on_track, current_progress,
             closest_waypoint_index, self.track_length, time.time(),
             float(simulation_time.secs) + float(simulation_time.nsecs) / 1e9))

        #build json record of the reward metrics
        reward_metrics = OrderedDict()
        reward_metrics['episode'] = self.episodes
        reward_metrics['steps'] = self.steps
        reward_metrics['X'] = model_location[0]
        reward_metrics['Y'] = model_location[1]
        reward_metrics['yaw'] = model_heading
        reward_metrics['steer'] = self.steering_angle
        reward_metrics['throttle'] = self.speed
        reward_metrics['action'] = self.action_taken
        reward_metrics['reward'] = self.reward
        reward_metrics['done'] = self.done
        reward_metrics['all_wheels_on_track'] = all_wheels_on_track
        reward_metrics['progress'] = current_progress
        reward_metrics['closest_waypoint'] = closest_waypoint_index
        reward_metrics['track_len'] = self.track_length
        reward_metrics['tstamp'] = time.time()
        self.simtrace_data.write_simtrace_data(reward_metrics)

        # Terminate this episode when ready
        if done and node_type == SIMULATION_WORKER:
            self.finish_episode(current_progress)
flat = list(sum(intersection_index, ()))
conn_per_wire = Counter(flat)

# checking for isolated wires
complete_list = range(nwires_plus_leads)
isolated_wires = [x for x in complete_list if not x in flat]

# list containing the length segments of each wire (if it has a junction)
each_wire_length_storage = [[] for _ in range(nwires_plus_leads)]

# Routine that obtains the segment lengths on each wire
for i in each_wire_inter_point_storage:

    point_ini = Point(mlines[i].coords[0])  # Initial point of the wire
    point_fin = Point(mlines[i].coords[1])  # Final point of the wire
    wlength = point_ini.distance(point_fin)  # Whole length
    wire_points = each_wire_inter_point_storage[i]

    dist = [0.0] * (len(wire_points) + 1)
    for j in range(len(wire_points)):
        point = Point(wire_points[j])
        dist[j] = point_ini.distance(point)

    dist[
        -1] = wlength  # Whole length stored on the last component of dist vector.
    dist.sort()  # Sorting in crescent order

    dist_sep = [0.0] * len(dist)
    dist_sep[0] = dist[0]
    dist_sep[1:len(dist)] = [
        dist[k] - dist[k - 1] for k in range(1, len(dist))
Exemple #33
0
    #print math.sqrt((x0-x1)**2+(y0-y1)**2)
    return x1, y1


def project((x, y), (x0, y0, x1, y1)):
    line = LineString([(x0, y0), (x1, y1)])
    p = Point(x, y)
    return list(line.interpolate(line.project(p)).coords)[0]


def dist((x, y), (x0, y0, x1, y1)):
    line = LineString([(x0, y0), (x1, y1)])
    p = Point(x, y)
    #_x, _y = list(line.interpolate(line.project(p)).coords)[0]
    #return distance(x, y, _x, _y, utm=True)
    return p.distance(line)


def scale_map_by_axis(map_df, axis):
    x0, x1, y0, y1 = axis
    rids = set(map_df[(map_df.x > x0) & (map_df.x < x1)\
        & (map_df.y > y0) & (map_df.y < y1)].rid)
    map_df.index = map_df.rid
    map_df = map_df.ix[rids]
    return map_df


'''
build a r-tree index for map
'''
    def transportation_Analysis(self, lineAnalysis=False, extern_Graph=None):

        if extern_Graph != None:
            self.graph = extern_Graph

        fichierCSV = open('data1.csv', 'w')
        csvData = open('data2.csv', 'w')

        isDiGraph = nx.DiGraph()
        noDiGraph = nx.Graph()
        default_counter = 1
        fichier = open('notes.txt', 'w')
        nb_relations, relations_count = len(self.relations), 1

        means = ['bus', 'subway', 'tram', 'light_rail']
        list_of_diGraphs = []

        for keyOsmid, relValues in self.relations.items():

            if relations_count < 0:
                print("RELATION NUMBER ", relations_count, "SKIPPED")
                relations_count += 1
                continue

            tags, members = relValues['tags'], relValues['membs']

            print("PROGRESS : ", relations_count, "/", nb_relations)
            # STEP 1 : Determine if the relation is linked to a transportation system and draw the line
            if (('route' in tags) and (tags['route'] in means)) or (
                ('route_master' in tags) and
                (tags['route_master'] in means)) or (
                    ('type' in tags) and ('public_transport' in tags)):
                try:
                    mean = tags['route']
                except:
                    try:
                        mean = tags['route_master']
                    except:
                        mean = 'stop_area'

                G = tla.elements_du_graphe(self.graph,
                                           isDiGraph,
                                           noDiGraph,
                                           keyOsmid,
                                           tags,
                                           members,
                                           self.nodes,
                                           self.ways,
                                           self.coords,
                                           default_counter,
                                           fichierCSV,
                                           csvData,
                                           self.stations,
                                           self.nodes_residue,
                                           type_tr=mean)

            else:
                if 'route' in tags:
                    print("The algorithm doesn't process this route : ",
                          tags['route'])
                else:
                    print("The algorithm doesn't process this relation : ",
                          tags)
                print('\n')
                relations_count += 1
                continue

            if type(G) is int:
                relations_count += 1
                continue
            else:
                if len(G) == 1:  # CASE OF STOP AREA
                    self.graph = G[0]
                    relations_count += 1
                    print('\n')
                    continue
                else:
                    self.graph, isDiGraph, noDiGraph, ways_in_relation, relation_station, relation_platforms, info_name, default_counter, self.stations, self.nodes_residue = G[
                        0], G[1], G[2], G[3], G[4], G[5], G[6], G[7], G[8], G[
                            9]
                    print("STEP 1 COMPLETE , FINAL GRAPH STATE : ",
                          len(list(self.graph.edges)), " EDGES ; ",
                          len(list(self.graph.nodes)), " NODES ")

            # STEP 2 : ORIENTING THE GRAPH
            # On essaye dans un premier temps de déterminer les terminus grâce au nom de ligne
            R = tla.analyse_lexicale(self.graph,
                                     noDiGraph,
                                     isDiGraph,
                                     info_name,
                                     relation_station,
                                     fichier,
                                     tags,
                                     type_mobility=mean)
            self.graph, noDiGraph, isDiGraph, res, departure, arrival = R[
                0], R[1], R[2], R[3], R[4], R[5]

            if res == 0:
                R = tla.analyse_lexicogeographique(self.graph,
                                                   noDiGraph,
                                                   isDiGraph,
                                                   info_name,
                                                   relation_station,
                                                   fichier,
                                                   ways_in_relation,
                                                   departure,
                                                   arrival,
                                                   type_mobility=mean)
                self.graph, noDiGraph, isDiGraph, res = R[0], R[1], R[2], R[3]

                if res == 0:
                    R = tla.redirection_par_defaut(self.graph,
                                                   noDiGraph,
                                                   isDiGraph,
                                                   info_name,
                                                   relation_station,
                                                   fichier,
                                                   ways_in_relation,
                                                   type_mobility=mean)

                    if type(R) == int:
                        self.to_eulerian[info_name] = relation_station
                        relations_count += 1
                        continue
                    else:
                        self.graph, noDiGraph, isDiGraph = R[0], R[1], R[2]
                    #fichierCSV.write(info_name.encode('utf-8') + ";" + str("Default") + "\n")
                else:
                    #fichierCSV.write(info_name.encode('utf-8') + ";" + str("Level 2") + "\n")
                    pass
            else:
                #fichierCSV.write(info_name.encode('utf-8') + ";" + str("Level 1") + "\n")
                pass

            # Tracé des cycles
            R = tla.trace_cycle(self.graph,
                                noDiGraph,
                                isDiGraph,
                                info_name,
                                relation_station,
                                fichier,
                                type_mobility=mean)
            self.graph, noDiGraph, isDiGraph = R[0], R[1], R[2]

            print("STEP 2 COMPLETE , FINAL GRAPH STATE : ",
                  len(list(self.graph.edges)), " EDGES ; ",
                  len(list(self.graph.nodes)), " NODES ")

            # ETAPE 3a : On rajoute les plateformes : simplifiés en noeud centroïde [exclusif bus pour l'instant]
            #Rq : mise à la fin pour éviter les conflits de degré de noeuds
            if mean in ['bus', 'light_rail', 'tram']:
                utiliZation = "Bus Station" if mean == 'bus' else "Tramway Station" if mean == 'tram' else "Light Rail Station" if mean == 'light_rail' else "Undfined Station"
                for key, value in relation_platforms.items():
                    list_of_nodes = value[0]
                    longs, latts = [], []
                    for nodei in range(len(list_of_nodes)):
                        longs.append(
                            self.nodes[list_of_nodes[nodei]]['coords'][0])
                        latts.append(
                            self.nodes[list_of_nodes[nodei]]['coords'][1])
                    moy_longs = float(sum(longs) / len(longs))
                    moy_latts = float(sum(latts) / len(longs))

                    s_point = Point(moy_longs, moy_latts)
                    adde = -int(hashlib.md5(s_point.wkt).hexdigest(), 16)
                    isDiGraph.add_node(adde,
                                       longitude=moy_longs,
                                       latitude=moy_latts,
                                       nom=value[1],
                                       utilisation=utiliZation,
                                       ligne=info_name)
                    self.graph.add_node(adde,
                                        longitude=moy_longs,
                                        latitude=moy_latts,
                                        nom=value[1],
                                        utilisation=utiliZation,
                                        ligne=info_name)

                    nearestCouple = {}
                    edges = list(isDiGraph.edges)
                    for p in range(len(edges)):
                        linestring = geojson.LineString([
                            (isDiGraph.nodes[edges[p][0]]['longitude'],
                             isDiGraph.nodes[edges[p][0]]['latitude']),
                            (isDiGraph.nodes[edges[p][1]]['longitude'],
                             isDiGraph.nodes[edges[p][1]]['latitude'])
                        ])
                        feature = geojson.Feature(geometry=linestring,
                                                  properties={
                                                      "node1": edges[p][0],
                                                      "node2": edges[p][1]
                                                  })
                        candidate = geojson.dumps(feature)
                        line = json.loads(candidate)
                        nearestCouple[s_point.distance(
                            LineString(line['geometry']['coordinates']))] = (
                                line['properties'].values())
                    if nearestCouple != {}:
                        proche_Edge = min(nearestCouple.keys())
                        #print proche_Edge
                        #print "A", nearestCouple[proche_Edge]
                        if proche_Edge < 0.0001:
                            R = linkNode_OSM(isDiGraph,
                                             adde,
                                             nearestCouple[proche_Edge],
                                             diriged=True,
                                             bus=True)
                            isDiGraph, new_node, u, v = R[0], R[1], R[2], R[3]
                            self.graph.add_node(
                                new_node,
                                utilisation='Link Platform_BusLine',
                                ligne=info_name)
                            new_node_attributes = {
                                new_node: isDiGraph.nodes[new_node]
                            }
                            #print new_node_attributes
                            nx.set_node_attributes(self.graph,
                                                   new_node_attributes)
                            use = 'Platform_to_' + str(mean.capitalize())
                            waiting_time = tla.waiting_bus_time if mean == 'bus' else tla.waiting_trm_time if (
                                mean == 'tram' or mean == 'light_rail'
                            ) else tla.waiting_bus_time
                            isDiGraph.add_edge(
                                adde,
                                new_node,
                                type_mobility=use,
                                ligne=info_name,
                                time_travel=waiting_time)  # Entry
                            isDiGraph.add_edge(new_node,
                                               adde,
                                               type_mobility=use,
                                               ligne=info_name,
                                               time_travel=0)  # Exit
                        else:
                            continue
                    else:
                        continue

                print("STEP 3 COMPLETE , FINAL GRAPH STATE : ",
                      len(list(self.graph.edges)), " EDGES ; ",
                      len(list(self.graph.nodes)), " NODES ")

            # STEP 4 : On raccorde les stations intermdiaires représentées que par des points
            #Rq : mise à la fin pour éviter les conflits de degré de noeuds
            for key, value in relation_station.items():
                if isDiGraph.degree[key] == 0:
                    node_to_link = key
                    s_point = Point([value['coords'][0], value['coords'][1]])

                    nearestCouple = {}
                    edges = list(isDiGraph.edges)
                    for p in range(len(edges)):
                        #print edges[p][0], isDiGraph.nodes[edges[p][0]], edges[p][1], isDiGraph.nodes[edges[p][1]]
                        linestring = geojson.LineString([
                            (isDiGraph.nodes[edges[p][0]]['longitude'],
                             isDiGraph.nodes[edges[p][0]]['latitude']),
                            (isDiGraph.nodes[edges[p][1]]['longitude'],
                             isDiGraph.nodes[edges[p][1]]['latitude'])
                        ])
                        feature = geojson.Feature(geometry=linestring,
                                                  properties={
                                                      "node1": edges[p][0],
                                                      "node2": edges[p][1]
                                                  })
                        candidate = geojson.dumps(feature)
                        line = json.loads(candidate)
                        nearestCouple[s_point.distance(
                            LineString(line['geometry']['coordinates']))] = (
                                line['properties'].values())
                    if nearestCouple != {}:
                        proche_Edge = min(nearestCouple.keys())
                        #print proche_Edge
                        #print "A", nearestCouple[proche_Edge]
                        if proche_Edge < 0.0001:
                            R = linkNode_OSM(isDiGraph,
                                             node_to_link,
                                             nearestCouple[proche_Edge],
                                             diriged=True,
                                             bus=True)
                            isDiGraph, new_node, u, v = R[0], R[1], R[2], R[3]
                            self.graph.add_node(
                                new_node,
                                utilisation='Link Station_BusLine',
                                ligne=info_name)
                            new_node_attributes = {
                                new_node: isDiGraph.nodes[new_node]
                            }
                            #print new_node_attributes
                            nx.set_node_attributes(self.graph,
                                                   new_node_attributes)
                            if mean in ['bus', 'light_rail', 'tram']:
                                use = 'Station_to_' + str(mean.capitalize())
                                waiting_time = tla.waiting_bus_time if mean == 'bus' else tla.waiting_trm_time if (
                                    mean == 'tram' or mean == 'light_rail'
                                ) else tla.waiting_bus_time
                                isDiGraph.add_edge(
                                    node_to_link,
                                    new_node,
                                    type_mobility=use,
                                    ligne=info_name,
                                    time_travel=waiting_time)  # Entry
                                isDiGraph.add_edge(new_node,
                                                   node_to_link,
                                                   type_mobility=use,
                                                   ligne=info_name,
                                                   time_travel=0)  # Exit
                            elif mean == 'subway':
                                isDiGraph.add_edge(
                                    node_to_link,
                                    new_node,
                                    type_mobility='Subway_Linked',
                                    ligne=info_name,
                                    time_travel=tla.waiting_sub_time)
                                isDiGraph.add_edge(
                                    new_node,
                                    node_to_link,
                                    type_mobility='Subway_Linked',
                                    ligne=info_name,
                                    time_travel=0)

                        else:
                            continue
                    else:
                        continue
                else:
                    continue

            print("STEP 4 COMPLETE , FINAL GRAPH STATE : ",
                  len(list(self.graph.edges)), " EDGES ; ",
                  len(list(self.graph.nodes)), " NODES ")

            nx.set_node_attributes(isDiGraph, self.stations)
            self.stations = {}

            # STEP 4 : ADD INTO THE FINAL GRAPH
            if mean == 'bus':
                self.graph = tla.replacer_A(isDiGraph, self.graph, info_name)
            else:
                self.graph = tla.replacer_B(isDiGraph, self.graph, info_name)
            print("STEP 5 COMPLETE , FINAL GRAPH STATE : ",
                  len(list(self.graph.edges)), " EDGES ; ",
                  len(list(self.graph.nodes)), " NODES ")

            print('\n')

            isDiGraph.clear()
            noDiGraph.clear()
            relations_count += 1

        fichier.close()
        fichierCSV.close()
Exemple #35
0
class LIDAR(object):
    def __init__(self, posX, posY, resolution=1., mapRadius=100):
        self.x = posX
        self.y = posY
        self.origin = Point(self.x, self.y)
        self.resolution = pi * resolution / 180.0
        self.nRays = int(np.floor(2. * pi / self.resolution))
        self.measure = np.zeros(self.nRays)
        self.pointMap = [None] * self.nRays
        self.rays = [None] * self.nRays
        self.angles = np.arange(0, 2. * pi, self.resolution)
        self.mapRadius = mapRadius

    def updatePose(self, delta_x, delta_y, delta_theta):
        self.x += delta_x
        self.y += delta_y
        self.origin = Point(self.x, self.y)
#        self.angles += delta_theta

    def setPose(self, pos, theta):
        self.x = pos[0]
        self.y = pos[1]
        self.origin = Point(self.x, self.y)

        self.angles = np.arange(theta, 2. * pi + theta, self.resolution)

    def updateRays(self):
        for idx in range(self.nRays):
            angle = self.angles[idx]
            endPoint = Point(self.x + cos(angle) * self.mapRadius,
                             self.y + sin(angle) * self.mapRadius)
            self.rays[idx] = LineString([self.origin, endPoint])

    def rayTracing(self, bodies):

        closeBodies = []
        for body in bodies:
            if self.origin.distance(
                    body.getOrigin()) <= self.mapRadius + body.radius:
                closeBodies.append(body)

        self.measure = np.repeat(self.mapRadius, self.nRays)
        self.pointMap = [r.boundary[1] for r in self.rays]

        for body in closeBodies:
            d = self.origin.distance(body.getOrigin())
            if (d > body.radius):
                alpha = asin(body.radius / d)
                midAngle = atan2(body.y - self.y, body.x - self.x)
                if (midAngle < 0):
                    midAngle += 2 * pi
                iAngle = midAngle - alpha
                fAngle = midAngle + alpha

                if (fAngle > 2 * pi):
                    fAngle -= 2 * pi
                if (iAngle < 0):
                    iAngle += 2 * pi

                infeasibleAngles = np.where(self.angles > fAngle)[0]
                if (len(infeasibleAngles) > 0):
                    lastRayIdx = infeasibleAngles[0]
                else:
                    lastRayIdx = 0

                infeasibleAngles = np.where(self.angles < iAngle)[0]
                if (len(infeasibleAngles) > 0):
                    idx = infeasibleAngles[-1]
                else:
                    idx = self.nRays

                if (idx > lastRayIdx):
                    idx = idx - self.nRays

            else:
                idx = 0
                lastRayIdx = self.nRays

            poly = body.getPolygon()
            while idx < lastRayIdx:
                ray = self.rays[idx]
                distance = self.mapRadius
                inter = poly.intersection(ray)

                if inter.is_empty:
                    # radial line doesn't intersect, so skip
                    idx += 1
                    continue

                if len(inter.coords) > 1:
                    # ray intersects at multiple points
                    inter_dict = {}
                    for p in inter.boundary:
                        inter_dict[self.origin.distance(p)] = p
                    distance = min(inter_dict.keys())
                    intersect = inter_dict[distance]
                else:
                    # ray intersects at one point only
                    distance = self.origin.distance(inter)
                    intersect = inter

                if (self.measure[idx] > distance):
                    self.measure[idx] = distance
                    self.pointMap[idx] = intersect
                idx += 1


#
#        closeBodies = []
#        for body in bodies:
#            if self.origin.distance(body.getOrigin()) <= self.mapRadius + body.radius:
#                closeBodies.append(body)
#
#        self.measure = np.repeat(self.mapRadius,self.nRays)
#        self.pointMap = [r.boundary[1] for r in self.rays]
#
#        for body in closeBodies:
#            lastRayIdx = self.nRays
#            idx = 0
#            bodyNotFound = True
#            poly = body.getPolygon()
#            while idx < lastRayIdx:
#                ray = self.rays[idx]
#                distance = self.mapRadius
#                inter = poly.intersection(ray)
#
#                if inter.is_empty:
#                   # radial line doesn't intersect, so skip
#                   idx += 1
#                   continue
#
#                if len(inter.coords) > 1:
#                   # ray intersects at multiple points
#                   inter_dict = {}
#                   for p in inter.boundary:
#                       inter_dict[self.origin.distance(p)] = p
#                   distance = min(inter_dict.keys())
#                   intersect = inter_dict[distance]
#                else:
#                   # ray intersects at one point only
#                   distance  = self.origin.distance(inter)
#                   intersect = inter
#
#                if(distance < self.mapRadius and bodyNotFound == True):
#                    # it's the first sign of this body. Let's limit the scan
#                    if(distance > body.radius):
#                        alpha = atan((2.*body.radius)/distance) #angle left for scanning
#                        lastRayIdx = min(lastRayIdx, idx + int(alpha/self.resolution))
#                    bodyNotFound = False
#
#                if(self.measure[idx] > distance):
#                    self.measure[idx] = distance
#                    self.pointMap[idx] = intersect
#                idx += 1
#
#
#
#        polys = [b.getPolygon() for b in bodies]
#        radial_sweep = cascaded_union(self.rays)
#        all_input_lines = cascaded_union(polys)
#        self.measure = np.repeat(self.mapRadius,self.nRays)
#        self.pointMap = [None]*self.nRays
#        for idx in range(len(radial_sweep)):
#            radial_line = radial_sweep[idx]
#            inter = radial_line.intersection(all_input_lines)
#            if inter.is_empty:
#               continue
#
#            if len(inter.coords) > 1:
#               # ray intersects at multiple points
#               inter_dict = {}
#               for inter_pt in inter.coords:
#                   p = Point(inter_pt)
#                   inter_dict[self.origin.distance(p)] = p
#               distance = min(inter_dict.keys())
#               intersect = inter_dict[distance]
#            else:
#               # ray intersects at one point only
#               distance  = self.origin.distance(inter)
#               intersect = inter
#
##            if(distance < self.mapRadius):
##                if(bodyNotFound == True):
##                    # it's the first sign of this body. Let's limit the scan
##                    if(distance > body.radius):
##                        alpha = asin(body.radius/distance) #angle left for scanning
##                        lastRayIdx = min(lastRayIdx, idx + int(alpha/self.resolution))
##                bodyNotFound = False
#
#            if(self.measure[idx] > distance):
#                self.measure[idx] = distance
#                self.pointMap[idx] = intersect
Exemple #36
0
def sourcedistance(lats, lons, deps, ss, ds, latsta, lonsta, site_length,
                   nvert, props):
    p = float(props.getpvalue())
    [x, y] = GMCOMP_coord_tools.ll2utm(lons, lats, numpy.median(lons),
                                       numpy.median(lats))
    z = deps

    rake = 180 / math.pi * numpy.arctan2(ds, ss)

    fault_slip = numpy.sqrt(numpy.power(ss, 2) + numpy.power(ds, 2))

    totslip = numpy.sum(fault_slip)

    xravel = numpy.ravel(x)
    yravel = numpy.ravel(y)
    zravel = numpy.ravel(z)

    if (nvert == 4):
        Z1 = z[:, 0]
        Z2 = z[:, 1]
        Z3 = z[:, 2]
        Z4 = z[:, 3]

        X1 = x[:, 0]
        X2 = x[:, 1]
        X3 = x[:, 2]
        X4 = x[:, 3]

        Y1 = y[:, 0]
        Y2 = y[:, 1]
        Y3 = y[:, 2]
        Y4 = y[:, 3]

        fseg = len(x)

        ##        for i in range (0, fseg):
        ##            XS = [X1[i], X2[i], X3[i]]
        ##            YS = [Y1[i], Y2[i], Y3[i]]
        ##            ZS = [Z1[i], Z2[i], Z3[i]]
        ##
        ##            [strike, dip] = strikedip(XS, YS, ZS)

        Ztor = numpy.amin(numpy.vstack((Z1, Z2, Z3, Z4))) / 1000

        atop = numpy.where(numpy.amin(zravel) == zravel)[0]
        abot = numpy.where(numpy.amax(zravel) == zravel)[0]
        yravelt = numpy.ravel(yravel[atop])
        xravelt = numpy.ravel(xravel[atop])
        zravelt = numpy.ravel(zravel[atop])
        atops = numpy.where((numpy.amin(yravelt) == yravelt))[0]
        atopn = numpy.where((numpy.amax(yravelt) == yravelt))[0]
        yravelb = numpy.ravel(yravel[abot])
        xravelb = numpy.ravel(xravel[abot])
        zravelb = numpy.ravel(zravel[abot])
        abots = numpy.where((numpy.amin(yravelb) == yravelb))[0]
        abotn = numpy.where((numpy.amax(yravelb) == yravelb))[0]

        ytopn = yravelt[atopn]
        ytops = yravelt[atops]
        ybotn = yravelb[abotn]
        ybots = yravelb[abots]

        xtopn = xravelt[atopn]
        xtops = xravelt[atops]
        xbotn = xravelb[abotn]
        xbots = xravelb[abots]

        ztopn = zravelt[atopn]
        ztops = zravelt[atops]
        zbotn = zravelb[abotn]
        zbots = zravelb[abots]

        FLen = math.sqrt(
            math.pow(xtops[0] - xtopn[0], 2) +
            math.pow(ytops[0] - ytopn[0], 2)) / 1000
        FWid = math.sqrt(
            math.pow(xtops[0] - xbots[0], 2) +
            math.pow(ytops[0] - ybots[0], 2) +
            math.pow((ztops[0] - zbots[0]) * 1000, 2)) / 1000

        XS = [xtops[0], xtopn[0], xbotn[0]]
        YS = [ytops[0], ytopn[0], ybotn[0]]
        ZS = [ztops[0], ztopn[0], zbotn[0]]
        [strike, dip] = strikedip(XS, YS, ZS)

        Rp = numpy.zeros([site_length, 1])
        Rrup = numpy.zeros([site_length, 1])
        Rjb = numpy.zeros([site_length, 1])
        Rx = numpy.zeros([site_length, 1])
        Hw = numpy.zeros([site_length, 1])

        for i in range(0, site_length):
            [xs, ys] = GMCOMP_coord_tools.ll2utm(lonsta[i, 0], latsta[i, 0],
                                                 numpy.median(lons),
                                                 numpy.median(lats))
            R1 = numpy.sqrt(
                numpy.power(xs - X1, 2) + numpy.power(ys - Y1, 2) +
                numpy.power(Z1, 2))
            R2 = numpy.sqrt(
                numpy.power(xs - X2, 2) + numpy.power(ys - Y2, 2) +
                numpy.power(Z2, 2))
            R3 = numpy.sqrt(
                numpy.power(xs - X3, 2) + numpy.power(ys - Y3, 2) +
                numpy.power(Z3, 2))
            R4 = numpy.sqrt(
                numpy.power(xs - X4, 2) + numpy.power(ys - Y4, 2) +
                numpy.power(Z4, 2))
            R = numpy.vstack((R1, R2, R3, R4))
            dist = numpy.mean(R, axis=0) / 1000

            Rrup[i, 0] = numpy.amin(dist)

            Rp[i, 0] = numpy.power(
                numpy.sum(fault_slip * numpy.power(dist, -1.4)),
                -1 / 1.4) / numpy.power(numpy.sum(fault_slip), -1 / 1.4)

            xl1 = xtops[0]
            xl2 = xtopn[0]
            yl1 = ytops[0]
            yl2 = ytopn[0]

            Rx[i, 0] = perp_dist(xl1, yl1, xl2, yl2, xs, ys) / 1000

            if (dip < 90):
                point = Point(xs, ys)
                polygon = Polygon([(xl1, yl1), (xl1, yl2), (xl2, yl2),
                                   (xl2, yl1)])
                Rjb[i, 0] = point.distance(polygon) / 1000.0
            else:
                Rjb[i, 0] = Rx[i, 0]

            Hw[i, 0] = numpy.sign((yl2 - yl1) * xs - (xl2 - xl1) * ys +
                                  xl2 * yl1 - yl2 * xl1)


##        a1 = numpy.where(numpy.amin(z) == z)[0]
##        #Southern and northern extent of top of fault
##        atops = numpy.where((numpy.amin(yravel) == yravel) & (numpy.amin(zravel) == zravel))[0]
##        atopn = numpy.where((numpy.amax(yravel) == yravel) & (numpy.amin(zravel) == zravel))[0]
##        abots = numpy.where((numpy.amin(yravel) == yravel) & (numpy.amax(zravel) == zravel))[0]
##        abotn = numpy.where((numpy.amax(yravel) == yravel) & (numpy.amax(zravel) == zravel))[0]
##        print (atops,atopn,abots,abotn)
##
##        #If fault is true E-W, ends defined by east west extent
##        if (strike == 90 or strike == 270):
##            atops = numpy.where((numpy.amax(xravel) == xravel) & (numpy.amin(zravel) == zravel))[0]
##            atopn = numpy.where((numpy.amin(xravel) == xravel) & (numpy.amin(zravel) == zravel))[0]
##            abots = numpy.where((numpy.amin(xravel) == xravel) & (numpy.amax(zravel) == zravel))[0]
##            abotn = numpy.where((numpy.amax(xravel) == xravel) & (numpy.amax(zravel) == zravel))[0]
##
##        FLen = math.sqrt(math.pow(xravel[atops[0]]-xravel[atopn[0]],2)+math.pow(yravel[atops[0]]-yravel[atopn[0]],2))/1000
##        FWid = math.sqrt(math.pow(xravel[atops[0]]-xravel[abots[0]],2)+math.pow(yravel[atops[0]]-yravel[abots[0]],2)+math.pow(zravel[atops[0]]-zravel[abots[0]],2))/1000
##
##        Rrup = numpy.zeros([site_length,1])
##        Rjb = numpy.zeros([site_length,1])
##        Rx = numpy.zeros([site_length,1])
##        Rp = numpy.zeros([site_length,1])
##        Hw = numpy.zeros([site_length,1])
##        for i in range (0, site_length):
##            [xs,ys] = GMCOMP_coord_tools.ll2utm(lonsta[i,0],latsta[i,0],numpy.median(lons),numpy.median(lats))
##
##            R1 = numpy.sqrt(numpy.power(xs-X1,2)+numpy.power(ys-Y1,2)+numpy.power(Z1,2))
##            R2 = numpy.sqrt(numpy.power(xs-X2,2)+numpy.power(ys-Y2,2)+numpy.power(Z2,2))
##            R3 = numpy.sqrt(numpy.power(xs-X3,2)+numpy.power(ys-Y3,2)+numpy.power(Z3,2))
##            R4 = numpy.sqrt(numpy.power(xs-X4,2)+numpy.power(ys-Y4,2)+numpy.power(Z4,2))
##            R = numpy.vstack((R1,R2,R3,R4))
##            dist = numpy.mean(R, axis=0)/1000
##
##            Rrup[i,0] = numpy.amin(dist)
##
##            Rp[i,0] = numpy.power(numpy.sum(fault_slip/totslip*numpy.power(dist,p)),1/p)
##            xmin = numpy.min(x)
##            xmax = numpy.max(x)
##            ymin = numpy.min(y)
##            ymax = numpy.max(y)
##
##
##            xsoff = 1000*math.sin(strike*math.pi/180)
##            ysoff = 1000*math.cos(strike*math.pi/180)
##
##            xl1 = xravel[atops[0]]
##            xl2 = xravel[atopn[0]]
##            yl1 = yravel[atops[0]]
##            yl2 = yravel[atopn[0]]
##
##            Rx[i,0] = perp_dist(xl1, yl1, xl2, yl2, xs, ys)/1000
##
##            if (dip < 90):
##                point = Point(xs,ys)
##                polygon = Polygon([(xmin,ymin), (xmin,ymax), (xmax,ymax), (xmax,ymin)])
##                Rjb[i,0] = point.distance(polygon)/1000.0
##            else:
##                Rjb[i,0] = Rx[i,0]
##
##
##            Hw[i,0] = numpy.sign((yl2-yl1)*xs-(xl2-xl1)*ys+xl2*yl1-yl2*xl1)

    return (Rrup, Rjb, Rx, Rp, Ztor, Hw, rake, strike, dip, FLen, FWid)
    print("Adding edges to H between ward center and nearest road node")
    l = range(len(shape_roads))  #Did this programatically
    count = 0
    for node in ALL_coord_pair_wc[:5]:
        count += 1
        point = Point(node)
        cutting_list = []
        longest_distance = 10000000000000000000000000  # ~ = infinity
        c = 0  #c will be the index of road segment selected

        #Parse list of road segments and update until closest one selected (c = index)
        for a in l:
            road_seg_a = LIST_ROAD_SEG[a]
            line_a = LineString(road_seg_a)
            dist_to_road = point.distance(
                line_a)  #how far is the road from this wc?
            if dist_to_road < longest_distance:
                longest_distance = dist_to_road
                c = a  #this gives us index number of the road to access for adding point

        #Get linestring for the closest road segment
        road_seg_c = LIST_ROAD_SEG[c]
        line_t = LineString(road_seg_c)

        #Get nearest point on line to point
        break_node_temp = line_t.interpolate(line_t.project(point))
        coords = list(break_node_temp.coords)

        #Turn point into list to match graph data format
        break_node = []
        break_node.append(coords[0][0])
Exemple #38
0
    return elevation_matrix[row, col]


g = nx.Graph()
road_links = solent_itn['roadlinks']
for link in road_links:
    Time = []
    pt_start = Point(road_links[link]['coords'][0])
    elevation_start = get_elevation(pt_start.x, pt_start.y)
    for i in road_links[link]['coords'][1:]:
        pt = Point(i)
        elevation_difference = get_elevation(pt.x, pt.y) - elevation_start
        pt_start = pt
        if elevation_difference > 0:
            Time.append(
                pt.distance(pt_start) / 5000 + elevation_difference / 600)
        else:
            Time.append(pt.distance(pt_start) / 5000)
    g.add_edge(road_links[link]['start'],
               road_links[link]['end'],
               fid=link,
               weight=Time)

#nx.draw(g, node_size=1)

path = nx.dijkstra_path(g,
                        source=(450000, 85000),
                        target=(460000, 92500),
                        weight="weight")

Exemple #39
0
    def infer_reward_state(self, steering_angle, speed):
        try:
            self.set_next_state()
        except Exception as ex:
            utils.json_format_logger(
                "Unable to retrieve image from queue: {}".format(ex),
                **utils.build_system_error_dict(
                    utils.SIMAPP_ENVIRONMENT_EXCEPTION,
                    utils.SIMAPP_EVENT_ERROR_CODE_500))

        # Read model state from Gazebo
        model_state = self.get_model_state('racecar', '')
        model_orientation = Rotation.from_quat([
            model_state.pose.orientation.x, model_state.pose.orientation.y,
            model_state.pose.orientation.z, model_state.pose.orientation.w
        ])
        model_location = np.array([
            model_state.pose.position.x,
            model_state.pose.position.y,
            model_state.pose.position.z]) + \
            model_orientation.apply(RELATIVE_POSITION_OF_FRONT_OF_CAR)
        model_point = Point(model_location[0], model_location[1])
        model_heading = model_orientation.as_euler('zyx')[0]

        # Read the wheel locations from Gazebo
        left_rear_wheel_state = self.get_link_state('racecar::left_rear_wheel',
                                                    '')
        left_front_wheel_state = self.get_link_state(
            'racecar::left_front_wheel', '')
        right_rear_wheel_state = self.get_link_state(
            'racecar::right_rear_wheel', '')
        right_front_wheel_state = self.get_link_state(
            'racecar::right_front_wheel', '')
        wheel_points = [
            Point(left_rear_wheel_state.link_state.pose.position.x,
                  left_rear_wheel_state.link_state.pose.position.y),
            Point(left_front_wheel_state.link_state.pose.position.x,
                  left_front_wheel_state.link_state.pose.position.y),
            Point(right_rear_wheel_state.link_state.pose.position.x,
                  right_rear_wheel_state.link_state.pose.position.y),
            Point(right_front_wheel_state.link_state.pose.position.x,
                  right_front_wheel_state.link_state.pose.position.y)
        ]

        # Project the current location onto the center line and find nearest points
        current_ndist = self.center_line.project(model_point, normalized=True)
        prev_index, next_index = self.find_prev_next_waypoints(current_ndist)
        distance_from_prev = model_point.distance(
            Point(self.center_line.coords[prev_index]))
        distance_from_next = model_point.distance(
            Point(self.center_line.coords[next_index]))
        closest_waypoint_index = (
            prev_index, next_index)[distance_from_next < distance_from_prev]

        # Compute distance from center and road width
        nearest_point_center = self.center_line.interpolate(current_ndist,
                                                            normalized=True)
        nearest_point_inner = self.inner_border.interpolate(
            self.inner_border.project(nearest_point_center))
        nearest_point_outer = self.outer_border.interpolate(
            self.outer_border.project(nearest_point_center))
        distance_from_center = nearest_point_center.distance(model_point)
        distance_from_inner = nearest_point_inner.distance(model_point)
        distance_from_outer = nearest_point_outer.distance(model_point)
        track_width = nearest_point_inner.distance(nearest_point_outer)
        is_left_of_center = (distance_from_outer < distance_from_inner) if self.reverse_dir \
            else (distance_from_inner < distance_from_outer)

        print('model_state.pose.position.x=%.2f, model_point.x=%.2f' %
              (model_state.pose.position.x, model_point.x))

        # Compute the distance to the closest bot car
        is_crashed = False
        dist_closest_bot_car = 1e3  # large number
        closest_bot = -1  # index of closest bot car
        for kk in range(len(self.bot_cars)):
            dist_to_bot_car = np.sqrt(
                (model_state.pose.position.x -
                 self.bot_cars[kk].car_model_state.pose.position.x)**2 +
                (model_state.pose.position.y -
                 self.bot_cars[kk].car_model_state.pose.position.y)**2)
            if dist_to_bot_car < dist_closest_bot_car:
                dist_closest_bot_car = dist_to_bot_car
                closest_bot = kk

        # new @suntao
        botcar = self.bot_cars[closest_bot].car_model_state
        botcar_orientation = Rotation.from_quat([
            botcar.pose.orientation.x, botcar.pose.orientation.y,
            botcar.pose.orientation.z, botcar.pose.orientation.w
        ])
        botcar_heading = botcar_orientation.as_euler('zyx')[0]

        botcar_location = np.array([
            botcar.pose.position.x,
            botcar.pose.position.y,
            botcar.pose.position.z]) + \
            botcar_orientation.apply(RELATIVE_POSITION_OF_FRONT_OF_CAR)
        botcar_point = Point(botcar_location[0], botcar_location[1])
        botcar_current_ndist = self.center_line.project(botcar_point,
                                                        normalized=True)
        botcar_prev_index, botcar_next_index = self.find_prev_next_waypoints(
            botcar_current_ndist)

        if dist_closest_bot_car < 0.30:
            is_crashed = True

        # Convert current progress to be [0,100] starting at the initial waypoint
        if self.reverse_dir:
            current_progress = self.start_ndist - current_ndist
        else:
            current_progress = current_ndist - self.start_ndist
        if current_progress < 0.0: current_progress = current_progress + 1.0
        current_progress = 100 * current_progress
        if current_progress < self.prev_progress:
            # Either: (1) we wrapped around and have finished the track,
            delta1 = current_progress + 100 - self.prev_progress
            # or (2) for some reason the car went backwards (this should be rare)
            delta2 = self.prev_progress - current_progress
            current_progress = (self.prev_progress, 100)[delta1 < delta2]

        # Car is off track if all wheels are outside the borders
        wheel_on_track = [self.road_poly.contains(p) for p in wheel_points]
        all_wheels_on_track = all(wheel_on_track)
        any_wheels_on_track = any(wheel_on_track)

        #         if current_progress > 30:
        #             done = True
        #             reward = 1e2

        # Compute the reward
        if (not is_crashed) and any_wheels_on_track:
            done = False
            params = {
                'all_wheels_on_track': all_wheels_on_track,
                'x': model_point.x,
                'y': model_point.y,
                'heading': model_heading * 180.0 / math.pi,
                'distance_from_center': distance_from_center,
                'progress': current_progress,
                'steps': self.steps,
                'speed': speed,
                'steering_angle': steering_angle * 180.0 / math.pi,
                'track_width': track_width,
                'waypoints': list(self.center_line.coords),
                'closest_waypoints': [prev_index, next_index],
                'is_left_of_center': is_left_of_center,
                'is_reversed': self.reverse_dir,
                'is_crashed': is_crashed,
                'dist_closest_bot': dist_closest_bot_car,
                'bot_x': botcar_point.x,
                'bot_y': botcar_point.y,
                'bot_heading': botcar_heading * 180.0 / math.pi,
                'bot_closest_waypoints':
                [botcar_prev_index, botcar_next_index],
            }
            try:
                reward = float(self.reward_function(params))
            except Exception as e:
                utils.json_format_logger(
                    "Exception {} in customer reward function. Job failed!".
                    format(e),
                    **utils.build_user_error_dict(
                        utils.SIMAPP_SIMULATION_WORKER_EXCEPTION,
                        utils.SIMAPP_EVENT_ERROR_CODE_400))
                traceback.print_exc()
                os._exit(1)
        else:
            done = True
            reward = CRASHED

        # Reset if the car position hasn't changed in the last 2 steps
        prev_pnt_dist = min(model_point.distance(self.prev_point),
                            model_point.distance(self.prev_point_2))

        if prev_pnt_dist <= 0.0001 and self.steps % NUM_STEPS_TO_CHECK_STUCK == 0:
            done = True
            reward = CRASHED  # stuck

        # Simulation jobs are done when progress reaches 100
        if current_progress >= 100:
            done = True
            reward = 1e3

        # Keep data from the previous step around
        self.prev_point_2 = self.prev_point
        self.prev_point = model_point
        self.prev_progress = current_progress

        # Set the reward and done flag
        self.reward = reward
        self.reward_in_episode += reward
        self.done = done

        # Trace logs to help us debug and visualize the training runs
        # btown TODO: This should be written to S3, not to CWL.
        logger.info(
            'SIM_TRACE_LOG:%d,%d,%.4f,%.4f,%.4f,%.2f,%.2f,%d,%.4f,%s,%s,%.4f,%d,%.2f,%s,%s,%.2f\n'
            % (self.episodes, self.steps, model_location[0], model_location[1],
               model_heading, self.steering_angle, self.speed,
               self.action_taken, self.reward, self.done, all_wheels_on_track,
               current_progress, closest_waypoint_index, self.track_length,
               time.time(), is_crashed, dist_closest_bot_car))

        # Terminate this episode when ready
        if done and node_type == SIMULATION_WORKER:
            self.finish_episode(current_progress)
Exemple #40
0
def evaluate_collision(ego_vehicle_state, dynamic_vehicle_state, dy_obsList,
                       static_vehicle_state, st_obsList, config):
    dis_list = []
    dis_satisfaction = []

    ## in case that the size are not same
    num_dynamic_obs = len(dy_obsList)
    num_static_obs = len(st_obsList)

    if num_dynamic_obs:
        range_list_dy = len(dynamic_vehicle_state[0])
    else:
        range_list_dy = 0
    for obs in range(1, num_dynamic_obs):
        range_list_dy = min(range_list_dy, len(dynamic_vehicle_state[obs]))

    if num_static_obs:
        range_list_st = len(static_vehicle_state[0])
    else:
        range_list_st = 0
    for obs in range(1, num_static_obs):
        range_list_st = min(range_list_st, len(static_vehicle_state[obs]))

    if num_dynamic_obs and num_static_obs:
        if range_list_dy < range_list_st:
            range_list = min(len(ego_vehicle_state), range_list_dy)
        else:
            range_list = min(len(ego_vehicle_state), range_list_st)
    elif num_dynamic_obs:
        range_list = min(len(ego_vehicle_state), range_list_dy)
    elif num_static_obs:
        range_list = min(len(ego_vehicle_state), range_list_st)

    for i in range(range_list):
        min_dist = 1000
        flag = 0
        ego_direction = ego_vehicle_state[i][3]
        x = ego_vehicle_state[i][0]
        y = ego_vehicle_state[i][1]
        ego_vehicle_center = Point(x, y)
        point1 = Point(x - config.ego_length / 2, y - config.ego_width / 2)
        point2 = Point(x + config.ego_length / 2, y - config.ego_width / 2)
        point3 = Point(x + config.ego_length / 2, y + config.ego_width / 2)
        point4 = Point(x - config.ego_length / 2, y + config.ego_width / 2)
        ego_rect = Polygon([point1, point2, point3, point4])
        angle = ego_direction * (180.0 / math.pi)

        rect_ego = affinity.rotate(ego_rect, angle)

        for num in range(len(dynamic_vehicle_state)):
            vehicle_width = float(dy_obsList[num]["width"])
            vehicle_length = float(dy_obsList[num]["length"])

            veh_x = dynamic_vehicle_state[num][i][4]
            veh_y = dynamic_vehicle_state[num][i][5]
            other_vehicle_center = Point(veh_x, veh_y)
            point1 = Point(veh_x - vehicle_length / 2,
                           veh_y - vehicle_width / 2)
            point2 = Point(veh_x + vehicle_length / 2,
                           veh_y - vehicle_width / 2)
            point3 = Point(veh_x + vehicle_length / 2,
                           veh_y + vehicle_width / 2)
            point4 = Point(veh_x - vehicle_length / 2,
                           veh_y + vehicle_width / 2)
            other_rect = Polygon([point1, point2, point3, point4])
            angle = dynamic_vehicle_state[num][i][2] * (180.0 / math.pi)
            rect_other = affinity.rotate(other_rect, angle)

            intersection = rect_ego.intersection(rect_other)
            if not intersection.is_empty:
                # print("rect_ego", rect_ego)
                # print("ego_state", ego_vehicle_state[i][0], ego_vehicle_state[i][1])
                # print("other rect:", other_rect)
                # print("other vehicle state:", dynamic_vehicle_state[num][i][4], dynamic_vehicle_state[num][i][5])
                flag = -1
                min_dist = -1
                break
            else:
                dist = ego_vehicle_center.distance(other_vehicle_center)
                if dist < min_dist:
                    # print("rect_ego", rect_ego)
                    # print("ego_state", ego_vehicle_state[i][0], ego_vehicle_state[i][1])
                    # print("other rect:", rect_other)
                    # print("other vehicle state:", dynamic_vehicle_state[num][i][4], dynamic_vehicle_state[num][i][5])
                    min_dist = dist

        for num in range(len(static_vehicle_state)):
            vehicle_width = float(st_obsList[num]["width"])
            vehicle_length = float(st_obsList[num]["length"])

            veh_x = dynamic_vehicle_state[num][i][4]
            veh_y = dynamic_vehicle_state[num][i][5]
            other_vehicle_center = Point(veh_x, veh_y)
            point1 = Point(veh_x - vehicle_width / 2,
                           veh_y - vehicle_length / 2)
            point2 = Point(veh_x + vehicle_width / 2,
                           veh_y - vehicle_length / 2)
            point3 = Point(veh_x + vehicle_width / 2,
                           veh_y + vehicle_length / 2)
            point4 = Point(veh_x - vehicle_width / 2,
                           veh_y + vehicle_length / 2)
            other_rect = Polygon([point1, point2, point3, point4])

            intersection = rect_ego.intersection(other_rect)
            if not intersection.is_empty:
                flag = -1
                min_dist = -1
                break
            else:
                dist = ego_vehicle_center.distance(other_vehicle_center)
                if dist < min_dist:
                    min_dist = dist

        dis_list.append(min_dist)
        # print(min_dist)
        if min_dist >= config.minimumseperation:
            satisfaction = 1
        elif min_dist < config.assured_clear_distance_ahead:
            satisfaction = 0
        else:
            satisfaction = (min_dist - config.assured_clear_distance_ahead) / (
                config.minimumseperation - config.assured_clear_distance_ahead)

        dis_satisfaction.append(satisfaction)

    # print(dis_list, dis_satisfaction, dis_satisfaction)

    if len(dis_list) and len(dis_satisfaction):
        min_dis = min(dis_list)
        min_satisfaction = min(dis_satisfaction)
        avg_satisfaction = np.mean(dis_satisfaction)
    else:
        min_dis = -1
        min_satisfaction = -1
        avg_satisfaction = -1

    return min_dis
Exemple #41
0
def update_charts(clickData, selected_data, clicks, task4_type, anomaly_type,
                  data):
    # read the data from the store
    empty_task2 = empty_task2_df.to_json(date_format='iso', orient='split')
    empty_task4 = empty_task4_df.to_json(date_format='iso', orient='split')
    data = data or {
        'last_clicks': 0,
        'origin_x': 0,
        'origin_y': 0,
        'distance': 0,
        'task2_df': empty_task2,
        'task4_df': empty_task4,
        'task4_box_df': empty_task4,
        'last_click_data': None,
        'last_selected_data': None,
    }
    last_clicks = data['last_clicks']
    origin = Point(data['origin_x'], data['origin_y'])
    distance = data['distance']
    df_task2 = pd.read_json(data['task2_df'], orient='split')
    df_task4 = pd.read_json(data['task4_df'], orient='split')
    df_task4_box = pd.read_json(data['task4_box_df'], orient='split')
    stored_click_data = data['last_click_data']
    stored_selected_data = data['last_selected_data']

    anomaly_name = 'Isostatic Anomaly'
    anomaly_column_name = 'isostatic_anom'
    if anomaly_type == 'bouguer':
        anomaly_name = 'Bouguer Anomaly'
        anomaly_column_name = 'Bouguer_anom_267'
    elif anomaly_type == 'freeair':
        anomaly_name = 'Free Air Anomaly'
        anomaly_column_name = 'Free_air_anom'
    elif anomaly_type == 'observed':
        anomaly_name = 'Observed Gravity Anomaly'
        anomaly_column_name = 'obs_grav'

    layout = dict(autosize=True,
                  title=dict(x=0.5),
                  margin=dict(l=0, r=0, b=0, t=40),
                  xaxis_title=" Distance (m)",
                  height=300)

    bar_layout = dict(
        height=300,
        bargap=0.1,
        xaxis_type="category",
        xaxis_title="Station ID",
        yaxis_title=anomaly_name + " (mGal)",
        title=dict(text="Distribution and Ranking", x=0.5),
        showlegend=False,
    )

    transect_fig = make_subplots(specs=[[{"secondary_y": True}]])

    task4_fig = px.bar()

    if clicks != last_clicks:
        last_clicks = clicks
        df_task2 = empty_task2_df
        df_task4 = empty_task4_df
        df_task4_box = empty_task4_df
        origin = Point(0, 0)
        distance = 0
        data['last_clicks'] = clicks

    elif clickData:
        lon = clickData['points'][0]['lon']
        lat = clickData['points'][0]['lat']
        x, y = degrees2meters(lon, lat)
        if origin == Point(0, 0):
            origin = Point(x, y)

        if task4_type == 'click':
            # update task2
            distance += origin.distance(Point(x, y))
            custom_data = clickData['points'][0]['customdata']
            if stored_click_data != clickData:
                df_task2 = df_task2.append(
                    {
                        'distance': distance,
                        'elevation': custom_data[2],
                        'station_id': custom_data[1],
                        'isostatic_anom': custom_data[0],
                        'Free_air_anom': custom_data[3],
                        'Bouguer_anom_267': custom_data[4],
                        'obs_grav': custom_data[5]
                    },
                    ignore_index=True)
            df_task2 = df_task2.sort_values('distance').reset_index(drop=True)

            origin = Point(x, y)

            # update task4
            station_id = str(clickData['points'][0]['customdata'][1])

            if station_id not in df_task4['station_id'].tolist(
            ) and stored_click_data != clickData:
                df_task4 = df_task4.append(
                    {
                        'isostatic_anom': custom_data[0],
                        'station_id': station_id,
                        'Free_air_anom': custom_data[3],
                        'Bouguer_anom_267': custom_data[4],
                        'obs_grav': custom_data[5]
                    },
                    ignore_index=True)
            df_task4 = df_task4.sort_values(anomaly_column_name,
                                            ascending=False)
            df_task4 = df_task4.reset_index(drop=True)

            colors = [
                px.colors.qualitative.Plotly[0],
            ] * len(df_task4['station_id'])
            if df_task4.shape[0] > 0:
                to_be_change_color = df_task4[df_task4['station_id'] ==
                                              station_id].index.item()
                colors[to_be_change_color] = px.colors.qualitative.Plotly[1]

            task4_fig = px.bar(df_task4,
                               x='station_id',
                               y=anomaly_column_name,
                               color="station_id",
                               color_discrete_sequence=colors,
                               labels={
                                   'station_id': 'Station ID',
                                   anomaly_column_name: anomaly_name
                               })

    anomaly_data = df_task2.isostatic_anom
    if anomaly_type == 'bouguer':
        anomaly_data = df_task2.Bouguer_anom_267
    elif anomaly_type == 'freeair':
        anomaly_data = df_task2.Free_air_anom
    elif anomaly_type == 'observed':
        anomaly_data = df_task2.obs_grav

    # Add traces
    transect_fig.add_trace(
        go.Scatter(
            x=df_task2.distance,
            y=anomaly_data,
            name=anomaly_name,
            customdata=list(zip(df_task2.station_id)),
            hovertemplate=
            'Station ID: %{customdata[0]}<br>Anomaly: %{y} mGal<br>Distance: %{x} m<extra></extra>'
        ),
        secondary_y=False,
    )

    transect_fig.add_trace(
        go.Scatter(
            x=df_task2.distance,
            y=df_task2.elevation,
            name="Elevation",
            customdata=list(zip(df_task2.station_id)),
            hovertemplate=
            'Station ID: %{customdata[0]}<br>Elevation: %{y} ft<br>Distance: %{x} m<extra></extra>'
        ),
        secondary_y=True,
    )

    transect_fig.update(layout=layout)
    transect_fig.update(layout=dict(
        title=dict(text="Transect", x=0.5),
        legend=dict(yanchor="bottom", y=1.02, xanchor="right", x=1.05)))

    # Set x-axis title
    transect_fig.update_xaxes(title_text="Distance (m)")

    # Set y-axes titles
    transect_fig.update_yaxes(title_text=anomaly_name + " (mGal)",
                              secondary_y=False)
    transect_fig.update_yaxes(title_text="Elevation (ft)", secondary_y=True)

    if task4_type == 'box':
        if not selected_data:
            task4_fig = px.bar()
        if selected_data and selected_data['points']:
            if selected_data != stored_selected_data:
                for point in selected_data['points']:
                    df_task4_box = df_task4_box.append(
                        {
                            'isostatic_anom': point['customdata'][0],
                            'station_id': point['customdata'][1],
                            'Free_air_anom': point['customdata'][3],
                            'Bouguer_anom_267': point['customdata'][4],
                            'obs_grav': point['customdata'][5]
                        },
                        ignore_index=True)

            # Update bar chart
            df_task4_box = df_task4_box.drop_duplicates()
            df_task4_box = df_task4_box.sort_values(anomaly_column_name,
                                                    ascending=False)
            df_task4_box = df_task4_box.reset_index(drop=True)
            task4_fig = px.bar(df_task4_box,
                               x='station_id',
                               y=anomaly_column_name,
                               labels={
                                   'station_id': 'Station ID',
                                   anomaly_column_name: anomaly_name
                               })
            task4_fig.update_layout(bargap=0)

            if clickData:
                colors = [
                    px.colors.qualitative.Plotly[0],
                ] * len(df_task4_box['station_id'])
                station_id = clickData['points'][0]['customdata'][1]

                if station_id in df_task4_box['station_id'].tolist():
                    to_be_change_color = df_task4_box[
                        df_task4_box['station_id'] == station_id].index.item()
                    colors[to_be_change_color] = px.colors.qualitative.Plotly[
                        1]

                    task4_fig = px.bar(df_task4_box,
                                       x='station_id',
                                       y=anomaly_column_name,
                                       color="station_id",
                                       color_discrete_sequence=colors,
                                       labels={
                                           'station_id': 'Station ID',
                                           anomaly_column_name: anomaly_name
                                       })

    task4_fig.update(layout=layout)
    task4_fig.update(layout=bar_layout)

    # update the stored data
    data['origin_x'] = origin.x
    data['origin_y'] = origin.y
    data['distance'] = distance
    data['task2_df'] = df_task2.to_json(date_format='iso', orient='split')
    data['task4_df'] = df_task4.to_json(date_format='iso', orient='split')
    data['task4_box_df'] = df_task4_box.to_json(date_format='iso',
                                                orient='split')
    data['last_click_data'] = clickData
    data['last_selected_data'] = selected_data

    return transect_fig, task4_fig, data
Exemple #42
0
def srcmod_fspreader(fspfile, sta_lat, sta_lon):
    fault_lat = []
    fault_lon = []
    fault_dep = []
    fault_slip = []
    fault_rake = []

    for line in open(fspfile):
        li = line.strip()
        if not li.startswith("%"):
            fs = line.split()
            if not fs:
                pass
            else:
                fault_lat = numpy.append(fault_lat, [float(fs[0])], axis=0)
                fault_lon = numpy.append(fault_lon, [float(fs[1])], axis=0)
                fault_dep = numpy.append(fault_dep, [float(fs[4])], axis=0)
                fault_slip = numpy.append(fault_slip, [float(fs[5])], axis=0)
                fault_rake = numpy.append(fault_rake, [float(fs[7])], axis=0)

    lat0 = numpy.mean(fault_lat)
    lon0 = numpy.mean(fault_lon)
    totslip = numpy.sum(fault_slip)
    rake = numpy.mean(fault_rake)

    [x0, y0] = GMCOMP_coord_tools.ll2utm(fault_lon, fault_lat, lon0,
                                         lat0)  #convert fault locations to UTM
    [xs,
     ys] = GMCOMP_coord_tools.ll2utm(sta_lon, sta_lat, lon0,
                                     lat0)  #convert station locations to UTM

    Ztor = numpy.amin(fault_dep)

    atop = numpy.where(Ztor == fault_dep)[0]
    abot = numpy.where(numpy.amax(fault_dep) == fault_dep)[0]
    yravelt = numpy.ravel(y0[atop])
    xravelt = numpy.ravel(x0[atop])
    zravelt = numpy.ravel(fault_dep[atop])
    atops = numpy.where((numpy.amin(yravelt) == yravelt))[0]
    atopn = numpy.where((numpy.amax(yravelt) == yravelt))[0]
    yravelb = numpy.ravel(y0[abot])
    xravelb = numpy.ravel(x0[abot])
    zravelb = numpy.ravel(fault_dep[abot])
    abots = numpy.where((numpy.amin(yravelb) == yravelb))[0]
    abotn = numpy.where((numpy.amax(yravelb) == yravelb))[0]

    ytopn = yravelt[atopn]
    ytops = yravelt[atops]
    ybotn = yravelb[abotn]
    ybots = yravelb[abots]

    xtopn = xravelt[atopn]
    xtops = xravelt[atops]
    xbotn = xravelb[abotn]
    xbots = xravelb[abots]

    ztopn = zravelt[atopn]
    ztops = zravelt[atops]
    zbotn = zravelb[abotn]
    zbots = zravelb[abots]

    FLen = math.sqrt(
        math.pow(xtops[0] - xtopn[0], 2) +
        math.pow(ytops[0] - ytopn[0], 2)) / 1000
    FWid = math.sqrt(
        math.pow(xtops[0] - xbots[0], 2) + math.pow(ytops[0] - ybots[0], 2) +
        math.pow((ztops[0] - zbots[0]) * 1000, 2)) / 1000

    XS = [float(xtops[0]), float(xtopn[0]), float(xbotn[0])]
    YS = [float(ytops[0]), float(ytopn[0]), float(ybotn[0])]
    ZS = [
        1000 * float(ztops[0]), 1000 * float(ztopn[0]), 1000 * float(zbotn[0])
    ]
    [strike, dip] = strikedip(XS, YS, ZS)
    fault_strike = strike * numpy.ones([len(fault_lat), 1])
    fault_dip = dip * numpy.ones([len(fault_lat), 1])

    numsta = len(xs)  #number of stations
    Rp = numpy.zeros([numsta, 1])
    Rrup = numpy.zeros([numsta, 1])
    Rjb = numpy.zeros([numsta, 1])
    Rx = numpy.zeros([numsta, 1])
    Hw = numpy.zeros([numsta, 1])

    for i in range(0, numsta):
        dist = numpy.sqrt(
            numpy.power(x0 - xs[i], 2) + numpy.power(y0 - ys[i], 2) +
            numpy.power(fault_dep * 1000, 2)) / 1000
        Rrup[i, 0] = numpy.amin(dist)

        Rp[i, 0] = numpy.power(numpy.sum(
            fault_slip * numpy.power(dist, -1.4)), -1 / 1.4) / numpy.power(
                numpy.sum(fault_slip), -1 / 1.4)

        xl1 = xtops[0]
        xl2 = xtopn[0]
        yl1 = ytops[0]
        yl2 = ytopn[0]

        Rx[i, 0] = perp_dist(xl1, yl1, xl2, yl2, xs[i], ys[i]) / 1000

        if (dip < 90):
            point = Point(xs[i], ys[i])
            polygon = Polygon([(xl1, yl1), (xl1, yl2), (xl2, yl2), (xl2, yl1)])
            Rjb[i, 0] = point.distance(polygon) / 1000.0
        else:
            Rjb[i, 0] = Rx[i, 0]

        Hw[i, 0] = numpy.sign((yl2 - yl1) * xs[i] - (xl2 - xl1) * ys[i] +
                              xl2 * yl1 - yl2 * xl1)

    return (Rrup, Rjb, Rx, Rp, Ztor, Hw, rake, strike, dip, FLen, FWid,
            fault_lat, fault_lon, fault_dep, fault_slip, fault_rake,
            fault_strike, fault_dip)
Exemple #43
0
    def lookup(self, loc, peril_id, coverage_type):
        """
        Area peril lookup for an individual lon/lat location item, which can be
        provided as a dict or a Pandas series. The data structure should contain
        the keys `lon` or `longitude` for longitude and `lat` or `latitude` for
        latitude.
        """
        idx = self.peril_areas_index
        boundary = self.peril_areas_boundary
        loc_to_areas_min_dist = self.loc_to_global_areas_boundary_min_distance

        loc_id_col = self.loc_id_col

        loc_id = loc.get(loc_id_col) or int(uuid.UUID(bytes=os.urandom(16)).hex[:16], 16)

        loc_x_col = self.loc_coords_x_col
        loc_y_col = self.loc_coords_y_col
        loc_x_bounds = self.loc_coords_x_bounds
        loc_y_bounds = self.loc_coords_y_bounds

        x = loc.get(loc_x_col)
        y = loc.get(loc_y_col)

        _lookup = lambda loc_id, x, y, st, perid, covtype, paid, pabnds, pacoords, msg: {
            loc_id_col: loc_id,
            loc_x_col: x,
            loc_y_col: y,
            'peril_id': perid,
            'coverage_type': covtype,
            'status': st,
            'peril_area_id': paid,
            'area_peril_id': paid,
            'area_bounds': pabnds,
            'area_coordinates': pacoords,
            'message': msg
        }

        try:
            x = float(x)
            y = float(y)
            if not ((loc_x_bounds[0] <= x <= loc_x_bounds[1]) and (loc_y_bounds[0] <= y <= loc_y_bounds[1])):
                raise ValueError('{}/{} out of bounds'.format(loc_x_col, loc_y_col))
        except (ValueError, TypeError) as e:
            msg = (
                'Peril area lookup: invalid {}/{} ({}, {}) - {}'
                .format(loc_x_col, loc_y_col, x, y, str(e))
            )
            return _lookup(loc_id, x, y, OASIS_KEYS_STATUS['fail']['id'], peril_id, coverage_type, None, None, None, msg)

        st = OASIS_KEYS_STATUS['nomatch']['id']
        msg = 'No peril area match'
        paid = None
        pabnds = None
        pacoords = None
        point = x, y

        try:
            results = list(idx.intersection(point, objects='raw'))

            if not results:
                raise IndexError

            for _perid, _covtype, _paid, _pabnds, _pacoords in results:
                if (peril_id, coverage_type) == (_perid, _covtype):
                    paid, pabnds, pacoords = _paid, _pabnds, _pacoords
                    break

            if paid == None:
                raise IndexError
        except IndexError:
            try:
                results = list(idx.nearest(point, objects='raw'))

                if not results:
                    raise IndexError

                for _perid, _covtype, _paid, _pabnds, _pacoords in results:
                    if (peril_id, coverage_type) == (_perid, _covtype):
                        paid, pabnds, pacoords = _paid, _pabnds, _pacoords
                        break

                if paid == None:
                    msg = 'No intersecting or nearest peril area found for peril ID {} and coverage type {}'.format(peril_id, coverage_type)
                    return _lookup(loc_id, x, y, OASIS_KEYS_STATUS['nomatch']['id'], peril_id, coverage_type, None, None, None, msg)
            except IndexError:
                pass
            else:
                p = Point(x, y)
                min_dist = p.distance(boundary)
                if min_dist > loc_to_areas_min_dist:
                    msg = (
                        'Peril area lookup: location is {} units from the '
                        'peril areas global boundary -  the required minimum '
                        'distance is {} units'
                        .format(min_dist, loc_to_areas_min_dist)
                    )
                    return _lookup(loc_id, x, y, OASIS_KEYS_STATUS['fail']['id'], peril_id, coverage_type, None, None, None, msg)
                st = OASIS_KEYS_STATUS['success']['id']
                msg = (
                    'Successful peril area lookup: {}'.format(paid)
                )
        except RTreeError as e:
            return _lookup(loc_id, x, y, OASIS_KEYS_STATUS['fail']['id'], peril_id, coverage_type, None, None, None, str(e))
        else:
            st = OASIS_KEYS_STATUS['success']['id']
            msg = 'Successful peril area lookup: {}'.format(paid)

        return _lookup(loc_id, x, y, st, peril_id, coverage_type, paid, pabnds, pacoords, msg)
Exemple #44
0
def add_linking_edges_for_new_node(graph,
                                   new_node,
                                   split_point,
                                   edge,
                                   nts,
                                   db_costs,
                                   logging=False):
    edge_geom = edge['geometry']
    # split edge at new node to two line geometries
    split_lines = geom_utils.split_line_at_point(edge_geom, split_point)
    node_from = edge['uvkey'][0]
    node_to = edge['uvkey'][1]
    node_from_p = get_node_geom(graph, node_from)
    node_to_p = get_node_geom(graph, node_to)
    edge_first_p = Point(edge_geom.coords[0])
    if (edge_first_p.distance(node_from_p) < edge_first_p.distance(node_to_p)):
        link1 = split_lines[0]
        link2 = split_lines[1]
    else:
        link1 = split_lines[1]
        link2 = split_lines[0]
    if (logging == True):
        print('add linking edges between:', node_from, new_node, node_to)
    # interpolate noise cost attributes for new linking edges so that they work in quiet path routing
    link1_noise_costs = get_edge_noise_cost_attrs(nts, db_costs, edge, link1)
    link2_noise_costs = get_edge_noise_cost_attrs(nts, db_costs, edge, link2)
    # combine link attributes to prepare adding them as new edges
    link1_attrs = {
        'geometry': link1,
        'length': round(link1.length, 3),
        **link1_noise_costs
    }
    link2_attrs = {
        'geometry': link2,
        'length': round(link2.length, 3),
        **link2_noise_costs
    }
    # add linking edges with noice cost attributes to graph
    graph.add_edges_from([(node_from, new_node, {
        'uvkey': (node_from, new_node),
        **link1_attrs
    })])
    graph.add_edges_from([(new_node, node_from, {
        'uvkey': (new_node, node_from),
        **link1_attrs
    })])
    graph.add_edges_from([(node_to, new_node, {
        'uvkey': (node_to, new_node),
        **link2_attrs
    })])
    graph.add_edges_from([(new_node, node_to, {
        'uvkey': (new_node, node_to),
        **link2_attrs
    })])
    link1_d = {'uvkey': (new_node, node_from), **link1_attrs}
    link2_d = {'uvkey': (node_to, new_node), **link2_attrs}
    return {
        'node_from': node_from,
        'new_node': new_node,
        'node_to': node_to,
        'link1': link1_d,
        'link2': link2_d
    }
def main(argv):
    try:
        directory = argv[0] + '/'
        model = argv[1]
        offset = float(argv[2])
        noit = int(argv[3])
    except:
        print '''
    *******************************************

       Usage: atan_bestfit_creep.py directory model offset

            directory: directory to transect.mat file
            model: 'interseismic' or 'creep' or 'both'
            offset: if there is an offset of creep from the fault (in meters)

    *******************************************
    '''
        sys.exit(1)
    fileList = glob.glob(directory + '/*.mat')
    #    print(fileList[0])
    transectmat = scipy.io.loadmat(str(fileList[0]))
    # transect = transectmat['dataset'][0][0][1]
    transect = transectmat['dataset'][0][0][1]

    avgInSAR = np.nanmean(transect, axis=1)
    stdInSAR = np.nanstd(transect, axis=1)

    transect_dist = transectmat['dataset'][0][0][2]
    transect_lat_first = transectmat['dataset'][0][0][0][0][25]
    transect_lon_first = transectmat['dataset'][0][0][3][0][25]
    transect_first = Point(transect_lon_first, transect_lat_first)
    transect_lat_end = transectmat['dataset'][0][0][0][-1][25]
    transect_lon_end = transectmat['dataset'][0][0][3][-1][25]
    transect_end = Point(transect_lon_end, transect_lat_end)

    transect_middle_line = LineString([(transect_first), (transect_end)])
    fault_line = LineString([(26.835038363171353, 40.600641025641025),
                             (26.849744245524295, 40.605769230769226),
                             (26.86253196930946, 40.610256410256405),
                             (26.875319693094628, 40.61474358974359),
                             (26.884271099744243, 40.61730769230769),
                             (26.896419437340153, 40.621153846153845),
                             (26.90920716112532, 40.625),
                             (26.929028132992325, 40.631410256410255),
                             (26.93542199488491, 40.63525641025641),
                             (26.95012787723785, 40.64038461538461),
                             (26.96547314578005, 40.64487179487179),
                             (26.975703324808183, 40.648717948717945),
                             (26.982097186700766, 40.651282051282045),
                             (26.992966751918157, 40.65448717948718),
                             (27.005754475703323, 40.65961538461538),
                             (27.01918158567775, 40.66474358974359),
                             (27.028772378516624, 40.66858974358974),
                             (27.033887468030688, 40.67115384615384),
                             (27.04092071611253, 40.674358974358974),
                             (27.04923273657289, 40.67820512820513),
                             (27.058823529411764, 40.68141025641025),
                             (27.062020460358056, 40.682051282051276),
                             (27.072250639386187, 40.68589743589743),
                             (27.083759590792837, 40.68846153846154),
                             (27.09846547314578, 40.69102564102564),
                             (27.108056265984654, 40.69358974358974),
                             (27.121483375959077, 40.69615384615384),
                             (27.13107416879795, 40.69807692307692),
                             (27.141943734015346, 40.70128205128205),
                             (27.160485933503836, 40.70641025641025),
                             (27.186700767263424, 40.71282051282051),
                             (27.19757033248082, 40.715384615384615),
                             (27.20843989769821, 40.71730769230769),
                             (27.224424552429667, 40.72115384615385),
                             (27.239769820971865, 40.725641025641025),
                             (27.251918158567772, 40.72948717948718),
                             (27.26790281329923, 40.73461538461538),
                             (27.280690537084396, 40.73782051282051)])

    intersect = transect_middle_line.intersection(fault_line)
    #intersect = intersection(transect_middle_line,fault_line)
    dist = (transect_first.distance(intersect)) * 100000.0
    dist2 = (intersect.distance(transect_end)) * 100000.0

    ################################################################################
    ###GPS LOCATIONS
    BGNT = Point(26.570, 40.932)
    BGNTvel = [2.11, -2.28, 0]
    BGNTvelLOS = enu2los(*BGNTvel) / -1000.
    BGNTdist = BGNT.distance(fault_line) * -100000.0

    DOKU = Point(26.706, 40.739)
    DOKUvel = [-2.73, -0.96, 0]
    DOKUvelLOS = enu2los(*DOKUvel) / -1000.
    DOKUdist = DOKU.distance(fault_line) * -100000.0  #in meters

    KVAM = Point(26.871, 40.601)
    KVAMvel = [-9.05, -3.26, 0]
    KVAMvelLOS = enu2los(*KVAMvel) / -1000.
    KVAMdist = KVAM.distance(fault_line) * -100000.0

    KVM2 = Point(26.871, 40.601)
    KVM2vel = [-10.33, -4, 0]
    KVM2velLOS = enu2los(*KVM2vel) / -1000.
    KVM2dist = KVM2.distance(fault_line) * -100000.0

    SEV2 = Point(26.880, 40.396)
    SEV2vel = [-15.39, -6.01, 0]
    SEV2velLOS = enu2los(*SEV2vel) / -1000.
    SEV2dist = SEV2.distance(fault_line) * 100000.0

    BKCT = Point(27.091, 40.932)
    BKCTvel = [-17.04, -6.81, 0]
    BKCTvelLOS = enu2los(*BKCTvel) / -1000.
    BKCTdist = BKCT.distance(fault_line) * 100000.0

    # GPSdist = [int(BGNTdist),int(DOKUdist),int(KVAMdist),int(KVM2dist),int(SEV2dist),int(BKCTdist)]
    GPSdist = [int(DOKUdist), int(SEV2dist), int(BKCTdist)]

    # GPSvel = [BGNTvelLOS,DOKUvelLOS,KVAMvelLOS,KVM2velLOS,SEV2velLOS,BKCTvelLOS]
    GPSvel = [DOKUvelLOS, SEV2velLOS, BKCTvelLOS]
    GPSvelRef = np.array(GPSvel) - GPSvel[0]
    GPSvelPlot = [i * 1000.0 for i in GPSvel]

    # print('BGNT VEL: '+str(BGNTvelLOS))
    # print('BGNT DIST: '+str(BGNTdist))
    #
    # print(DOKUvelLOS)
    # print(KVAMvelLOS)
    # print(KVM2velLOS)
    # print(SEV2velLOS)
    # print(BKCTvelLOS)
    # print(len(avgInSAR))
    # sys.exit()
    ################################################################################
    depth = []
    slip = []
    varModel = []
    ##1-D space##
    insarSpace = np.linspace(-dist, dist2, num=len(avgInSAR), endpoint=True)
    # numB = abs(GPSdist[0])+abs(GPSdist[-1])
    # insarGPSSpace = np.linspace(GPSdist[0],GPSdist[-1],num=numB, dtype = int,endpoint=True)
    insarGPSSpace = np.linspace(GPSdist[0],
                                GPSdist[-1],
                                num=len(avgInSAR) + len(GPSdist),
                                dtype=int,
                                endpoint=True)
    sortedSpace = (sorted(insarGPSSpace))
    # sortedSpace = (sorted(insarSpace))

    ################################################################################
    n = -1

    def find_nearest(array, value):
        idx = np.searchsorted(array, value, side="left")
        if idx > 0 and (idx == len(array) or math.fabs(value - array[idx - 1])
                        < math.fabs(value - array[idx])):
            return array[idx - 1]
        else:
            return array[idx]

    Vel = list(avgInSAR[:] - avgInSAR[0])
    for i in GPSdist:
        n = n + 1
        GPSindex = find_nearest(sortedSpace, i)
        # GPSindex = sortedSpace.index(i)
        Vel.insert(GPSindex, GPSvelRef[n])
    Vel = np.array(Vel)
    # print(Vel[0])
    x = np.array(sortedSpace)
    sortedSpaceKm = (x / 1000.0)
    # sys.exit()
    ################################################################################
    #    G = np.ones([len(transect),2])

    G = np.ones([len(Vel), 2])
    G[:, 0] = sortedSpaceKm
    G_inv = np.dot(np.linalg.inv(np.dot(G.T, G)), G.T)
    G_inv = np.array(G_inv, np.float32)
    sol = np.dot(G_inv, Vel)
    k = np.dot(G, sol)

    D = np.arange(100., 20000., 100.)
    V = np.arange(-0.001, -0.10, -0.001)
    rmse_inv = []
    for d in D:
        for s in V:
            v2 = sol[0] + ((s / np.pi) * np.arctan(x / d)) + sol[1]
            residual = v2 - Vel
            # rms = np.sqrt((sum((residual)**2,0))/len(transect))
            rms = np.sqrt((sum((residual)**2, 0)) / len(Vel))
            rmse_inv.append([d, s, rms])

    rmse_inv = np.array(rmse_inv, np.float32)
    idx = np.argmin(rmse_inv[:, 2])
    rmse_inv_min = rmse_inv[idx]
    print 'Inversion: ' + str(rmse_inv_min)
    v2_rms_min_inv = ((rmse_inv_min[1] / np.pi) *
                      np.arctan(x / rmse_inv_min[0])) + sol[0] + sol[1]
    ################################################################################
    if model == 'interseismic':
        for q in xrange(noit):
            avgVel = []
            for i in xrange(len(avgInSAR)):
                avgVel.append(np.random.normal(avgInSAR[i], stdInSAR[i]))
            avgVel = np.array(avgVel)

            G = np.ones([len(Vel), 2])
            G[:, 0] = sortedSpaceKm
            G_inv = np.dot(np.linalg.inv(np.dot(G.T, G)), G.T)
            G_inv = np.array(G_inv, np.float32)
            sol = np.dot(G_inv, Vel)
            k = np.dot(G, sol)

            D = np.arange(1000., 10000., 100.)
            V = np.arange(-0.001, -0.030, -0.0005)
            rmse = []
            for d in D:
                for s in V:
                    v2 = sol[0] + ((s / np.pi) * np.arctan(x / d)) + sol[1]
                    residual = v2 - avgVel
                    rms = np.sqrt((sum((residual)**2, 0)) / len(transect))
                    rmse.append([d, s, rms])

            rmse = np.array(rmse, np.float32)
            idx = np.argmin(rmse[:, 2])
            rmse_min = rmse[idx]
            print str(q) + ' ' + str(rmse_min)
            depth.append(rmse_min[0])
            slip.append(rmse_min[1])
            # r.append(rmse_min[2])
            varModel.append(rmse_min[2]**2)

        stdModel = np.sqrt(sum(varModel))
        slip[:] = [x * 1000.0 for x in slip]

        ################################################################################
        ##PLOT average and standard deviation##
        fig = plt.figure()
        plt.rcParams.update({'font.size': 22})
        fig.set_size_inches(10, 10)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        cov = np.cov(slip, depth)
        vals, vecs = eigsorted(cov)
        theta = np.degrees(np.arctan2(*vecs[:, 0][::-1]))
        for nstd in xrange(1, 4):  ####nstd is 1sigma, 2sigma, 3sigma
            w, h = 2 * nstd * np.sqrt(vals)
            ell = Ellipse(xy=(np.mean(slip), np.mean(depth)),
                          width=w,
                          height=h,
                          angle=theta,
                          color='black')
            ell.set_facecolor('none')
            ax.add_artist(ell)

        plt.scatter(slip, depth)
        plt.scatter(rmse_inv_min[1] * 1000.,
                    rmse_inv_min[0],
                    s=60,
                    c='red',
                    marker='*')
        plt.ylabel('Depth (m)')
        plt.xlabel('Slip (mm/yr)')
        # plt.xlim(1,30)
        # plt.ylim(1000,10000)
        fig.savefig('depth_vs_slip.png')
        plt.close()

        depth.append(rmse_inv_min[0])
        slip.append(rmse_inv_min[1])
        stdDepth = roundup(np.std(depth))
        stdSlip = math.ceil(np.std(slip))

        # fig = plt.figure()
        # plt.rcParams.update({'font.size': 22})
        # fig.set_size_inches(20,8)
        # ax = plt.Axes(fig, [0., 0., 1., 1.], )
        # ax=fig.add_subplot(111)
        # ax.plot(xp,transect*1000.0,'o',ms=1,mfc='Black', linewidth='0')
        # label = 'Best Fit: '+str(int(rmse_inv_min[0]))+'$\pm$'+str(int(stdDepth))+'m; '+str(int(abs(round(rmse_inv_min[1]*1000.,2))))+'$\pm$'+str(int(stdSlip))+'mm/yr'
        # ax.plot(xp,(v2_rms_min_inv*1000.),'b--',label=label)
        # ax.legend(loc='upper right')
        # plt.ylabel('Velocity (mm/yr)')
        # plt.xlabel('Distance (km)')
        # fig.savefig('atan_best_'+str(rmse_inv_min[0])+'_'+str(rmse_inv_min[1])+'.png')
        # plt.close()
        fig = plt.figure()
        plt.rcParams.update({'font.size': 22})
        fig.set_size_inches(20, 8)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        ax.plot(xp, transect * 1000.0, 'o', ms=1, mfc='Black', linewidth='0')
        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR - i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #,color='#DCDCDC')#'LightGrey')
        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR + i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #'LightGrey')
        ax.plot(xp, avgInSAR * 1000.0, 'r-', label='Average velocity')

        # label1 = 'Interseismic only: Locking depth: '+str(int(rmse_min[0]))+' meters - Slip rate: '+str(int(abs(round(rmse_min[1]*1000.,2))))+' mm/yr'
        label1 = 'Best Fit: ' + str(int(rmse_inv_min[0])) + '$\pm$' + str(
            int(stdDepth)) + 'm; ' + str(
                int(abs(round(rmse_inv_min[1] * 1000., 2)))) + '$\pm$' + str(
                    int(stdSlip)) + 'mm/yr'
        ax.plot(xp, ((sol[0] + v2_rms_min_inv + sol[1]) * 1000.),
                'b--',
                label=label1)
        ax.legend(loc='lower left')
        plt.ylabel('Velocity (mm/yr)')
        plt.xlabel('Distance (km)')
        fig.savefig(directory + 'atan_best_' + str(rmse_inv_min[0]) + '.png')
        plt.close()
################################################################################
    elif model == 'creep':
        ##Atan plus creep##
        x = x + offset
        rmse_c_inv = []
        # Q = np.arange(0.00001,rmse_inv[0],100)

        d1 = 0.01
        for d2 in D:
            for s in V:
                v1 = v2_rms_min_inv + (
                    (s / np.pi) *
                    (np.arctan(x / d1) - np.arctan(x / d2))) + sol[0] + sol[1]
                # residual = v1 - avgInSAR
                residual = v1 - Vel

                # rms = np.sqrt((sum((residual)**2,0))/len(transect))
                rms = np.sqrt((sum((residual)**2, 0)) / len(Vel))
                rmse_c_inv.append([d1, d2, s, rms])

        rmse_c_inv = np.array(rmse_c_inv, np.float32)
        idx = np.argmin(rmse_c_inv[:, 3])
        rmse_c_inv_min = rmse_c_inv[idx]
        print 'Creep Inversion: ' + str(rmse_c_inv_min)
        v1_rms_min = (rmse_c_inv_min[2] / np.pi) * (np.arctan(
            x / rmse_c_inv_min[0]) - np.arctan(x / rmse_c_inv_min[1]))
        # v1_rms_min = (-0.006/np.pi)*(np.arctan(x/rmse_c_inv_min[0])-np.arctan(x/rmse_c_inv_min[1]))

        m_c_inv = v1_rms_min + v2_rms_min_inv + sol[0] + sol[1]
        ################################################################################
        # for q in xrange(noit):
        #     avgVel = []
        #     rmse_c=[]
        #     for i in xrange(len(avgInSAR)):
        #         avgVel.append(np.random.normal(avgInSAR[i], stdInSAR[i]))
        #     avgVel = np.array(avgVel)
        #
        #     for d2 in D:
        #         for s in V:
        #             v1 = sol[0]+sol[1]+v2_rms_min_inv+((s/np.pi)*(np.arctan(x/d1)-np.arctan(x/d2)))
        #             residual = v1 - avgVel
        #             # rms = np.sqrt((sum((residual)**2,0))/len(transect))
        #             rms = np.sqrt((sum((residual)**2,0))/len(Vel))
        #
        #             rmse_c.append([d1,d2, s, rms])
        #
        #     rmse_c = np.array(rmse_c,np.float32)
        #     idx = np.argmin(rmse_c[:,3])
        #     rmse_c_min = rmse_c[idx]
        #     print str(q)+' '+ str(rmse_c_min)
        #     # v1_rms_c_min = (rmse_c_min[2]/np.pi)*(np.arctan(x/rmse_c_min[0])-np.arctan(x/rmse_c_min[1]))
        #     # m_c =v1_rms_c_min+v2_rms_min_inv+sol[0]+sol[1]
        #
        #     depth.append(rmse_c_min[1])
        #     slip.append(rmse_c_min[2])
        #     varModel.append(rmse_c_min[3]**2)
        ################################################################################
        stdModel = np.sqrt(sum(varModel))
        slip[:] = [x * 1000.0 for x in slip]

        # fig = plt.figure()
        # plt.rcParams.update({'font.size': 22})
        # fig.set_size_inches(10,10)
        # ax = plt.Axes(fig, [0., 0., 1., 1.], )
        # ax=fig.add_subplot(111)
        #
        # cov = np.cov(slip, depth)
        # vals, vecs = eigsorted(cov)
        # theta = np.degrees(np.arctan2(*vecs[:,0][::-1]))
        # for nstd in xrange(1,4):        ####nstd is 1sigma, 2sigma, 3sigma
        #     w, h = 2 * nstd * np.sqrt(vals)
        #     ell = Ellipse(xy=(np.mean(slip), np.mean(depth)),
        #                 width=w, height=h,
        #                 angle=theta, color='black')
        #     ell.set_facecolor('none')
        #     ax.add_artist(ell)
        #
        # plt.scatter(slip,depth)
        # plt.scatter(rmse_c_min[2]*1000.,rmse_c_min[1], s=60, c='red', marker='*')
        # plt.ylabel('Depth (m)')
        # plt.xlabel('Slip (mm/yr)')
        # # plt.xlim(1,30)
        # # plt.ylim(1000,10000)
        # fig.savefig('depth_vs_slip.png')
        # plt.close()

        depth.append(rmse_inv_min[0])
        slip.append(rmse_inv_min[1])
        stdDepth = roundup(np.std(depth))
        stdSlip = math.ceil(np.std(slip))

        ################################################################################
        fig = plt.figure()
        plt.rcParams.update({'font.size': 22})
        fig.set_size_inches(20, 8)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        ##PLOT average and standard deviation##
        ax.plot(insarSpace, (transect - transect[0]) * 1000.0,
                'o',
                ms=1,
                mfc='Black',
                linewidth='0')

        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(insarSpace,
                    ((avgInSAR - avgInSAR[0]) - i * stdInSAR) * 1000.,
                    '-',
                    color='#DCDCDC',
                    alpha=0.2)  #,color='#DCDCDC')#'LightGrey')
            # ax.plot(xp, (avgInSAR-i*stdInSAR)*1000, '-',color='#DCDCDC',alpha=0.5)#,color='#DCDCDC')#'LightGrey')
        for i in np.arange(0.0, 1.01, 0.01):
            # ax.plot(xp, (avgInSAR+i*stdInSAR)*1000, '-',color='#DCDCDC',alpha=0.5)#'LightGrey')
            ax.plot(insarSpace,
                    ((avgInSAR - avgInSAR[0]) + i * stdInSAR) * 1000.,
                    '-',
                    color='#DCDCDC',
                    alpha=0.2)  #'LightGrey')

        # ax.plot(xp,avgInSAR*1000.0,'r-',label = 'Average velocity')
        ax.plot(insarSpace, (avgInSAR - avgInSAR[0]) * 1000.0,
                'r-',
                label='Average velocity')
        ax.plot(GPSdist,
                GPSvelPlot - GPSvelPlot[0],
                'b^',
                label='GPS velocity')

        ##PLOT average and standard deviation##

        label2 = 'Creep depth: 0 - ' + str(
            int(rmse_c_inv_min[1])
        ) + ' $\pm$' + str(stdDepth) + ' m - Creep rate: ' + str(
            int(abs(round(rmse_c_inv_min[2] * 1000., 2)))) + ' $\pm$' + str(
                int(stdSlip)) + ' mm/yr'

        # ax.plot(xp,(m_c_inv*1000.),'k-',label=label2)
        # ax.plot(insarGPSSpace,(m_c_inv*1000.),'k-',label=label2)
        ax.plot(insarGPSSpace, (m_c_inv * 1000.), 'k-', label=label2)

        #
        ax.legend(loc='lower left')
        plt.ylabel('Velocity (mm/yr)')
        plt.xlabel('Distance (km)')
        fig.savefig(directory + 'GPS_atan_best_' + str(rmse_c_inv_min[0]) +
                    '_' + str(rmse_c_inv_min[1]) + '.png')
        plt.close()
################################################################################

    elif model == 'both':
        for d in D:
            for s in V:
                v2 = sol[0] + ((s / pi) * arctan(x / d)) + sol[1]
                residual = v2 - avgInSAR
                rms = sqrt((sum((residual)**2, 0)) / len(transect))
                rmse.append([d, s, rms])
    #        print 'RMSE of '+ str(d)+' meters ' + str(s)+' m/yr: '+str(rms)

        rmse = array(rmse, float32)
        idx = argmin(rmse[:, 2])
        rmse_min = rmse[idx]
        print rmse_min
        v2_rms_min = ((rmse_min[1] / pi) * arctan(x / rmse_min[0]))
        ##Atan plus creep##
        x = x + offset
        rmse_c = []
        Q = arange(0.00001, rmse_min[0], 100)

        d1 = 0.01

        for d2 in D:
            for s in V:
                v1 = sol[0] + sol[1] + v2_rms_min + (
                    (s / pi) * (arctan(x / d1) - arctan(x / d2)))
                residual = v1 - avgInSAR
                rms = sqrt((sum((residual)**2, 0)) / len(transect))
                rmse_c.append([d1, d2, s, rms])

        rmse_c = array(rmse_c, float32)
        idx = argmin(rmse_c[:, 3])
        rmse_c_min = rmse_c[idx]
        print rmse_c_min
        v1_rms_min = (rmse_c_min[2] / pi) * (arctan(x / rmse_c_min[0]) -
                                             arctan(x / rmse_c_min[1]))

        m_c = v1_rms_min + v2_rms_min + sol[0] + sol[1]

        ####################
        fig = plt.figure()
        plt.rcParams.update({'font.size': 22})
        fig.set_size_inches(20, 8)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        ##PLOT average and standard deviation##
        ax.plot(xp, transect * 1000.0, 'o', ms=1, mfc='Black', linewidth='0')
        for i in arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR - i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #,color='#DCDCDC')#'LightGrey')
        for i in arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR + i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #'LightGrey')
        ax.plot(xp, avgInSAR * 1000.0, 'r-', label='Average velocity')
        label1 = 'Interseismic only: Locking depth: ' + str(int(
            rmse_min[0])) + ' meters - Slip rate: ' + str(
                int(abs(round(rmse_min[1] * 1000., 2)))) + ' mm/yr'

        ax.plot(xp, ((sol[0] + v2_rms_min + sol[1]) * 1000.),
                'b--',
                label=label1)

        label2 = 'Interseismic with creep: Creep depth: 0 - ' + str(
            int(rmse_c_min[1])) + ' meters - Creep rate: ' + str(
                int(abs(round(rmse_c_min[2] * 1000., 2)))) + ' mm/yr'

        ax.plot(xp, (m_c * 1000.), 'k-', label=label2)

        ax.legend(loc='lower left')
        plt.ylabel('Velocity (mm/yr)')
        plt.xlabel('Distance (km)')
        fig.savefig(directory + 'atan_best_' + str(rmse_c_min[0]) + '_' +
                    str(rmse_c_min[1]) + '.png')
        plt.close()

    else:
        print '''
            *******************************************

               Usage: atan_bestfit_creep.py directory model offset

                    directory: directory to transect.mat file
                    model: 'interseismic' or 'creep'
                    offset: if there is an offset of creep from the fault

            *******************************************
            '''
        sys.exit(1)
Exemple #46
0
    def removeBadSamples(self, mindist=7, maxdist=9999):
        '''
        Do the following refinement:
        (1) remove the samples who's minimum distance to facet boundary is smaller than mindist
        (2) remove the samples who's maximum distance to facet boundary is larger than mindist

        ## input
        mindist, maxdist
            as explained in the begining

        author: weiwei
        date: 20160623 flight to tokyo
        '''

        # ref = refine
        self.objsamplepnts_ref = np.ndarray(shape=(self.facets.shape[0], ),
                                            dtype=np.object)
        self.objsamplenrmls_ref = np.ndarray(shape=(self.facets.shape[0], ),
                                             dtype=np.object)
        self.facet2dbdries = []
        for i, faces in enumerate(self.facets):
            # print("removebadsample")
            # print(i,len(self.facets))
            facetp = None
            face0verts = self.objtrimesh.vertices[self.objtrimesh.faces[
                faces[0]]]
            facetmat = robotmath.rotmatfacet(self.facetnormals[i],
                                             face0verts[0], face0verts[1])
            # face samples
            samplepntsp = []
            for j, apnt in enumerate(self.objsamplepnts[i]):
                apntp = np.dot(facetmat, apnt)[:2]
                samplepntsp.append(apntp)
            # face boundaries
            for j, faceidx in enumerate(faces):
                vert0 = self.objtrimesh.vertices[self.objtrimesh.faces[faceidx]
                                                 [0]]
                vert1 = self.objtrimesh.vertices[self.objtrimesh.faces[faceidx]
                                                 [1]]
                vert2 = self.objtrimesh.vertices[self.objtrimesh.faces[faceidx]
                                                 [2]]
                vert0p = np.dot(facetmat, vert0)[:2]
                vert1p = np.dot(facetmat, vert1)[:2]
                vert2p = np.dot(facetmat, vert2)[:2]
                facep = Polygon([vert0p, vert1p, vert2p])
                if facetp is None:
                    facetp = facep
                else:
                    try:
                        facetp = facetp.union(facep)
                    except:
                        continue
            self.facet2dbdries.append(facetp)
            selectedele = []
            for j, apntp in enumerate(samplepntsp):
                try:
                    apntpnt = Point(apntp[0], apntp[1])
                    dbnds = []
                    dbnds.append(apntpnt.distance(facetp.exterior))
                    for fpinter in facetp.interiors:
                        dbnds.append(apntpnt.distance(fpinter))
                    dbnd = min(dbnds)
                    if dbnd < mindist or dbnd > maxdist:
                        pass
                    else:
                        selectedele.append(j)
                except:
                    pass
            self.objsamplepnts_ref[i] = np.asarray(
                [self.objsamplepnts[i][j] for j in selectedele])
            self.objsamplenrmls_ref[i] = np.asarray(
                [self.objsamplenrmls[i][j] for j in selectedele])
        self.facet2dbdries = np.array(self.facet2dbdries)
Exemple #47
0
def main(argv):
    try:
        directory = argv[0] + '/'
        model = argv[1]
        offset = float(argv[2])
        noit = int(argv[3])
    except:
        print '''
    *******************************************
       Usage: atan_bestfit_creep.py [directory] [model] [offset] [number of MC iterations]
            directory: directory to transect.mat file
            model: 'interseismic' or 'creep' or 'both'
            offset: if there is an offset of creep from the fault (in meters)
    *******************************************
    '''
        sys.exit(1)
    fileList = glob.glob(directory + '/*.mat')
    transectmat = scipy.io.loadmat(str(fileList[0]))
    # transect = transectmat['dataset'][0][0][1]
    transect = transectmat['dataset'][0][0][1]

    avgInSAR = np.nanmean(transect, axis=1)
    stdInSAR = np.nanstd(transect, axis=1)

    transect_dist = transectmat['dataset'][0][0][2]
    transect_lat_first = transectmat['dataset'][0][0][0][0][25]
    transect_lon_first = transectmat['dataset'][0][0][3][0][25]
    transect_first = Point(transect_lon_first, transect_lat_first)
    transect_lat_end = transectmat['dataset'][0][0][0][-1][25]
    transect_lon_end = transectmat['dataset'][0][0][3][-1][25]
    transect_end = Point(transect_lon_end, transect_lat_end)

    transect_middle_line = LineString([(transect_first), (transect_end)])
    fault_line = LineString([(26.835038363171353, 40.600641025641025),
                             (26.849744245524295, 40.605769230769226),
                             (26.86253196930946, 40.610256410256405),
                             (26.875319693094628, 40.61474358974359),
                             (26.884271099744243, 40.61730769230769),
                             (26.896419437340153, 40.621153846153845),
                             (26.90920716112532, 40.625),
                             (26.929028132992325, 40.631410256410255),
                             (26.93542199488491, 40.63525641025641),
                             (26.95012787723785, 40.64038461538461),
                             (26.96547314578005, 40.64487179487179),
                             (26.975703324808183, 40.648717948717945),
                             (26.982097186700766, 40.651282051282045),
                             (26.992966751918157, 40.65448717948718),
                             (27.005754475703323, 40.65961538461538),
                             (27.01918158567775, 40.66474358974359),
                             (27.028772378516624, 40.66858974358974),
                             (27.033887468030688, 40.67115384615384),
                             (27.04092071611253, 40.674358974358974),
                             (27.04923273657289, 40.67820512820513),
                             (27.058823529411764, 40.68141025641025),
                             (27.062020460358056, 40.682051282051276),
                             (27.072250639386187, 40.68589743589743),
                             (27.083759590792837, 40.68846153846154),
                             (27.09846547314578, 40.69102564102564),
                             (27.108056265984654, 40.69358974358974),
                             (27.121483375959077, 40.69615384615384),
                             (27.13107416879795, 40.69807692307692),
                             (27.141943734015346, 40.70128205128205),
                             (27.160485933503836, 40.70641025641025),
                             (27.186700767263424, 40.71282051282051),
                             (27.19757033248082, 40.715384615384615),
                             (27.20843989769821, 40.71730769230769),
                             (27.224424552429667, 40.72115384615385),
                             (27.239769820971865, 40.725641025641025),
                             (27.251918158567772, 40.72948717948718),
                             (27.26790281329923, 40.73461538461538),
                             (27.280690537084396, 40.73782051282051)])

    intersect = transect_middle_line.intersection(fault_line)
    #intersect = intersection(transect_middle_line,fault_line)
    dist = (transect_first.distance(intersect)) * 100000.0
    dist2 = (intersect.distance(transect_end)) * 100000.0

    ################################################################################
    ###GPS LOCATIONS
    # bgnt = [26.570, 40.932]
    # doku = [26.706, 40.739]
    # kvam = [26.871, 40.601]
    # kvam2 = [26.871, 40.601]
    # sev2 = [26.880, 40.396]
    # transect_lat = transectmat['dataset'][0][0][0][0]
    # transect_lon = transectmat['dataset'][0][0][3][0]
    # print(transect_lat[-1])
    # print(transect_lon[0])
    # transect_lat_end = transectmat['dataset'][0][0][0][-1]
    # transect_lon_end = transectmat['dataset'][0][0][3][-1]
    # print(transect_lat_end[0])
    # print(transect_lon_end[-1])
    # sys.exit()
    ################################################################################
    depth = []
    slip = []
    McMinRms = []
    varModel = []
    ##1-D space##
    x = np.linspace(-dist, dist2, num=len(avgInSAR), endpoint=True)

    xp = (x / 1000.)
    ################################################################################
    avgInSAR = avgInSAR - avgInSAR[0]
    transect = transect - transect[0]

    G = np.ones([len(transect), 2])
    G[:, 0] = xp
    G_inv = np.dot(np.linalg.inv(np.dot(G.T, G)), G.T)
    G_inv = np.array(G_inv, np.float32)
    sol = np.dot(G_inv, avgInSAR)
    k = np.dot(G, sol)

    V = np.arange(-0.000, -0.03, -0.0005)
    D = np.arange(0.0001, 6000., 100.)

    rmse_inv = []
    for d in D:
        for s in V:
            v2 = sol[0] + ((s / np.pi) * np.arctan(x / d)) + sol[1]
            residual = v2 - avgInSAR
            rms = np.sqrt((sum((residual)**2, 0)) / len(transect))
            rmse_inv.append([d, s, rms])

    rmse_inv = [[9000, -0.020, 0]]
    rmse_inv = np.array(rmse_inv, np.float32)
    idx = np.argmin(rmse_inv[:, 2])
    rmse_inv_min = rmse_inv[idx]
    print 'Inversion: ' + str(rmse_inv_min)
    v2_rms_min_inv = sol[0] + sol[1] + (
        (rmse_inv_min[1] / np.pi) * np.arctan(x / rmse_inv_min[0]))
    ################################################################################
    if model == 'interseismic':

        #######Monte-Carlo Error Bounds#################################################
        for q in xrange(noit):
            avgVel = []
            for i in xrange(len(avgInSAR)):
                avgVel.append(np.random.normal(avgInSAR[i], stdInSAR[i]))
            avgVel = np.array(avgVel)

            G = np.ones([len(avgVel), 2])
            G[:, 0] = xp
            G_inv = np.dot(np.linalg.inv(np.dot(G.T, G)), G.T)
            G_inv = np.array(G_inv, np.float32)
            sol = np.dot(G_inv, avgVel)
            k = np.dot(G, sol)

            # D = np.arange(1000.,10000.,100.)
            # V = np.arange(-0.001,-0.030,-0.0005)
            rmse_Mc = []
            for d in D:
                for s in V:
                    v2 = sol[0] + ((s / np.pi) * np.arctan(x / d)) + sol[1]
                    residual = v2 - avgVel
                    rms = np.sqrt((sum((residual)**2, 0)) / len(transect))
                    rmse_Mc.append([d, s, rms])

#######Monte-Carlo Error Bounds#################################################
            rmse_Mc_inv = np.array(rmse_Mc, np.float32)
            idx = np.argmin(rmse_Mc_inv[:, 2])
            rmse_Mc_inv_min = rmse_Mc_inv[idx]
            print str(q) + ' ' + str(rmse_Mc_inv_min)
            depth.append(rmse_Mc_inv_min[0])
            slip.append(rmse_Mc_inv_min[1])
            # r.append(rmse_min[2])
            varModel.append(rmse_Mc_inv_min[2]**2)
            McMinRms.append(rmse_Mc_inv_min[2])

        stdModel = np.sqrt(sum(varModel))
        slip[:] = [x * -1000.0 for x in slip]

        stdDepth = roundup(np.std(depth))
        stdSlip = math.ceil(np.std(slip))

        ################################################################################
        ##PLOT average and standard deviation##
        fig = plt.figure()
        plt.rcParams.update({'font.size': 24})
        fig.set_size_inches(12, 12)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        plt.scatter(slip, depth, c='blue', marker='.')
        plt.scatter(rmse_inv_min[1] * -1000.,
                    rmse_inv_min[0],
                    s=200,
                    c='red',
                    marker='*')

        depth.append(rmse_inv_min[0])
        slip.append(rmse_inv_min[1])
        McMinRms.append(rmse_inv_min[2] * 1000.)

        Xi, Yi = np.meshgrid(-V * 1000., D)
        z = np.array(np.reshape(rmse_Mc_inv[:, 2], (len(V), len(D))) * 1000.)
        # z = np.array(McMinRms)*1000
        # contours = plt.contour(Xi, Yi, z*1000.,int(np.amax(z*1000.)-np.amin(z*1000.)))
        # contours = plt.contour(Xi, Yi, z,500,colors='black')
        # contours = plt.contour(Xi, Yi, z,levels=(2.50,2.75,3,3.25,3.50,3.75,4,4.25,4.50,4.75,5),colors='black',linewidths=0.5)
        contours = plt.contour(Xi, Yi, z, 20, colors='black', linewidths=0.5)

        plt.clabel(contours, inline=True, fontsize=18, fmt='%.2f')

        cov = np.cov(slip, depth)
        vals, vecs = eigsorted(cov)
        theta = np.degrees(np.arctan2(*vecs[:, 0][::-1]))
        for nstd in xrange(1, 2):  ####nstd is 1sigma, 2sigma, 3sigma
            w, h = 2 * nstd * np.sqrt(vals)
            ell = Ellipse(xy=(np.mean(slip), np.mean(depth)),
                          width=w,
                          height=h,
                          angle=theta,
                          color='black')
            ell.set_facecolor('none')
            ell.set_linestyle('--')
            ell.set_linewidth('2')
            ax.add_artist(ell)
            ax.annotate('68%',
                        xy=((np.mean(slip)), (np.mean(depth) - (w / 2))),
                        xycoords='data')

        print 'Mean Slip ' + str(np.mean(slip))
        print 'Mean Depth ' + str(np.mean(depth))
        print 'W: ' + str(w)
        print 'H: ' + str(h)

        plt.ylabel('Locking Depth (m)')
        plt.xlabel('Slip (mm/yr)')
        # plt.xlim(0,20)
        # plt.ylim(1000,6000)
        fig.savefig('depth_vs_slip.png')
        plt.close()
        ################################################################################
        fig = plt.figure()
        plt.rcParams.update({'font.size': 20})
        fig.set_size_inches(20, 8)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        ax.plot(xp, transect * 1000.0, 'o', ms=1, mfc='Black', linewidth='0')
        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR - i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #,color='#DCDCDC')#'LightGrey')
        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR + i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #'LightGrey')
        ax.plot(xp, avgInSAR * 1000.0, 'r-', label='Average velocity')

        label1 = 'Interseismic only: Locking depth: ' + str(
            int(rmse_inv_min[0])) + '$\pm$' + str(
                int(stdDepth)) + 'm - Slip rate: ' + str(
                    int(abs(round(
                        rmse_inv_min[1] * 1000., 2)))) + '$\pm$' + str(
                            int(stdSlip)) + ' mm/yr'
        # label1 = 'D: '+str(int(rmse_inv_min[0]))+'$\pm$'+str(int(stdDepth))+'m - V: '+str(int(abs(round(rmse_inv_min[1]*1000.,2))))+'$\pm$'+str(int(stdSlip))+'mm/yr'
        ax.plot(xp, ((v2_rms_min_inv) * 1000.), 'b--', label=label1)
        ax.legend(loc='lower left')
        plt.ylabel('Velocity (mm/yr)')
        plt.xlabel('Distance (km)')
        fig.savefig(directory + 'atan_best_' + str(rmse_inv_min[0]) + '.png')
        plt.close()
################################################################################
    elif model == 'creep':
        x = x + offset
        rmse_c_inv = []
        # Q = np.arange(0.00001,rmse_inv[0],100)

        d1 = 0.01

        for d2 in D:
            for s in V:
                v1 = v2_rms_min_inv + ((s / np.pi) *
                                       (np.arctan(x / d1) - np.arctan(x / d2)))
                residual = v1 - avgInSAR
                rms = np.sqrt((sum((residual)**2, 0)) / len(transect))
                rmse_c_inv.append([d1, d2, s, rms])

        rmse_c_inv = np.array(rmse_c_inv, np.float32)
        idx = np.argmin(rmse_c_inv[:, 3])
        rmse_c_inv_min = rmse_c_inv[idx]
        print 'Creep Inversion: ' + str(rmse_c_inv_min)
        v1_rms_min = (rmse_c_inv_min[2] / np.pi) * (np.arctan(
            x / rmse_c_inv_min[0]) - np.arctan(x / rmse_c_inv_min[1]))
        m_c_inv = v1_rms_min + v2_rms_min_inv
        #######Monte-Carlo Error Bounds#################################################
        for q in xrange(noit):
            avgVel = []
            rmse_c = []
            for i in xrange(len(avgInSAR)):
                avgVel.append(np.random.normal(avgInSAR[i], stdInSAR[i]))
            avgVel = np.array(avgVel)

            for d2 in D:
                for s in V:
                    v1 = v2_rms_min_inv + (
                        (s / np.pi) * (np.arctan(x / d1) - np.arctan(x / d2)))
                    residual = v1 - avgVel
                    rms = np.sqrt((sum((residual)**2, 0)) / len(transect))
                    rmse_c.append([d1, d2, s, rms])

            rmse_c = np.array(rmse_c, np.float32)
            idx = np.argmin(rmse_c[:, 3])
            rmse_c_min = rmse_c[idx]
            print str(q) + ' ' + str(rmse_c_min)
            # v1_rms_c_min = (rmse_c_min[2]/np.pi)*(np.arctan(x/rmse_c_min[0])-np.arctan(x/rmse_c_min[1]))
            # m_c =v1_rms_c_min+v2_rms_min_inv+sol[0]+sol[1]

            depth.append(rmse_c_min[1])
            slip.append(rmse_c_min[2])
            varModel.append(rmse_c_min[3]**2)
            McMinRms.append(rmse_c_min[3])

        stdModel = np.sqrt(sum(varModel))
        slip[:] = [x * -1000.0 for x in slip]
        #######Monte-Carlo Error Bounds Plot STD Ellipses###############################
        fig = plt.figure()
        plt.rcParams.update({'font.size': 24})
        fig.set_size_inches(12, 12)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        plt.scatter(slip, depth, c='blue', marker='.')
        plt.scatter(rmse_c_inv_min[2] * -1000.,
                    rmse_c_inv_min[1],
                    s=200,
                    c='red',
                    marker='*')

        depth.append(rmse_c_inv_min[1])
        slip.append(rmse_c_inv_min[2] * -1000.)
        McMinRms.append(rmse_c_inv_min[3] * 1000.)

        Xi, Yi = np.meshgrid(-V * 1000., D)
        z = np.array(np.reshape(rmse_c[:, 3], (len(V), len(D))) * 1000.)
        # z = np.array(McMinRms)*1000
        # contours = plt.contour(Xi, Yi, z*1000.,int(np.amax(z*1000.)-np.amin(z*1000.)))
        # contours = plt.contour(Xi, Yi, z,500,colors='black')
        # contours = plt.contour(Xi, Yi, z,levels=(2.50,2.75,3,3.25,3.50,3.75,4,4.25,4.50,4.75,5),colors='black',linewidths=0.5)
        # contours = plt.contour(Xi, Yi, z,700,colors='black',linewidths=0.5)
        #
        # plt.clabel(contours, inline=True, fontsize=18,fmt='%.2f')

        cov = np.cov(slip, depth)
        vals, vecs = eigsorted(cov)
        theta = np.degrees(np.arctan2(*vecs[:, 0][::-1]))
        for nstd in xrange(1, 2):  ####nstd is 1sigma, 2sigma, 3sigma
            w, h = 2 * nstd * np.sqrt(vals)
            ell = Ellipse(xy=(np.mean(slip), np.mean(depth)),
                          width=w,
                          height=h,
                          angle=theta,
                          color='black')
            ell.set_facecolor('none')
            ell.set_linestyle('--')
            ell.set_linewidth('2')
            ax.add_artist(ell)
            ax.annotate('68%',
                        xy=((np.mean(slip)), (np.mean(depth) + (w / 2))),
                        xycoords='data')

        print 'Mean Slip ' + str(np.mean(slip))
        print 'Mean Depth ' + str(np.mean(depth))
        print 'W: ' + str(w)
        print 'H: ' + str(h)

        plt.ylabel('Depth (m)')
        plt.xlabel('Slip (mm/yr)')
        # plt.xlim(0,10)
        # plt.ylim(200,1200)
        fig.savefig('depth_vs_slip.png')
        plt.close()
        # fig = plt.figure()
        # plt.rcParams.update({'font.size': 22})
        # fig.set_size_inches(10,10)
        # ax = plt.Axes(fig, [0., 0., 1., 1.], )
        # ax=fig.add_subplot(111)
        #
        # plt.scatter(slip,depth)
        # plt.scatter(rmse_c_min[2]*1000.,rmse_c_min[1], s=200, c='red', marker='*')
        # plt.ylabel('Depth (m)')
        # plt.xlabel('Slip (mm/yr)')
        # # plt.xlim(1,30)
        # # plt.ylim(1000,10000)
        # fig.savefig('depth_vs_slip.png')
        # plt.close()
        #
        # depth.append(rmse_inv_min[0])
        # slip.append(rmse_inv_min[1])
        # stdDepth = roundup(np.std(depth))
        # stdSlip = math.ceil(np.std(slip))
        #
        # fig = plt.figure()
        # plt.rcParams.update({'font.size': 22})
        # fig.set_size_inches(15,15)
        # ax = plt.Axes(fig, [0., 0., 1., 1.], )
        # ax=fig.add_subplot(111)
        # InvRms = rmse_c[:,3]
        # z = np.array(np.reshape(InvRms,(len(V),len(D))))
        # Xi, Yi = np.meshgrid(-V*1000.,D)
        # # plt.scatter(-rmse_inv[:,1]*1000.,rmse_inv[:,0],c='blue')
        # plt.scatter(rmse_c_min[2]*1000.,rmse_c_min[1], s=500, c='red', marker='*')
        # contours = plt.contour(Xi, Yi, z*1000,int(np.amax(z*1000.)-np.amin(z*1000.)), colors='black')
        # # contours = plt.contour(Xi, Yi, z*1000.,int(np.amax(z*1000.)-np.amin(z*1000.)), cmap='RdGy')
        # plt.clabel(contours, inline=True, fontsize=10)
        # # CS = plt.contourf(Xi, Yi, z*1000.,50, cmap='RdGy')
        # # plt.colorbar();
        # plt.ylabel('Depth (m)')
        # plt.xlabel('Slip (mm/yr)')
        # # plt.xlim(-1,20)
        # # plt.ylim(-100,20000)
        # fig.savefig('Contour_depth_vs_slip.png')
        # plt.close()

        ####################################Error Ellipses##############################
        # fig = plt.figure()
        # plt.rcParams.update({'font.size': 22})
        # fig.set_size_inches(10,10)
        # ax = plt.Axes(fig, [0., 0., 1., 1.], )
        # ax=fig.add_subplot(111)
        #
        # cov = np.cov(slip, depth)
        # vals, vecs = eigsorted(cov)
        # theta = np.degrees(np.arctan2(*vecs[:,0][::-1]))
        # for nstd in xrange(1,4):        ####nstd is 1sigma, 2sigma, 3sigma
        #     w, h = 2 * nstd * np.sqrt(vals)
        #     ell = Ellipse(xy=(np.mean(slip), np.mean(depth)),
        #                 width=w, height=h,
        #                 angle=theta, color='black')
        #     ell.set_facecolor('none')
        #     ax.add_artist(ell)

        # plt.scatter(slip,depth)
        # plt.scatter(rmse_c_min[2]*1000.,rmse_c_min[1], s=60, c='red', marker='*')
        # plt.ylabel('Depth (m)')
        # plt.xlabel('Slip (mm/yr)')
        # # plt.xlim(1,30)
        # # plt.ylim(1000,10000)
        # fig.savefig('depth_vs_slip.png')
        # plt.close()
        ####################################Error Ellipses##############################
        ##Monte-Carlo Error Bounds Plot STD Ellipses####################################

        # depth.append(rmse_c_inv_min[0])
        # slip.append(rmse_c_inv_min[1])
        stdDepth = roundup(np.std(depth))
        stdSlip = math.ceil(np.std(slip))

        ################################################################################
        fig = plt.figure()
        plt.rcParams.update({'font.size': 22})
        fig.set_size_inches(20, 8)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        ax.plot(xp, transect * 1000.0, 'o', ms=1, mfc='Black', linewidth='0')
        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR - i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #,color='#DCDCDC')#'LightGrey')
        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR + i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #'LightGrey')
        ax.plot(xp, avgInSAR * 1000.0, 'r-', label='Average velocity')

        label2 = 'Creep depth: 0 - ' + str(
            int(rmse_c_inv_min[1])
        ) + '$\pm$' + str(stdDepth) + ' m - Creep rate: ' + str(
            int(abs(round(rmse_c_inv_min[2] * 1000., 2)))) + '$\pm$' + str(
                int(stdSlip)) + ' mm/yr'

        ax.plot(xp, (m_c_inv * 1000.), 'k-', label=label2)

        ax.legend(loc='lower left')
        plt.ylabel('Velocity (mm/yr)')
        plt.xlabel('Distance (km)')
        fig.savefig(directory + 'atan_best_' + str(rmse_c_inv_min[0]) + '_' +
                    str(rmse_c_inv_min[1]) + '.png')
        plt.close()
################################################################################

    elif model == 'both':
        transect = transect - transect[0]
        avgInSAR = avgInSAR - avgInSAR[0]
        rmse = []
        for d in D:
            for s in V:
                v2 = sol[0] + ((s / np.pi) * np.arctan(x / d)) + sol[1]
                residual = v2 - avgInSAR
                rms = np.sqrt((sum((residual)**2, 0)) / len(transect))
                rmse.append([d, s, rms])
                #        print 'RMSE of '+ str(d)+' meters ' + str(s)+' m/yr: '+str(rms)

        rmse = np.array(rmse, np.float32)
        idx = np.argmin(rmse[:, 2])
        rmse_min = rmse[idx]
        print rmse_min
        v2_rms_min = ((rmse_min[1] / np.pi) * np.arctan(x / rmse_min[0]))
        ##Atan plus creep##
        x = x + offset
        rmse_c = []
        #        Q = arange(0.00001,rmse_min[0],100)

        d1 = 0.01

        rmse_c = []
        for d2 in D:
            for s in V:
                v1 = sol[0] + sol[1] + v2_rms_min + (
                    (s / np.pi) * (np.arctan(x / d1) - np.arctan(x / d2)))
                residual = v1 - avgInSAR
                rms = np.sqrt((sum((residual)**2, 0)) / len(transect))
                rmse_c.append([d1, d2, s, rms])

        rmse_c = np.array(rmse_c, np.float32)
        idx = np.argmin(rmse_c[:, 3])
        rmse_c_min = rmse_c[idx]
        print rmse_c_min
        v1_rms_min = (rmse_c_min[2] / np.pi) * (np.arctan(x / rmse_c_min[0]) -
                                                np.arctan(x / rmse_c_min[1]))

        m_c = v1_rms_min + v2_rms_min + sol[0] + sol[1]

        ####################
        fig = plt.figure()
        plt.rcParams.update({'font.size': 22})
        fig.set_size_inches(20, 8)
        ax = plt.Axes(
            fig,
            [0., 0., 1., 1.],
        )
        ax = fig.add_subplot(111)

        ##PLOT average and standard deviation##
        ax.plot(xp, transect * 1000.0, 'o', ms=1, mfc='Black', linewidth='0')
        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR - i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #,color='#DCDCDC')#'LightGrey')
        for i in np.arange(0.0, 1.01, 0.01):
            ax.plot(xp, (avgInSAR + i * stdInSAR) * 1000,
                    '-',
                    color='#DCDCDC',
                    alpha=0.5)  #'LightGrey')
        ax.plot(xp, avgInSAR * 1000.0, 'r-', label='Average velocity')
        label1 = 'Interseismic only: Locking depth: ' + str(int(
            rmse_min[0])) + ' meters - Slip rate: ' + str(
                int(abs(round(rmse_min[1] * 1000., 2)))) + ' mm/yr'

        ax.plot(xp, ((sol[0] + v2_rms_min + sol[1]) * 1000.),
                'b--',
                label=label1)

        label2 = 'Interseismic with creep: Creep depth: 0 - ' + str(
            int(rmse_c_min[1])) + ' meters - Creep rate: ' + str(
                int(abs(round(rmse_c_min[2] * 1000., 2)))) + ' mm/yr'

        ax.plot(xp, (m_c * 1000.), 'k-', label=label2)

        ax.legend(loc='lower left')
        plt.ylabel('Velocity (mm/yr)')
        plt.xlabel('Distance (km)')
        fig.savefig(directory + 'atan_best_' + str(rmse_c_min[0]) + '_' +
                    str(rmse_c_min[1]) + '.png')
        plt.close()

    else:
        print '''
            *******************************************
               Usage: atan_bestfit_creep.py directory model offset
                    directory: directory to transect.mat file
                    model: 'interseismic' or 'creep'
                    offset: if there is an offset of creep from the fault
            *******************************************
            '''
        sys.exit(1)
for point in predictionPoints["features"]:
    road = linkki.loc[linkki["TIENUMERO"] ==
                      point["properties"]["roadAddress"]["roadNumber"], :]
    road = road.loc[road["AJOSUUNTA"] < 4, :]
    road = road.reset_index()

    p = Point(point["geometry"]["coordinates"][0],
              point["geometry"]["coordinates"][1])

    pred2[point["id"]] = {}

    i = 0
    nearest_linkki = -1
    d = float('Inf')
    for r in road.geometry:
        if p.distance(r) < d:
            d = p.distance(r)
            nearest_linkki = i
        i += 1

    segment_id = road.loc[nearest_linkki, "SEGM_ID"]

    tmp = width.loc[width['SEGM_ID'] == segment_id, "ARVO"]
    if len(tmp) > 0:
        pred2[point["id"]]["road_width"] = tmp.values[0]
    else:
        pred2[point["id"]]["road_width"] = "N/A"

    tmp = speed.loc[speed['SEGM_ID'] == segment_id, "ARVO"]
    if len(tmp) > 0:
        pred2[point["id"]]["speed_limit"] = tmp.values[0]
Exemple #49
0
class VisibilityManager(object):
    def __init__(self):
        self.method = 0
        self.points = []
        self.segments = []
        self.rays = []
        self.lines = []
        self.polygon = []
        self.eyePosition = Point(950, 500)

    def addSegment(self, x1, y1, x2, y2):
        self.points.append(Point(x1, y1))
        self.points.append(Point(x2, y2))
        self.segments.append(LineString([(x1, y1), (x2, y2)]))

    def addWorldBoundaries(self, width, height):
        self.addSegment(1, 1, width, 1)
        self.addSegment(width, 1, width, height)
        self.addSegment(width, height, 1, height)
        self.addSegment(1, height, 1, 1)

    def castRays(self):
        self.rays.clear()
        if self.method == 0:
            for point in self.points:
                line = LineString([(self.eyePosition.x, self.eyePosition.y),
                                   (point.x, point.y)])
                self.rays.append(line)
        if self.method == 1:
            for degree in range(0, 360):
                rads = radians(degree)
                magnitude = 10000
                x = cos(rads) * magnitude
                y = sin(rads) * magnitude
                self.rays.append(
                    LineString([(self.eyePosition.x, self.eyePosition.y),
                                (x, y)]))
        #self.polygon = DrawPolygon(vertexList)

    def calculatePolygon(self):
        self.polygon.clear()
        for ray in self.rays:
            intersections = []
            for segment in self.segments:
                if ray.intersects(segment):
                    intersections.append(
                        (self.eyePosition.distance(ray.intersection(segment)),
                         ray.intersection(segment)))
            self.polygon.append(sorted(intersections)[0][1])

    def addLines(self):
        self.lines.clear()
        if False:  #TODO debug
            for ray in self.rays:
                self.lines.append(
                    DrawLine(
                        ray.coords[0][0],
                        ray.coords[0][1],
                        ray.coords[1][0],
                        ray.coords[1][1],
                    ))
        for segment in self.segments:
            self.lines.append(
                DrawLine(
                    segment.coords[0][0],
                    segment.coords[0][1],
                    segment.coords[1][0],
                    segment.coords[1][1],
                ))

        if True:  #TODO debug
            for point in self.polygon:
                line = DrawLine(point.x - 5, point.y - 5, point.x + 5,
                                point.y + 5)
                line.changeColor(255, 0, 0)
                self.lines.append(line)

    def update(self):
        self.castRays()
        self.calculatePolygon()
        self.addLines()

    def draw(self):
        for line in self.lines:
            line.draw()

    def on_mouse_motion(self, x, y):
        self.eyePosition = Point(x, y)
Exemple #50
0
print(point1)
print(point3D)
print(type(point1))

point_coords = point1.coords
type(point_coords)

xy = point_coords.xy
x = point1.x
y = point1.y

print(xy)
print(x)
print(y)

points_dist = point1.distance(point2)
print('Distance between the points is {0:.2f} decimal degrees'.format(
    points_dist))

line = LineString([point1, point2, point3])
line2 = LineString([(2.2, 4.2), (7.2, -25.1), (9.26, -2.456)])
print(line)

lxy = line.xy
print(lxy)

line_x = lxy[0]
line_y = line.xy[1]
print(line_x)
print(line_y)
def extendAllMergedLines(dirPathLines, vectorMask, epsgValue):
    """
    extendAllMergedLines.
       
    :param dirPathLines: (text) path
	:param vectorMask: (text) path
	:param epsgValue: (text) epsg value
    :returns: OpenCV version.
    """

    crsEpsgId = {'init': 'epsg:' + str(epsgValue)}

    crutils.printLogMsg(crglobals.OK_MSG + 'Path Lines: %s' % (dirPathLines))
    crutils.printLogMsg(crglobals.OK_MSG + 'Mask: %s' % (vectorMask))
    crutils.printLogMsg(crglobals.OK_MSG + 'EPSG: %s' % (epsgValue))

    #input merged line file
    fileNameMergedLines = 'merged_lines.shp'
    mergedLinesGeoDataFrame = gpd.GeoDataFrame.from_file(
        os.path.join(
            dirPathLines,
            fileNameMergedLines))  #dirPathLines+'/'+fileNameMergedLines)
    #input mask polygon file
    boundsMaskGeoDataFrame = gpd.GeoDataFrame.from_file(vectorMask)

    longLinesArray = []
    idLongLinesArray = []
    for x in range(0, len(mergedLinesGeoDataFrame.geometry)):
        linea_bx = (list(mergedLinesGeoDataFrame.geometry[x].coords))
        extrapoledLine = getExtrapoledLine(*linea_bx[-2:])
        idLongLinesArray.append(x)
        longLinesArray.append(extrapoledLine)

    dataFrameLongLines = pd.DataFrame({'id': idLongLinesArray})
    longLinesGeoDataFrame = gpd.GeoDataFrame(dataFrameLongLines,
                                             crs=crsEpsgId,
                                             geometry=longLinesArray)
    longLinesFileName = os.path.join(
        dirPathLines,
        'merged_lines_long.shp')  #dirPathLines+'/'+'merged_lines_long.shp'
    longLinesGeoDataFrame.to_file(driver='ESRI Shapefile',
                                  filename=longLinesFileName)
    crutils.printLogMsg(crglobals.DONE_MSG + 'Generated long lines !')

    #######################################################################################
    #Get the convex hull lines
    convexHullFromBoundsMask = boundsMaskGeoDataFrame.convex_hull.iloc[0]
    x, y = convexHullFromBoundsMask.exterior.xy
    pointsConvexHullFromBoundsMaskArray = np.array(list(zip(x, y)))
    minBBoxRect = imboundrect.minimum_bounding_rectangle(
        pointsConvexHullFromBoundsMaskArray)
    polygonOMBB = Polygon(
        [minBBoxRect[0], minBBoxRect[1], minBBoxRect[2], minBBoxRect[3]])
    #######################################################################################
    #cut lines by ombb
    #update applying buffer. it fix some strange bug at some corner lines
    geoDataFrameLineCuttedByBoxBuffer = (longLinesGeoDataFrame.intersection(
        polygonOMBB.buffer(20)))
    dataFrameLineCuttedByBoxDf = pd.DataFrame({
        'id':
        idLongLinesArray,
        'len':
        geoDataFrameLineCuttedByBoxBuffer.length
    })
    geoDataFrameLinesCuttedByBox = gpd.GeoDataFrame(
        dataFrameLineCuttedByBoxDf,
        crs=crsEpsgId,
        geometry=geoDataFrameLineCuttedByBoxBuffer)
    mergedLinesCuttedByOMBBPolygonFile = os.path.join(
        dirPathLines, 'merged_lines_long_cut_ombb.shp'
    )  #dirPathLines+'/'+'merged_lines_long_cut_ombb.shp'
    geoDataFrameLinesCuttedByBox.to_file(
        driver='ESRI Shapefile', filename=mergedLinesCuttedByOMBBPolygonFile)
    crutils.printLogMsg(crglobals.DONE_MSG + 'Cut long lines by OMBB bounds !')

    #######################################################################################
    projectDistance = 1
    #enumerate lines in spatial order
    angle = crutils.getAzimuth(
        (geoDataFrameLinesCuttedByBox.geometry[0].coords[0][0]),
        (geoDataFrameLinesCuttedByBox.geometry[0].coords[0][1]),
        (geoDataFrameLinesCuttedByBox.geometry[0].coords[1][0]),
        (geoDataFrameLinesCuttedByBox.geometry[0].coords[1][1]))
    anglep = (angle + 270)
    #search for the closest geometry line to temporal point
    temporalXpoint = (geoDataFrameLinesCuttedByBox.geometry[0].centroid.x
                      ) + np.sin(np.deg2rad(anglep)) * 5000
    temporalYpoint = (geoDataFrameLinesCuttedByBox.geometry[0].centroid.y
                      ) + np.cos(np.deg2rad(anglep)) * 5000
    temporalExternalPoint = Point((temporalXpoint, temporalYpoint))
    crutils.printLogMsg(crglobals.DONE_MSG + 'Generate a Temporal Point')
    #print(temporalExternalPoint)
    tmpLineDistance = []
    for i in range(len(geoDataFrameLinesCuttedByBox)):
        distanceCalculated = (temporalExternalPoint.distance(
            geoDataFrameLinesCuttedByBox.geometry[i].centroid))
        tmpLineDistance.append(distanceCalculated)
        #print("i: %s -> distanceCalculated: %s -> %s" % (str(i), str(distanceCalculated) , str(geoDataFrameLinesCuttedByBox.geometry[i])   ))
    minelem = np.argmin(tmpLineDistance)
    crutils.printLogMsg(crglobals.DONE_MSG + "Found a closest line id: %s" %
                        (str(minelem)))
    #######################################################################################
    #Calc Distances using the closest line found
    xp = (geoDataFrameLinesCuttedByBox.geometry[minelem].centroid.x) + np.sin(
        np.deg2rad(anglep)) * projectDistance
    yp = (geoDataFrameLinesCuttedByBox.geometry[minelem].centroid.y) + np.cos(
        np.deg2rad(anglep)) * projectDistance
    externalPoint = Point((xp, yp))
    #print(externalPoint)

    geoDistance = []
    index = []

    for i in range(len(geoDataFrameLinesCuttedByBox)):
        #print('-> %s' % ( str( i)))
        distanceCalculated = (externalPoint.distance(
            geoDataFrameLinesCuttedByBox.geometry[i].centroid))
        geoDistance.append(distanceCalculated)
        #print(distanceCalculated)
        index.append(i)

    dataFrameLineCuttedByBoxBuffer = pd.DataFrame({
        'len':
        geoDataFrameLineCuttedByBoxBuffer.length,
        'geo_dist':
        geoDistance,
        'idx':
        index
    })
    geoDataFrameLinesCuttedByBox = gpd.GeoDataFrame(
        dataFrameLineCuttedByBoxBuffer,
        crs=crsEpsgId,
        geometry=geoDataFrameLineCuttedByBoxBuffer)

    mergedLongLinesCuttedByOMBwDistFileName = os.path.join(
        dirPathLines, 'merged_lines_long_cut_ombb_wdist.shp'
    )  #dirPathLines+'/'+'merged_lines_long_cut_ombb_wdist.shp'
    geoDataFrameLinesCuttedByBox.to_file(
        driver='ESRI Shapefile',
        filename=mergedLongLinesCuttedByOMBwDistFileName)

    #######################################################################################
    #######################################################################################
    ############## FILTERING LINES LOOKING FOR CANDIDATES #################################
    #######################################################################################
    #######################################################################################

    sortedDistance = np.argsort(geoDistance).astype('int')
    idByGeo = [x for _, x in sorted(zip(sortedDistance, index))]

    newObjDistancesSorted = np.sort(geoDistance)

    #Removing Adjacents and lines duplicates
    newObjDistancesSorted = crutils.removeAdjacentsInArray(
        newObjDistancesSorted)

    #print('====== new distances sorted =====')
    #print(newObjDistancesSorted)
    crutils.printLogMsg(crglobals.DONE_MSG + 'Candidate lines: %s ' %
                        str(len(newObjDistancesSorted)))

    ##Removing Closing Lines
    #pairsdistances = zip([0]+newObjDistancesSorted, newObjDistancesSorted)

    #TODO: distancesFiltered
    #distancesFiltered = [pair[1] for pair in pairsdistances if abs(pair[0]-pair[1]) >=0.5 ]
    ###########################################
    ## OTRO APPROACH TO FIND DISTANCES

    groups, current_group, first = [], [], newObjDistancesSorted[0]
    for item in newObjDistancesSorted:
        # Check if this element falls under the current group
        if item - first <= 1.3:
            current_group.append(item)
        else:
            # If it doesn't, create a new group and add old to the result
            groups.append(current_group[:])
            current_group, first = [item], item
        # Add the last group which was being gathered to the result
    groups.append(current_group[:])
    distancesFiltered = [np.max(item) for item in groups]

    #iter 1 removing proximal lines
    pairsdistances2 = zip([0] + distancesFiltered, distancesFiltered)
    distancesFiltered = [
        pair[1] for pair in pairsdistances2 if abs(pair[0] - pair[1]) >= 0.4
    ]

    #iter 2 removing proximal lines
    pairsdistances3 = zip([0] + distancesFiltered, distancesFiltered)
    distancesFiltered = [
        pair[1] for pair in pairsdistances3 if abs(pair[0] - pair[1]) >= 0.9
    ]  #>=0.8  -preff: 0.9

    ######################################3
    #print('====== distances filtered =====')
    #print(distancesFiltered)

    crutils.printLogMsg(crglobals.DONE_MSG +
                        'Resulting lines: %s ' % str(len(distancesFiltered)))

    #TODO: final

    #cut final lines by mask
    dataFrameCandidateLines = (geoDataFrameLinesCuttedByBox.intersection(
        boundsMaskGeoDataFrame.geometry.iloc[0]))

    candidateLinesFileName = os.path.join(
        dirPathLines,
        'candidate_lines.shp')  #dirPathLines+'/'+'candidate_lines.shp'

    dataFrameLineCuttedByMask = pd.DataFrame({
        'distance':
        dataFrameCandidateLines.length,
        'geo_dist':
        geoDistance,
        'idx':
        index
    })

    geoDataFrameLineCuttedByMask = gpd.GeoDataFrame(
        dataFrameLineCuttedByMask,
        crs=crsEpsgId,
        geometry=dataFrameCandidateLines)
    geoDataFrameLineCuttedByMask.to_file(driver='ESRI Shapefile',
                                         filename=candidateLinesFileName)

    #####################################
    ## NEW CROPROWS - GEOMETRY SEARCH
    #####################################

    #save candidateLinesbuffer
    candidateLinesBufferFileName = os.path.join(dirPathLines,
                                                'candidate_lines_buffer.shp')
    candidateLinesBuffer = geoDataFrameLineCuttedByMask.buffer(0.3)
    s = candidateLinesBuffer

    #ADDING FEATURE 3-9-2018
    #DISOLVE OVERLAPPING BUFFER POLYGONS
    #https://gis.stackexchange.com/questions/271733/geopandas-dissolve-overlapping-polygons/271735
    overlap_matrix = s.apply(lambda x: s.overlaps(x)).values.astype(int)
    n, ids = connected_components(overlap_matrix)
    df = gpd.GeoDataFrame({'geometry': s, 'group': ids}, crs=crsEpsgId)
    res = df.dissolve(by='group')
    res.to_file(driver='ESRI Shapefile', filename=candidateLinesBufferFileName)

    candidateLinesBufferCentroidFileName = os.path.join(
        dirPathLines, 'candidate_lines_buffer_centroid.shp')
    points = res.copy()
    points.geometry = res['geometry'].centroid
    points.crs = res.crs
    points.head()
    points.to_file(driver='ESRI Shapefile',
                   filename=candidateLinesBufferCentroidFileName)

    df_lines = dataFrameCandidateLines.geometry
    df_points = points.geometry
    #print(len(df_points))
    #print(len(df_lines))

    idClosestLineArr = []
    #find the closest candidate line to point
    for x in range(0, len(df_points)):
        minDistancePointLine = df_lines.distance(df_points[x]).min()
        allDistanceToLines = df_lines.distance(df_points[x])
        idClosestLine = np.where(allDistanceToLines == minDistancePointLine)[0]
        idClosestLineArr.append(idClosestLine[0])
        #print('centroid point: %s - id closest line: ' % (str(x)) , (str(idClosestLine))   )

    #print(idClosestLineArr)
    selcr = []
    for x in range(0, len(df_lines)):
        selcr = df_lines[idClosestLineArr]

    crf = 0
    geoidcr = []
    croprowLength = []

    for y in range(0, len(selcr)):
        #print(selcr.geometry.iloc[y])
        #print(selcr.geometry.iloc[y].length)
        croprowLength.append(selcr.geometry.iloc[y].length)
        geoidcr.append(crf)
        crf = crf + 1

    dataFrameCr = pd.DataFrame({
        'geometry': selcr,
        'crlen': croprowLength,
        'idg': geoidcr,
        'crg': 'Generated by Crop Rows Generator v1'
    })
    geoDataFrameCropRows = gpd.GeoDataFrame(dataFrameCr, crs=crsEpsgId)
    cropRowsFileNameByGeom = os.path.join(dirPathLines, 'croprows_lines.shp')
    geoDataFrameCropRows.to_file(driver='ESRI Shapefile',
                                 filename=str(cropRowsFileNameByGeom))

    ##EXPORT CROP ROWS RESULTS TO WGS84 SHP AND KML FORMATS
    crsExportID = {'init': 'epsg:' + str(crglobals.EXPORT_EPSG)}
    exportCropRowsShapeFile = os.path.join(
        os.path.dirname(os.path.dirname(dirPathLines)), crglobals.EXPORTDIR,
        'croprows_wgs84.shp')
    geoDataFrameCropRowsWGS84 = gpd.GeoDataFrame(dataFrameCr, crs=crsEpsgId)
    geoDataFrameCropRowsWGS84 = geoDataFrameCropRowsWGS84.to_crs(crsExportID)
    geoDataFrameCropRowsWGS84.to_file(driver='ESRI Shapefile',
                                      filename=str(exportCropRowsShapeFile))
    exportCropRowsShapeFileKML = os.path.join(
        os.path.dirname(os.path.dirname(dirPathLines)), crglobals.EXPORTDIR,
        'croprows_wgs84.kml')
    geoDataFrameCropRowsWGS84.to_file(driver='kml',
                                      filename=str(exportCropRowsShapeFileKML))
    crutils.printLogMsg(
        crglobals.DONE_MSG +
        'Exported Resulting Crop Rows to KML and SHP format in WGS84 CRS')

    cropRowsBufferFileName = os.path.join(dirPathLines,
                                          'croprows_lines_buffer.shp')
    cropRowsLinesBuffer = geoDataFrameCropRows.buffer(0.3)
    cropRowsLinesBufferGeoData = gpd.GeoDataFrame(crs=crsEpsgId,
                                                  geometry=cropRowsLinesBuffer)
    cropRowsLinesBufferGeoData.to_file(driver='ESRI Shapefile',
                                       filename=str(cropRowsBufferFileName))

    #####################################
    ## OLD CROPROWS - STAT SEARCH
    #####################################
    getIndexes = lambda x, xs: [
        i for (y, i) in zip(xs, range(len(xs))) if x == y
    ]
    cuttedLineArray = []
    #look for
    k = []
    flagCounter3 = 0
    for x in distancesFiltered:
        #print(distancesFiltered[i])
        #print(getIndexes(distancesFiltered[i],newobjdistances))
        k.append(getIndexes(distancesFiltered[flagCounter3], geoDistance)[0])
        flagCounter3 = flagCounter3 + 1

    #Reindex lines filtered
    index2 = []
    flagCounter2 = 0
    m = []
    j = 0
    croprowLength = []
    for x in k:
        m.append(dataFrameCandidateLines[k[j]])
        index2.append(flagCounter2)
        #line len for each geometry
        croprowLength.append(m[j].length)
        flagCounter2 += 1
        j = j + 1

    #print('index2:')
    #print(index2)
    #print('k')
    #print(k)
    #print('m')
    #print(m)

    sortdistance2 = np.argsort(distancesFiltered).astype('int')
    idByGeo2 = [x for _, x in sorted(zip(sortdistance2, index2))]

    #print('idByGeo2')
    #print(idByGeo2)

    crutils.printLogMsg(crglobals.DONE_MSG + 'Re-indexing candidate lines !')

    #Fix distances substracting projectDistance
    arrayDistances = np.array(distancesFiltered)
    #fixdist = arrayDistances - projectDistance
    crutils.printLogMsg(crglobals.DONE_MSG + 'Distances fixed !')

    #print(croprowLength)

    #fixdist
    dataFrameFixedLines = pd.DataFrame({
        'id': k,
        'geo_dist': arrayDistances,
        'idgeo': idByGeo2,
        'crlen': croprowLength
    })
    #dataFrameFixedLines = pd.DataFrame({  })
    geoDataFrameFixedLines = gpd.GeoDataFrame(dataFrameFixedLines,
                                              crs=crsEpsgId,
                                              geometry=m)
    geoDataFrameFixedLines.dropna()

    crutils.printLogMsg(crglobals.DONE_MSG + 'Result lines generated !')

    cropRowsLinesFileName = os.path.join(dirPathLines,
                                         'croprows_lines_stat.shp')
    geoDataFrameFixedLines.to_file(driver='ESRI Shapefile',
                                   filename=str(cropRowsLinesFileName))

    crutils.printLogMsg(crglobals.DONE_MSG +
                        'Writing file with resulting lines : %s ' %
                        (cropRowsLinesFileName))

    #saveResultXMLFile(cropRowsLinesFileName)
    resultingFiles = [
        cropRowsFileNameByGeom, cropRowsBufferFileName,
        exportCropRowsShapeFile, exportCropRowsShapeFileKML,
        str(epsgValue),
        str(len(newObjDistancesSorted)),
        str(len(distancesFiltered)), vectorMask
    ]
    saveResultXMLFile(resultingFiles)
    #saveResultXMLFile(cropRowsFileNameByGeom,cropRowsBufferFileName,exportCropRowsShapeFile,exportCropRowsShapeFileKML)

    #####################################

    return 1
def LineGroupingFromSHP(abs_file_path, MINIMALDIST):
    

    sf = shapefile.Reader(abs_file_path)

#------------------------ read lines from shapefile ---------------------------
    chains=[]
    for geom in sf.shapeRecords():

        chain=[(geom.shape.points[0][0],geom.shape.points[0][1]),(geom.shape.points[1][0],geom.shape.points[1][1])]
        chains.append(chain)

#------------------------------------------------------------------------------
    
    
#------------------ group lines & fix unconnected vertices --------------------
    closed_chains=[]
    
    k=1
    RADIUS=MINIMALDIST
    
    print '#-----------------------------'
    print 'k=', k, 'RADIUS=', RADIUS
    print 'len(chains)', len(chains)           
    print 'len(closed_chains)', len(closed_chains)
    
    while (k<=5 and len(chains)>0):
        
    
        
        if len(chains)==1:
            pt11=Point(chains[0][0][0], chains[0][0][1])
            pt12=Point(chains[0][-1][0],chains[0][-1][1])
            
            if pt11.distance(pt12)<=RADIUS:
                chains.pop(0)
                l1=LineString([chain1[0], chain1[1]])
                l2=LineString([chain1[-1], chain1[-2]])
                new_pts1=fix_disjoint_vertices(l1, l2)
                new_chain=chain1[1:-1]+new_pts1
                closed_chains.append(new_chain)
                break
            else:
                print 'wawawa'
                k=k+1
                RADIUS=RADIUS+MINIMALDIST
                
                print '#-----------------------------'
                print 'k=', k, 'RADIUS=', RADIUS
                print 'len(chains)', len(chains)           
                print 'len(closed_chains)', len(closed_chains)
                
                break
        
        L=len(chains)
        for i in range(0, len(chains)-1):
            
            chain1=chains[i]
            pt11=Point(chain1[0][0], chain1[0][1])
            pt12=Point(chain1[-1][0],chain1[-1][1])
            
            if pt11.distance(pt12)<=RADIUS:
                chains.pop(i)
                l1=LineString([chain1[0], chain1[1]])
                l2=LineString([chain1[-1], chain1[-2]])
                new_pts1=fix_disjoint_vertices(l1, l2)
                new_chain=chain1[1:-1]+new_pts1
                closed_chains.append(new_chain)
                break
            
            
            for j in range(i+1, len(chains)+1):
                
                if j==len(chains):
                    break
                
                chain2=chains[j]
                pt21=Point(chain2[0][0], chain2[0][1])
                pt22=Point(chain2[-1][0],chain2[-1][1])
                
                if pt11.distance(pt21)<=RADIUS:
                    l1=LineString([chain1[0], chain1[1]])
                    l2=LineString([chain2[0], chain2[1]])
                    new_pts1=fix_disjoint_vertices(l1, l2)
                    
                    if pt12.distance(pt22)<=RADIUS:
                        # closed
                        l1=LineString([chain1[-1], chain1[-2]])
                        l2=LineString([chain2[-1], chain2[-2]])
                        new_pts2=fix_disjoint_vertices(l1, l2)
                        
                        chains.pop(j)
                        chains.pop(i)
                        chain1.reverse()
                        new_chain=new_pts2+chain1[1:-1]+new_pts1+chain2[1:-1]
                        closed_chains.append(new_chain)
                        break
                    
                    else:
                        chains.pop(j)
                        chains.pop(i)
                        chain1.reverse()
                        new_chain=chain1[0:-1]+new_pts1+chain2[1:]
                        chains.append(new_chain)
                        break
                    
                elif pt11.distance(pt22)<=RADIUS:
                    l1=LineString([chain1[0], chain1[1]])
                    l2=LineString([chain2[-1], chain2[-2]])
                    new_pts1=fix_disjoint_vertices(l1, l2)
                    
                    if pt12.distance(pt21)<=RADIUS:
                        # closed
                        l1=LineString([chain1[-1], chain1[-2]])
                        l2=LineString([chain2[0], chain2[1]])
                        new_pts2=fix_disjoint_vertices(l1, l2)
                        
                        chains.pop(j)
                        chains.pop(i)
                        new_chain=new_pts1+chain1[1:-1]+new_pts2+chain2[1:-1]
                        closed_chains.append(new_chain)
                        break
                    
                    else:
                        chains.pop(j)
                        chains.pop(i)
                        new_chain=chain2[0:-1]+new_pts1+chain1[1:]
                        chains.append(new_chain)
                        break
                    
                elif pt12.distance(pt21)<=RADIUS:
                    l1=LineString([chain1[-1], chain1[-2]])
                    l2=LineString([chain2[0], chain2[1]])
                    new_pts1=fix_disjoint_vertices(l1, l2)
            
                    chains.pop(j)
                    chains.pop(i)
                    new_chain=chain1[0:-1]+new_pts1+chain2[1:]
                    chains.append(new_chain)
                    break
                    
                elif pt12.distance(pt22)<=RADIUS:
                    l1=LineString([chain1[-1], chain1[-2]])
                    l2=LineString([chain2[-1], chain2[-2]])
                    new_pts1=fix_disjoint_vertices(l1, l2)
                
                    chains.pop(j)
                    chains.pop(i)
                    chain2.reverse()
                    new_chain=chain1[0:-1]+new_pts1+chain2[1:]
                    chains.append(new_chain)
                    break
                
                else:
                    continue
                
            if j==L:
                continue
            else:
                break
            
        if i==L-2 and j==L:
            print 'hahaha'
            k=k+1
            RADIUS=RADIUS+MINIMALDIST
            
            print '#-----------------------------'
            print 'k=', k, 'RADIUS=', RADIUS
            print 'len(chains)', len(chains)           
            print 'len(closed_chains)', len(closed_chains)
    
        else:
            continue
    
    
    
    
    print '#-----------------------------'
    print 'len(chains)', len(chains)           
    print 'len(closed_chains)', len(closed_chains)      


#------------------------------------------------------------------------------             
    

#------------------------------------------------------------------------------    
    groups_P=[]
    
    for i in range(0, len(closed_chains)):
        
        if len(closed_chains[i])>2:
            if Polygon(closed_chains[i]).is_valid==True:
                ply=Polygon(closed_chains[i])
                new_ply=polygon.orient(ply, sign=1.0)
                groups_P.append(list(new_ply.exterior.coords)[0:-1])
      
            else:
                ply=Polygon(closed_chains[i]).buffer(0).exterior.coords
                new_ply=polygon.orient(ply, sign=1.0)
                groups_P.append(list(new_ply.exterior.coords)[0:-1])
        else:
            print 'closed_chains ',i,' only has two points'

    
    return groups_P
Exemple #53
0
    def build_interp(self, constraint_lines, long_step, constant_long_disc):
        """
        Build interpolation, add points and segments

        @param constraint_lines <[ConstraintLine]>: list of constraint lines
        @param long_step <float>: longitudinal space step
        @param constant_long_disc <bool>
        """
        nb_pts_inter = 0
        self.build_initial_profiles()

        # LOOP ON ZONES (between 2 consecutive cross-sections)
        logger.info("~> Building mesh per zone and then per bed")

        for i, (prev_section, next_section) in enumerate(
                zip(self.section_seq, self.section_seq[1:])):
            logger.debug("> Zone n°{} : between {} and {}".format(
                i, prev_section, next_section))

            if constant_long_disc:
                nb_pts_inter = prev_section.compute_nb_pts_inter(
                    next_section, long_step)
                Xp_adm_list = np.linspace(0.0, 1.0, num=nb_pts_inter + 2)[1:-1]

            # Looking for common limits between cross-sections
            common_limits_id = prev_section.common_limits(
                next_section.limits.keys())
            logger.debug("Common limits: {}".format(list(common_limits_id)))

            if len(common_limits_id) < 2:
                raise TatooineException(
                    "No interpolation in the interval %i, between %s and %s (%i common limits)"
                    % (i, prev_section, next_section, len(common_limits_id)))

            else:
                first_bed = True
                # LOOP ON BEDS
                for j, (id1, id2) in enumerate(
                        zip(common_limits_id, common_limits_id[1:])):
                    pt_list_L1 = []
                    pt_list_L2 = []

                    logger.debug("Bed {}-{}".format(id1, id2))

                    # Extraction of cross-section portions (= beds)
                    bed_1 = prev_section.extract_bed(id1, id2)
                    bed_2 = next_section.extract_bed(id1, id2)

                    # Curvilinear abscissa along constraint lines
                    (Xp_profil1_L1,
                     Xp_profil1_L2) = prev_section.get_Xt_lines(id1, id2)
                    (Xp_profil2_L1,
                     Xp_profil2_L2) = next_section.get_Xt_lines(id1, id2)
                    dXp_L1 = Xp_profil2_L1 - Xp_profil1_L1
                    dXp_L2 = Xp_profil2_L2 - Xp_profil1_L2

                    if dXp_L1 < 0:
                        raise TatooineException(
                            "The constraint line {} is not oriented correctly".
                            format(id1))
                    if dXp_L2 < 0:
                        raise TatooineException(
                            "The constraint line {} is not oriented correctly".
                            format(id2))

                    if not constant_long_disc:
                        nb_pts_inter = math.ceil(
                            min(dXp_L1, dXp_L2) / long_step) - 1
                        Xp_adm_list = np.linspace(0.0,
                                                  1.0,
                                                  num=nb_pts_inter + 2)[1:-1]

                    L1_coord_int = constraint_lines[
                        id1].coord_sampling_along_line(Xp_profil1_L1,
                                                       Xp_profil2_L1,
                                                       Xp_adm_list)
                    L2_coord_int = constraint_lines[
                        id2].coord_sampling_along_line(Xp_profil1_L2,
                                                       Xp_profil2_L2,
                                                       Xp_adm_list)

                    # LOOP ON INTERMEDIATE CROSS-SECTIONS
                    for k in range(nb_pts_inter):
                        Xp = Xp_adm_list[k]
                        P1 = Point(tuple(L1_coord_int[k]))
                        P2 = Point(tuple(L2_coord_int[k]))

                        if self.nb_pts_lat is None:
                            nb_pts_lat = math.ceil(
                                P1.distance(P2) / self.lat_step) + 1
                        else:
                            nb_pts_lat = self.nb_pts_lat
                        array = bed_1.interp_coord_linear(
                            bed_2, Xp, nb_pts_lat)
                        bed_int = Bed(array, ['Xt', 'xt'])
                        bed_int.move_between_targets(P1, P2)
                        coord_int = bed_int.array[[
                            'X', 'Y', 'xt', 'Xt_upstream', 'Xt_downstream'
                        ]]  # Ignore `Xt`
                        pt_list_L1.append(self.i_pt + 1)

                        if not first_bed:
                            # ignore first point because the constraint line was already considered
                            coord_int = coord_int[1:]

                        self.add_points(coord_int, i, Xp, j)

                        pt_list_L2.append(self.i_pt)

                    pt_list_L2 = np.array(
                        [prev_section.get_limit_by_id(id2)['id_pt']] +
                        pt_list_L2 +
                        [next_section.get_limit_by_id(id2)['id_pt']])
                    self.add_segments_from_node_list(pt_list_L2)

                    if first_bed:
                        pt_list_L1 = np.array(
                            [prev_section.get_limit_by_id(id1)['id_pt']] +
                            pt_list_L1 +
                            [next_section.get_limit_by_id(id1)['id_pt']])
                        self.add_segments_from_node_list(pt_list_L1)
                        first_bed = False
def contour_intersection(contour_x, contour_y, line_x, line_y, do_plot_in_case_of_failure=False):
    """
    Computes the intersection point between two lines.
    The first line should be the environmental contour and the second line a
    straight line. If there are more multiple intersections the point that has
    the longest distance to the origin is returned (assuming the origin is at
    the first point of the straight line).
    Parameters
    ----------
    contour_x : ndarray of floats,
        Coordinates in the first dimension of the contour.
    contour_y : ndarray of floats
        Coordinates in the second dimension of the contour.
    line_x : ndarray of floats
        Coordinates in the first dimension of the straight line.
    line_y : ndarray of floats
        Coordaintes in the second dimension of the straight line.
    do_plot_in_case_of_failure : Boolean,
        if True a plot of the two lines for which no intersection could be
        found is shown.
    Returns
    -------
    x : float
        Intersection coordinate in the first dimension.
    y : float
        Intersection coordinate in the second dimension.
    """

    point_list_l1 = list()
    for x,y in zip(contour_x, contour_y):
        point = Point(x, y)
        point_list_l1.append(point)
    contour = LineString(point_list_l1)

    point_list_l2 = list()
    for x,y in zip(line_x, line_y):
        point = Point(x, y)
        point_list_l2.append(point)
    line2 = LineString(point_list_l2)

    intersection = contour.intersection(line2)
    if type(intersection) is Point:
        return intersection.x, intersection.y
    if type(intersection) is MultiPoint:
        if len(intersection.geoms) > 0:
            print(str(len(intersection.geoms)) + ' intersections were found.'
                  + ' Using the intersection that is the farthest'
                  + ' away from the origin.')
        origin = Point(line_x[0], line_y[0])
        for i, p in enumerate(intersection.geoms):
            if i == 0:
                inter_x = p.x
                inter_y = p.y
                longest_distance = origin.distance(p)
            else:
                if origin.distance(p) > longest_distance:
                    inter_x = p.x
                    inter_y = p.y
        return inter_x, inter_y
    else:
        print('The result is: ' + str(intersection))
        warnings.warn('No point of intersection could be found. '
                      'Returning (nan, nan).', UserWarning)
        if do_plot_in_case_of_failure:
            fig = plt.figure(figsize=(5, 5), dpi=150)
            fig.add_subplot(111)
            plt.plot(contour_x, contour_y, 'b-')
            plt.plot(line_x, line_y, 'r-')
            plt.show()
        return np.nan, np.nan
Exemple #55
0
def trova_spazi_parziali_da_frontiere_v2(plan_o, lista_pixel_frontiere, immagine_cluster, labels):

	#devo flippare le coordinate di lista_pixel_frontiere
	lista_pixel = [] #mi tengo una copia
	for p in lista_pixel_frontiere:
		a = copy.deepcopy(p)
		lista_pixel.append(a)
	
	for p in lista_pixel_frontiere:
		p.y = immagine_cluster.shape[0]-1 - p.y
	for p in lista_pixel_frontiere:
		altezza = len(immagine_cluster)
		y_p2 = - p.x+ altezza
		x_p2 = - p.y+ altezza
		p.x = x_p2
		p.y = y_p2
	
	counter=collections.Counter(labels)
	stanze_gt_con_frontiera = []
	ordinamento_frontiere = []
	
	for spazio in plan_o.spazi:
		for celletta in spazio.cells:

			num_pixel = 0
			cluster = None
			prima_volta = True
			c = []
			for pixel in lista_pixel_frontiere:
				posizione = Point(pixel.x,pixel.y)
				if posizione.distance(celletta.cella) == 0:
					#significa che quel pixel e' all'interno
					#print "ci entro????????? mai "
					num_pixel+=1
					c.append(pixel.cluster)#added
					#if prima_volta == True:
						#cluster = pixel.cluster
						#prima_volta = False
			#print "-------------------<<<<<<<<<<<<", num_pixel	
			
			#--------aggiungo parte dove faccio media del cluster dominante #nel caso questa cosa va aggiunta anche dalle altre parti
			max = 0
			for elem in c:
				if c.count(elem) > max:
					max = c.count(elem)
					cluster = elem			
			#---------------------		

			n_elementi_cluster = counter[cluster]
			#print "il numero di n_elementi_cluster", n_elementi_cluster
			#if pixel_diversi >= 0.3 * n_elementi_cluster and n_elementi_cluster > 0: #la copertura deve essere il 30% di quel cluster
			if num_pixel >= 15: #elimino un po' di rumore
				celletta.set_vedo_frontiera(True)
				celletta.set_celletta_out(False)
				celletta.set_celletta_parziale(True)	
				celletta.set_cluster_frontiera(cluster)
			else:
				celletta.set_vedo_frontiera(False)
				celletta.set_celletta_out(False)
				celletta.set_celletta_parziale(False)
				celletta.set_cluster_frontiera(None) #se la cella non ha una frontiera associata allora non esiste il cluster frontiera

	del lista_pixel_frontiere
	#rifaccio tornare la lista come prima
	lista_pixel_frontiere = []
	for p in lista_pixel:
		lista_pixel_frontiere.append(p) 
Exemple #56
0
def dist_to_shape(pp, s):
    p = Point(pp)
    d = p.distance(s)
    if (d == 0.0):
        d = -p.distance(s.exterior)
    return d
Exemple #57
0
                    carundcent = Point((centx, c2[1]))
                    carupcent = Point((centx, c1[1]))
                    """ 차의 중앙 지점과 겹치는 곳이 있으면 그곳이 차의 위치 """
                    for val in vals:
                        if int(x[-1]) == val:
                            cnt += 1
                            if l_poly.intersects(carcent):
                                l_cnt += 1
                            if r_poly.intersects(carcent):
                                r_cnt += 1
                            if c_poly.intersects(carcent):
                                c_cnt += 1
                                if c_cnt > 1: c_cnt = 1

                                # 앞 차량과의 거리계산
                                pl = carundcent.distance(Point(whalf - 5, 720))
                                dist = (pl * 1.8 /
                                        (next_frame[6] -
                                         next_frame[2])) * 180 / np.pi
                                dist = round(map(dist, 20, 40, 10, 70), 2)

                                # 앞 차량의 Detection Box----------------------------
                                cv2.rectangle(frame, c1, c2, blue, 1)

                                t_size = cv2.getTextSize(label, font2, 1, 1)[0]
                                c2 = c1[0] + t_size[0], c1[1] - t_size[1]

                                cv2.rectangle(frame, c1, c2, blue, -1)
                                #---------------------------------------------------

                                cv2.line(frame, (centx, c1[1]),
Exemple #58
0
def calc_gz(GL_FILE='',WIDTH_FILE='',BASIN_FILE='',VEL_FILE='',POINT_FILE='',region='',dist=0,N=0,vel_thr=0):
	#-- read the grounding lines
	df_gl = gpd.read_file(GL_FILE)
	#-- read widths
	df_w = gpd.read_file(WIDTH_FILE)
	#-- read the basin file
	basins = gpd.read_file(BASIN_FILE)
	idx = basins.index[basins['NAME']==region]
	#-- get polygon
	poly = basins['geometry'][idx[0]]

	#-- add a 5km buffer to find the corresponding GLs
	region_poly = poly.buffer(5e3)

	lines = []
	dates = []
	for i in range(len(df_gl)):
		#-- extract geometry to see if it's in region of interest
		ll = df_gl['geometry'][i]
		if ll.intersects(region_poly):
			lines.append(ll)
			dates.append(df_gl['FILENAME'][i].split("_")[2])

	#-- get width lines
	ws = []
	for i in range(len(df_w)):
		ws.append(df_w['geometry'][i])
	widths = MultiLineString(ws)
	
	#-- merge all lines into linestring
	lm = ops.linemerge(lines)

	#-- also create a polygon to represent the GZ with a small buffer (10cm)
	gz_file = os.path.join(os.path.dirname(GL_FILE),'GZ_{0}.shp'.format(region))
	if os.path.exists(gz_file):
		print('Reading GZ polygon from file.')
		gz_df = gpd.read_file(gz_file)
		gz_poly = []
		for i in range(len(gz_df)):
			gz_poly.append(gz_df['geometry'][i])
		gz_poly = MultiPolygon(gz_poly)
	else:
		print('Creating GZ polygon.')
		ep = lm.buffer(1e-1)
		#-- get the boundary of the polygon containing all lines to make new polygon of just the envelope
		gz_poly = []
		for ip in ep:
			x,y = ip.exterior.coords.xy
			gz_poly.append(Polygon(zip(x,y)))
		#-- save the error polygon
		#-- first make DataFrame
		df = {'REGION':[],'center_x':[],'center_y':[]}
		out_geo = []
		for p in gz_poly:
			#-- note the width can be calculated from area and perimeter
			w = (-p.length+np.sqrt(p.length**2 - 16*p.area))/4
			l = p.length/2 - w
			print(w,l)
			if (w > 1 and l > 1):
				df['REGION'].append(region)
				x,y = p.centroid.coords.xy
				df['center_x'].append(x[0])
				df['center_y'].append(y[0])
				out_geo.append(p)
		out_gdf = gpd.GeoDataFrame(df,geometry=out_geo,crs=df_gl.crs)
		out_gdf.to_file(gz_file,driver='ESRI Shapefile')

	#-- read velocity field
	vel_fid = nc.Dataset(VEL_FILE,'r')
	x = vel_fid['x'][:]
	y = vel_fid['y'][:]
	vx = vel_fid['VX'][:]
	vy = vel_fid['VY'][:]
	#-- also read lat and lon
	# vel_lat = vel_fid['lat'][:]
	# vel_lon = vel_fid['lon'][:]
	vel_fid.close()
	
	#-- select the points for the calculation of GZ width
	#-- in order to generate points, we randomly draw a line from the
	#-- mutliline, and then draw a random distance to go along the line to get
	#-- a coordinate. We repeat until the specified number of points is reached
	#-- first we will the array with the given points in POINTS_FILE, and fill
	#-- the rest randomly.
	xlist = np.zeros(N)
	ylist = np.zeros(N)
	gz = np.zeros(N)
	date1_list = [None]*N
	date2_list = [None]*N
	vel_transects = {}
	cn_transects = {}
	
	#-- read given points
	df_pts = gpd.read_file(POINT_FILE)
	#-- reproject to the projection of GL data
	df_pts = df_pts.to_crs(df_gl.crs)
	N_given = len(df_pts)
	print("{0:d} points prescribed out a total of {1:d}".format(N_given,N))
	for i in range(N_given):
		xx,yy = df_pts['geometry'][i].coords.xy
		xlist[i] = float(xx[0])
		ylist[i] = float(yy[0])
	random.seed(13)
	for i in range(N_given,N):
		#-- draw a random index for line along multilines
		ind_line = random.randrange(0,len(lm))
		rand_line = lm[ind_line]
		rand_dist = random.uniform(0, rand_line.length)
		rand_pt = rand_line.interpolate(rand_dist)
		xx,yy = rand_pt.coords.xy
		xlist[i] = float(xx[0])
		ylist[i] = float(yy[0])
	
	#-- make special polygons that require a different transect length
	plong = Polygon([[-1175399.2293594137,-1124281.6845298712],
					[-1166026.3695500833,-1132757.1428680948],
					[-1194493.9384390623,-1149957.337730963],
					[-1198332.822509906,-1146467.4431211061]])
	#-- loop through points and calculate GZ
	for i,(xi,yi) in enumerate(zip(xlist,ylist)):
		if i%100 == 0:
			print(i)
		
		#-- A) velocity based approach
		#-- get list of distances to get a list of closest points
		#- For a given coordinate, get the flow angle and then the intersecting line
		ii = np.argmin(np.abs(x - xi))
		jj = np.argmin(np.abs(y - yi))

		#-- chech if velocity is above required threshold
		vel_mag = np.sqrt(vy[jj,ii]**2 + vx[jj,ii]**2)
		if vel_mag > vel_thr:
			#-- find flow angle
			ang = np.arctan(vy[jj,ii]/vx[jj,ii])
			#-- Now constuct a line of a given length, centered at the 
			#-- chosen coordinates, with the angle above
			if Point(xi,yi).within(plong):
				dx,dy = 25e3*np.cos(ang),25e3*np.sin(ang)
			else:
				dx,dy = dist*np.cos(ang),dist*np.sin(ang)
			vel_transects[i] = LineString([[x[ii]-dx,y[jj]-dy],[x[ii],y[jj]],[x[ii]+dx,y[jj]+dy]])
			#-- get intersection length
			vel_int = vel_transects[i].intersection(gz_poly)
			gz[i] = vel_int.length

			#-- get dates
			pt0 = vel_int.interpolate(0,normalized=True)
			pt1 = vel_int.interpolate(1,normalized=True)
			for l in range(len(lines)):
				if lines[l].distance(pt1) < 0.2:
					date1_list[i] = dates[l]
				elif lines[l].distance(pt0) < 0.2:
					date2_list[i] = dates[l]
		else:
			#-- B) retrieve width from QGIS centerline width calculation
			#-- first get the closest line to the point
			po = Point(xi,yi)
			wdist = np.zeros(len(widths))
			for wi in range(len(widths)):
				wdist[wi] = widths[wi].distance(po)
			ind_w = np.argmin(wdist)

			cn_transects[i] = widths[ind_w]
			#-- get length
			gz[i] = cn_transects[i].length	
			#-- also get the corresponding dates
			pt0 = cn_transects[i].interpolate(0,normalized=True)
			pt1 = cn_transects[i].interpolate(1,normalized=True)
			for l in range(len(lines)):
				if lines[l].distance(pt1) < 0.2:
					date1_list[i] = dates[l]
				elif lines[l].distance(pt0) < 0.2:
					date2_list[i] = dates[l]

	#-- write grounding zone widths to file
	outfile = os.path.join(os.path.dirname(GL_FILE),'GZ_widths-hybrid_{0}.csv'.format(region))
	outfid = open(outfile,'w')
	outfid.write('X (m),Y (m),width (km),date1,date2\n')
	for i in range(N):
		outfid.write('{0:.6f},{1:.6f},{2:.3f},{3},{4}\n'.\
		format(xlist[i],ylist[i],gz[i]/1e3,date1_list[i],date2_list[i]))
	outfid.close()

	#-- plot a sample of points to check the grounding zones
	fig = plt.figure(1,figsize=(10,8))
	ax = fig.add_subplot(111)
	pp = PolygonPatch(poly,alpha=0.3,fc='lawngreen',ec='lawngreen',zorder=1)
	ax.add_patch(pp)
	for il in lines:
		xs,ys = il.coords.xy
		ax.plot(xs,ys,linewidth=0.4,alpha=0.8,color='k',zorder=2)
	for i in range(30):
		if i < N_given:
			ip = copy(i)
			if i == 0:
				plot_pts = [Point(xlist[ip],ylist[ip])]
			else:
				plot_pts.append(Point(xlist[ip],ylist[ip]))
		else:
			ip = random.randrange(0,N)
			#-- while distance to any of the previous points is less than 20km,
			#-- keep trying new indices (doesn't apply to 1st point)
			pt = Point(xlist[ip],ylist[ip])
			while (pt.distance(MultiPoint(plot_pts)) < 12e3):
				ip = random.randrange(0,N)
				pt = Point(xlist[ip],ylist[ip])
			#-- now we can ensure the points aren't overlapping
			print("minimum distance to previous points: ", pt.distance(MultiPoint(plot_pts)))
			plot_pts.append(pt)
		#-- Now plot the transect for the given index
		if ip in vel_transects.keys():
			lx,ly = vel_transects[ip].coords.xy
			ax.plot(lx,ly,linewidth=2.0,alpha=1.0,color='red',zorder=3)
		elif ip in cn_transects.keys():
			lx,ly = cn_transects[ip].coords.xy
			ax.plot(lx,ly,linewidth=2.0,alpha=1.0,color='darkorange',zorder=3)
		ax.text(xlist[ip]+5e3,ylist[ip]+5e3,'{0:.1f}km'.format(gz[ip]/1e3),color='darkred',\
			fontsize=6,fontweight='bold',bbox=dict(facecolor='mistyrose', alpha=0.5))
		# ax.scatter(xlist[ip],ylist[ip],s=10,color='darkorchid',zorder=4,alpha=0.5)
	ax.plot([],[],color='red',label="Velocity-based Intersect")
	ax.plot([],[],color='darkorange',label="Centerline-based Intersect")
	ax.get_xaxis().set_ticks([])
	ax.get_yaxis().set_ticks([])
	ax.set_title("Grounding Zone Width for {0}".format(region))
	plt.legend()
	plt.tight_layout()
	plt.savefig(outfile.replace('.csv','.pdf'),format='PDF')
	plt.close(fig)
Exemple #59
0
import sys
fn = sys.argv[1]
path = sys.argv[2]

def Pixel2World ( geoMatrix, i , j ): #import matrix that represents pixels in the input image 
    ulX = geoMatrix[0]
    ulY = geoMatrix[3]
    xDist = geoMatrix[1]
    yDist = geoMatrix[5]
    rtnX = geoMatrix[2]
    rtnY = geoMatrix[4]
    return(1.0 * i * xDist  + ulX, -1.0 * j * xDist + ulY)
ds8 = gdal.Open(path+'8band/'+'8band_'+fn+'.tif') #assign 8-band geoTIFF image to var ds8
ds3 = gdal.Open(path+'3band/'+'3band_'+fn+'.tif') #assign 3-band geoTIFF image to var ds3
geoTrans = ds8.GetGeoTransform()
with open(path + 'vectorData/geoJson/'+fn+'_Geo.geojson','r') as f: #import geoJSON file with building footprint represented as polygon
    js = json.load(f)
    dist = np.zeros((ds8.RasterXSize, ds8.RasterYSize))
    for i in range(ds8.RasterXSize): #for each pixel along x axis of matrix
        for j in range(ds8.RasterYSize): #for each pixel along y axis of matrix 
            point = Point(Pixel2World( geoTrans, i , j )) #point = pixel at (i, j)
            pd = -100000.0
            for feature in js['features']: #for each annotated feature in the geoJSON file (the buildings that are represented as polygons)
                polygon = shape(feature['geometry']) #a polygon is a feature contained in the field 'geometry'
                newpd = point.distance(polygon.boundary) #newpd = the distance of the point (pixel) from the polygon 
                 if False == polygon.contains(point): #if the building polygon does not contain the pixel
                     newpd = -1.0 * newpd #pixel is an exterior pixel and has a negative distance 
                 if newpd > pd : #if the distance of the point from the polygon is greater than pd
                     pd = newpd #set pd to the distance of the point from the polygon 
             dist[i,j] = pd
np.save(path+'CosmiQ_distance/'+fn+'.distance',dist)
Exemple #60
0
    def cut_line_features_at_points(self, geometry, ratio=0.5, points=None):
        """
        cut_line_features_at_points

        :type geometry: shapely.geometry.*
        :type ratio: float, default: 0.5
        :type points: [shapely.geometry.Point], default None
        :return: [shapely.geometry.LineString]
        """
        self.__assert_all_geometry_type(geometry)

        new_geometries = []

        if isinstance(geometry, (LineString, LinearRing)):

            new_geometries = list(geometry.coords)
            if ratio is not None:
                points_coords = [
                    geometry.interpolate(ratio, normalized=True).xy
                ]
                new_geometries[1:1] = points_coords

            elif points is not None:

                start_point = Point(new_geometries[0])
                points_on_line_mapping = {
                    point.coords[0]: start_point.distance(point)
                    for point in points if start_point.distance(point) > 0
                    and start_point.distance(point) < geometry.length
                    and geometry.envelope.intersects(point.envelope)
                }

                if len(points_on_line_mapping) > 0:
                    points_on_line_mapping_sorted = OrderedDict(
                        sorted(points_on_line_mapping.items(),
                               key=lambda x: x[1]))
                    points_coords = [
                        point for point in points_on_line_mapping_sorted
                    ]
                    new_geometries[1:1] = points_coords
                    new_geometries = [
                        LineString(pair) for pair in zip(
                            new_geometries[:-1], new_geometries[1:])
                    ]
                else:
                    new_geometries = [geometry]

        elif isinstance(geometry, MultiLineString):
            geometries = geometry.geoms
            for geometry in geometries:
                lines = list(
                    self.cut_line_features_at_points(geometry, ratio,
                                                     points).geoms)
                new_geometries.extend(lines)

        elif isinstance(geometry, (Point, MultiPoint)):
            new_geometries = None

        else:
            geom_lines = self.convert_geometry_to_simple_linestrings(
                geometry).geoms
            for geom in geom_lines:
                lines = self.cut_line_features_at_points(geom, ratio,
                                                         points).geoms
                new_geometries.extend(lines)

        return MultiLineString(new_geometries)