Ejemplo n.º 1
0
def optimize_rotate_UTMconvert(angle,df,base):
	import numpy as np
	import math
	import utm
	from HCF_functions import rotate_xy
	import matplotlib.pyplot as plt

	sum = 0
	pick = np.isfinite(df['gps_lat'])
	k = utm.from_latlon(df.loc[base]['y_working'],df.loc[base]['x_working'])
	x0 = k[0]
	y0 = k[1]
	for index, row in df[pick].iterrows():
		i = utm.from_latlon(row['y_working'], row['x_working'])
		j = utm.from_latlon(row['gps_lat'], row['gps_lon'])
		gpsX, gpsY = j[0], j[1]
		newX, newY = rotate_xy(i[0],i[1],x0,y0, angle * math.pi / 180)

		a = newX - gpsX
		b = newY - gpsY
		c = np.square(a) + np.square(b)
		misfit = np.sqrt(c)
		misfit = np.sqrt(c) / row['gps_horiz_acc']
		sum = sum + misfit
		# print angle, index, misfit
	plt.scatter(df['x_working'], df['y_working'], color='orange')
	print angle, sum
	return sum
Ejemplo n.º 2
0
 def test_force_zone(self):
     # test forcing zone ranges
     # NYC should be zone 18T
     self.assert_zone_equal(UTM.from_latlon(40.71435, -74.00597, 19, 'T'), 19, 'T')
     self.assert_zone_equal(UTM.from_latlon(40.71435, -74.00597, 17, 'T'), 17, 'T')
     self.assert_zone_equal(UTM.from_latlon(40.71435, -74.00597, 18, 'u'), 18, 'U')
     self.assert_zone_equal(UTM.from_latlon(40.71435, -74.00597, 18, 'S'), 18, 'S')
Ejemplo n.º 3
0
def global_to_local(origin, lon, lat, heading=0.0):
    """
    Converts from the WGS84 lon/lat global frame into the local
    frame in meters.  Note that heading is in degrees!

    @param origin (lon, lat, heading)
    @return (x, y, theta) in WGS84
    """
    # Create local transformation frame.
    import utm
    ox, oy, zone, hemi = utm.from_latlon(origin[1], origin[0])
    o_theta = (90 - origin[2]) * (math.pi/180.0)

    # Convert global point into local frame.
    px, py, zone, hemi = utm.from_latlon(lat, lon)
    p_theta = math.pi/2.0 - heading

    # Translate and rotate point to origin.
    point = shapely.geometry.Point(px - ox, py - oy)
    point = shapely.affinity.rotate(point, -o_theta,
                                    origin=ORIGIN, use_radians=True)
    theta = p_theta - o_theta

    # Return transformed point.
    return point.x, point.y, theta
Ejemplo n.º 4
0
    def test_from_latlon_range_checks(self):
        '''from_latlon should fail with out-of-bounds input'''
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, -100, 0)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, -80.1, 0)
        for i in range(-8000, 8400):
            UTM.from_latlon(i / 100.0, 0)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 84.1, 0)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 100, 0)

        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 0, -300)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 0, -180.1)
        for i in range(-18000, 18000):
            UTM.from_latlon(0, i / 100.0)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 0, 180.1)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 0, 300)

        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, -100, -300)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 100, -300)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, -100, 300)
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 100, 300)

        # test forcing zone ranges
        # NYC should be zone 18T
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 40.71435, -74.00597, 70, 'T')
        self.assertRaises(UTM.OutOfRangeError, UTM.from_latlon, 40.71435, -74.00597, 18, 'A')
Ejemplo n.º 5
0
def circle(latlng_origin=None, pid_origin=None, radius=15):
    """
    Returns a validator function for a Panorama. which Returns True if
    the Panorama is inside a circle of the radius r around the center
    point latlng_0.
    :param latlng_0: tuple (lat, lng) - center GPS
    :param r: float - radius in meters
    :return: validator(panorama) - given Panorama it returns True
             if the Panorama is inside the circle
    """
    if latlng_origin:
        (easting, northing, z_number, z_letter) = utm.from_latlon(latlng_origin[0], latlng_origin[1])
    elif pid_origin:
        latlng_origin = Panorama(pid_origin).getGPS()
        (easting, northing, z_number, z_letter) = utm.from_latlon(latlng_origin[0], latlng_origin[1])
    else:
        raise ValueError("One of the arguments 'latlang_origin' or 'pid_origin' must be given.")

    def isClose(p):
        ll = p.getGPS()
        (est, nth, zn, zl) = utm.from_latlon(ll[0], ll[1], force_zone_number=z_number)
        x, y = est-easting, nth-northing
        d = sqrt(x**2+y**2)
        return d < radius
    return isClose
Ejemplo n.º 6
0
def box(latlng_origin=None, pid_origin=None, width=15, height=15):
    """
    Returns a validator function of Panorama that returns True is
    the Panorama is inside a box of the size w,h around center
    point latlng_0.
    :param latlng_origin: tuple (lat, lng) - center GPS
    :param w: float - width in meters
    :param h: float - height in meters
    :return: validator(Panorama) - given a Panorama it returns True
             if the panorama is inside the box
    """
    if latlng_origin:
        (easting, northing, z_number, z_letter) = utm.from_latlon(latlng_origin[0], latlng_origin[1])
    elif pid_origin:
        latlng_origin = Panorama(pid_origin).getGPS()
        (easting, northing, z_number, z_letter) = utm.from_latlon(latlng_origin[0], latlng_origin[1])
    else:
        raise ValueError("One of the arguments 'latlang_origin' or 'pid_origin' must be given.")


    def isClose(p):
        ll = p.getGPS()
        (est, nth, zn, zl) = utm.from_latlon(ll[0], ll[1], force_zone_number=z_number)
        x, y = est-easting, nth-northing
        return abs(x) < w/2 and abs(y) < h/2
    return isClose
Ejemplo n.º 7
0
def distance_2d(lon1, lat1, lon2, lat2):
    ym1, xm1, _, _ = utm.from_latlon(lat1, lon1)
    ym2, xm2, _, _ = utm.from_latlon(lat2, lon2)

    arr1 = numpy.array((xm1, ym1))
    arr2 = numpy.array((xm2, ym2))

    dist = numpy.linalg.norm(arr1-arr2)
    return dist
Ejemplo n.º 8
0
def latlon_to_meters(coord_list, origin):
    # Convert coord_list to UTM
    coord_list[:] = [utm.from_latlon(*coord) for coord in coord_list]

    # Convert origin to UTM
    origin = utm.from_latlon(*origin)

    # Convert coord_list to meters relative to origin
    coord_list[:] = [(coord[0] - origin[0], coord[1] - origin[1])
                     for coord in coord_list]
def getPolygon(row,coordinateDir,coordinateFileName):
    polygonData = openCSVFile(coordinateDir+coordinateFileName)   
    UTM1 = utm.from_latlon(float(polygonData[row][0]),float(polygonData[row][1]))
    UTM1 = [UTM1[0],UTM1[1]]
    UTM2 = utm.from_latlon(float(polygonData[row][2]),float(polygonData[row][3]))
    UTM2 = [UTM2[0],UTM2[1]]
    UTM3 = utm.from_latlon(float(polygonData[row][4]),float(polygonData[row][5]))
    UTM3 = [UTM3[0],UTM3[1]]
    UTM4 = utm.from_latlon(float(polygonData[row][6]),float(polygonData[row][7]))
    UTM4 = [UTM4[0],UTM4[1]]
    return Polygon([(UTM1[0],UTM1[1]),(UTM2[0],UTM2[1]),(UTM3[0],UTM3[1]),(UTM4[0],UTM4[1])])
Ejemplo n.º 10
0
def calc_utm_distance(lat1, lon1, lat2, lon2):
	"""
	Returns distance in millimeters
	"""
	point1 = utm.from_latlon(lat1, lon1)
	point2 = utm.from_latlon(lat2, lon2)
	
	x = point2[1]-point1[1]
	y = point2[2]-point1[2]
	
	return 1000*math.sqrt(x*x+y*y)
Ejemplo n.º 11
0
def events():
  """Gets isc data."""
  missing_params = []
  for p in ['lat', 'lon', 'distance', 'start', 'end', 'mag', 'catalog']:
    if p not in request.args:
      missing_params.append(p)
  if missing_params:
    return 'Missing parameters: {}'.format(','.join(missing_params))

  start = datetime.datetime.strptime(request.args.get('start'),
                                     DATE_FORMAT)
  end = datetime.datetime.strptime(request.args.get('end'),
                                   DATE_FORMAT)
  time_delta = end - start
  logging.info('Start Time {}'.format(start))
  logging.info('End Time {}'.format(end))
  logging.info('Time Delta {}'.format(time_delta))

  # Read the isc data.
  isc_data = isc.ReadISCData('gs://clouddfe-cfs/isc',
                             request.args.get('catalog'),
                             start, abs(time_delta.days),
                             (long(request.args.get('lat')),
                              long(request.args.get('lon'))),
                             int(request.args.get('distance')))

  # Prepare the ISC data by:
  #   1) Filtering out magnitude events smaller than specified.
  #   2) Projecting the lat/lon to UTM.
  ret = []
  _, _, num, let = utm.from_latlon(float(request.args.get('lat')),
                                   float(request.args.get('lon')))
  proj = lambda lat, lon: utm.from_latlon(lat, lon, num)[0:2]
  mag = float(request.args.get('mag'))
  num_filtered = 0
  for data in isc_data:
    if data['magnitude'] < mag:
      num_filtered += 1
      continue
    x, y = proj(data['lat'], data['lon'])
    ret.append({
        'lat': data['lat'],
        'lon': data['lon'],
        'utm_x': x,
        'utm_y': y,
        'depth': data['depth'],
        'mag': data['magnitude'],
        'date': data['date_time'].isoformat(' '),  # YYYY-MM-DD HH:MM:SS
    })
  logging.info('Filtered (%d) ISC events due to magnitude', num_filtered)

  if 'callback' in request.args:
    return '{}({})'.format(request.args.get('callback'), json.dumps(ret))
  return json.dumps(ret)
Ejemplo n.º 12
0
def get_distance(origin_lat, origin_lng, dest_lat, dest_lng):	
	xy = utm.from_latlon(origin_lat, origin_lng) # convert origin from lat-lng to xy
	originXY = point(float(xy[0]), float(xy[1]))
	
	xy = utm.from_latlon(dest_lat, dest_lng) # convert destination from lat-lng to xy
	destinationXY = point(float(xy[0]), float(xy[1]))

	distance = math.sqrt((originXY.x - destinationXY.x) ** 2 + (originXY.y - destinationXY.y) ** 2)
	res = [(destinationXY.x - originXY.x) * 4, (destinationXY.y - originXY.y) * 4]
	# print res
	return res # return the distance vector
Ejemplo n.º 13
0
def calc_utm_distance(lat1, lon1, lat2, lon2):
	"""
	Returns distance in millimeters
	This is not currently used in any of the code. Left for now in case needed
	"""
	point1 = utm.from_latlon(lat1, lon1)
	point2 = utm.from_latlon(lat2, lon2)

	x = point2[1]-point1[1]
	y = point2[2]-point1[2]

	return 1000*math.sqrt(x*x+y*y)
Ejemplo n.º 14
0
def calc_list_core( l_points, EP, func_fretch ):
    npos1 = 0
    npos2 = 0
    npos3 = 0
    list_count = len(l_points)

    ret_list = {}
    ret_list[3]=[]
    ret_list[5]=[]

    while npos1 < list_count - 2:
        try:
            la1, lo1, dis1 = func_fretch(l_points[npos1])
            npos1 = npos1 + 1
            if not la1 or not lo1 or not dis1:
                continue
            u1 = utm_point(*utm.from_latlon(la1,lo1), r=int(dis1))
        except Exception as e:
            continue

        npos2 = npos1
        while npos2 < list_count - 1:
            try:
                la2, lo2, dis2 = func_fretch(l_points[npos2])
                npos2 = npos2 + 1
                if not la2 or not lo2 or not dis2:
                    continue
                u2 = utm_point( *utm.from_latlon(la2,lo2), r=int(dis2))
            except Exception as e:
                continue

            s, p1, p2 = calc_cross_point(u1, u2)
            if not p1 or not p2:
                continue



            npos3 = npos2
            while npos3 < list_count:
                try:
                    la3, lo3, dis3 = func_fretch(l_points[npos3])
                    npos3 = npos3 + 1
                    if not la3 or not lo3 or not dis3:
                        continue
                    u3 = utm_point( *utm.from_latlon(la3,lo3), r=int(dis3))
                except Exception as e:
                    continue
                
                ret, level = calc_list_core_cross_point(s, p1, p2, u3, EP)
                if ret:
                    return ret, level
    return None, 0
Ejemplo n.º 15
0
def calc( latitude1, longitude1, r1,
              latitude2, longitude2, r2,
              latitude3, longitude3, r3, EP = global_EP):
    try:
        u1 = utm_point( *utm.from_latlon(latitude1,longitude1), r=r1)
        u2 = utm_point( *utm.from_latlon(latitude2,longitude2), r=r2)
        u3 = utm_point( *utm.from_latlon(latitude3,longitude3), r=r3)
    except Exception as e:
        return None

    pr = calc_distance_utm(u1,u2,u3, EP)
    if pr:
        latitude, longitude = utm.to_latlon(pr.x, pr.y, pr.zone, pr.mark)
        return latitude, longitude, pr.r
    return None
 def new_allotment(self, address, application, disabled_access, external_link, guidence, location, name, plot_size, rent, Easting, Northing):
     self.new_address(address)
     print utm.from_latlon(float(location.split(',')[0]), float(location.split(',')[1]))[1]
     allotment = al[name.replace(" ", "-").lower()] # @@ humanize the identifier (something like #rev-$date)
     self.graph.add((allotment, RDF.type, URIRef('http://data.gmdsp.org.uk/def/council/allotment/Allotment')))
     self.graph.add((allotment, COUNCIL['application'], URIRef(application)))
     self.graph.add((allotment, COUNCIL['information'], URIRef(external_link)))
     self.graph.add((allotment, COUNCIL['guidance'], URIRef("http://www.manchester.gov.uk"+guidence)))
     self.graph.add((allotment, GEO["lat"], Literal(location.split(',')[0])))
     self.graph.add((allotment, GEO["long"], Literal(location.split(',')[1])))
     self.graph.add((allotment, OS["northing"], Literal(str(utm.from_latlon(float(location.split(',')[0]), float(location.split(',')[1]))[1]))))
     self.graph.add((allotment, OS["easting"], Literal(str(utm.from_latlon(float(location.split(',')[0]), float(location.split(',')[1]))[0]))))
     self.graph.add((allotment, RDFS['label'], Literal(name)))
     self.graph.add((allotment,VCARD['hasAddress'], URIRef("http://data.gmdsp.org.uk/id/manchester/allotments/address/"+address.replace(" ", "_").replace(",","-").lower())))
     self.save()
Ejemplo n.º 17
0
def get_cdec(site_names, latitudes, longitudes, elevations, start_time, end_time, bool_dates=True):
    all_sites = {}
    aspect = gdal.Open("3m_data/Merced_500m_ASP.tif")
    aspect_raster = aspect.ReadAsArray()
    aspect_georeference = aspect.GetGeoTransform()
    if bool_dates:
        dates = []
    for site, lat, lon, elev in zip(site_names, latitudes, longitudes, elevations):
        temp_data = cdec.historical.get_data([site], [3, 18], 'monthly', start_time, end_time)
        temp_swe = temp_data[site]['3']['value'].values
        temp_sd = temp_data[site]['18']['value'].values
        temp_density = temp_swe / temp_sd

        new_df = pd.DataFrame({'swe, inch': temp_swe, 'sd, inch': temp_sd, 'density': temp_density},
                              index=temp_data[site]['3']['value'].index)
        if bool_dates:
            dates = temp_data[site]['3']['value'].index
        new_dict = {}
        new_dict['data'] = new_df
        new_dict['lat_lon'] = [lat, lon]
        new_dict['elev'] = elev * 0.3048
        new_dict['utm'] = utm.from_latlon(lat, lon)
        new_dict['aspect'] = aspect_raster[int((new_dict['utm'][0] - aspect_georeference[0]) / aspect_georeference[1]),
                                           int((new_dict['utm'][1] - aspect_georeference[3]) / aspect_georeference[5])]
        all_sites[site] = new_dict
    if bool_dates:
        return all_sites, dates
    else:
        return all_sites
    def __init__(self):
        #set up the initial position
        #Syntax see python package index
        #The syntax is utm.from_latlon(LATITUDE, LONGITUDE).
        #The return has the form (EASTING, NORTHING, ZONE NUMBER, ZONE LETTER).

        self.sim_period = 10.0
        self.dt = 1.0/self.sim_period
        self.steering = 0
        self.wheelbase = 1.13
        self.rate = rospy.Rate(10)
    #subject to change in the future
        self.utm_zone_number = 17
        self.utm_zone_letter = 'T'
        self.init_latitude = 40.441687
        self.init_longitude = -79.941594
        initial_pos_utm = utm.from_latlon(self.init_latitude, self.init_longitude)
        self.x = \
            [
                #UTM Easting
                [initial_pos_utm[0]], 
                #UTM Northing
                [initial_pos_utm[1]], 
                [3], 
                [250 * math.pi / 180], 
                [0]
            ]
        self.model = self.updateModel(self.x)
 def ExtractWaypoints(self):
     
     WPKeys = list(self.logdata.channels['CMD']['CNum'].dictData.keys())
     WPKeys.sort();
     
     WPnum = 1;
     WPtot = 35;
     for k in WPKeys:
         if(self.logdata.channels['CMD']['CNum'].dictData[k]==WPnum or WPnum == WPtot):
             
             tempLine = np.matrix('0.0 0.0 0.0 0.0 0.0 0.0');
             tempLine[0,0] = self.logdata.channels['CMD']['Lat'].dictData[k];
             tempLine[0,1] = self.logdata.channels['CMD']['Lng'].dictData[k];
             tempLine[0,2] = self.logdata.channels['CMD']['Alt'].dictData[k];
             tempLine[0,3] = self.logdata.channels['CMD']['TimeMS'].dictData[k];
             
             tempLine2 = np.matrix('0.0 0.0 0.0 0.0 0.0 0.0');
             (easting,northing,_,_) = utm.from_latlon(tempLine[0,0],tempLine[0,1]);
             tempLine2[0,0] = easting - self.LakeLag0[0,0];
             tempLine2[0,1] = northing - self.LakeLag0[0,1];
             tempLine2[0,2] = tempLine[0,2];
             tempLine2[0,3] = tempLine[0,3];
             if(WPnum == 1):
                 self.WPgeo = tempLine;
                 self.WPenu = tempLine2;
             else:
                 self.WPgeo = np.append(self.WPgeo,tempLine,axis=0);
                 self.WPenu = np.append(self.WPenu,tempLine2,axis=0);
                 
             
             WPnum = WPnum + 1;
Ejemplo n.º 20
0
def kml_roi_process(rpc, kml):
    """
    """
    # extract lon lat from kml
    with open(kml, 'r') as f:
        a = bs4.BeautifulSoup(f, "lxml").find_all('coordinates')[0].text.split()
    ll_bbx = np.array([list(map(float, x.split(','))) for x in a])[:4, :2]

    # save lon lat bounding box to cfg dictionary
    lon_min = min(ll_bbx[:, 0])
    lon_max = max(ll_bbx[:, 0])
    lat_min = min(ll_bbx[:, 1])
    lat_max = max(ll_bbx[:, 1])
    cfg['ll_bbx'] = (lon_min, lon_max, lat_min, lat_max)

    # convert lon lat bbox to utm
    z = utm.conversion.latlon_to_zone_number((lat_min + lat_max) * .5,
                                             (lon_min + lon_max) * .5)
    utm_bbx = np.array([utm.from_latlon(p[1], p[0], force_zone_number=z)[:2] for
                        p in ll_bbx])
    east_min = min(utm_bbx[:, 0])
    east_max = max(utm_bbx[:, 0])
    nort_min = min(utm_bbx[:, 1])
    nort_max = max(utm_bbx[:, 1])
    cfg['utm_bbx'] = (east_min, east_max, nort_min, nort_max)

    # project lon lat vertices into the image
    if not isinstance(rpc, rpc_model.RPCModel):
        rpc = rpc_model.RPCModel(rpc)
    img_pts = [rpc.inverse_estimate(p[0], p[1], rpc.altOff)[:2] for p in ll_bbx]

    # return image roi
    x, y, w, h = common.bounding_box2D(img_pts)
    return {'x': x, 'y': y, 'w': w, 'h': h}
Ejemplo n.º 21
0
def read_coords(filename):
    """ Return a map from county to lat/long. """
    d = {}
    for row in csv.reader(open(filename, 'rb'), delimiter='\t'):
        # d[row[1]] = (float(row[10]), float(row[11]))
        d[row[1]] = utm.from_latlon(float(row[10]), float(row[11]))[:2]
    return d
    def __init__(self):

        # initialise message structure
        self._gpsOut = gps()

        self.nmeaID = {'GGA' : self.gga,
                       'RMC' : self.rmc,
                       'GSA' : self.gsa,
                       'GSV' : self.gsv,
                       'VTG' : self.vtg
        }
        
        # get parameters # TODO Sophia will have to loop at this section
        self._controlRate = 1. # Hz
        lat_ori = rospy.get_param('lat_orig', 50.9567)
        lon_ori = rospy.get_param('long_orig', -1.36735)
        self.dt_status = rospy.get_param('status_timing', 2.)
        
        self.x_UTM_ori, self.y_UTM_ori, _, _ = utm.from_latlon(lat_ori, lon_ori)

        self._r = rospy.Rate(self._controlRate*2)  # Hz

        self._getAll = False

        #Define Publishers
        self.pubStatus = rospy.Publisher('status', status)
        self.pub = rospy.Publisher('gps_out', gps)
        self.pubMissionLog = rospy.Publisher('MissionStrings', String)
        
        self.serialPort = self.openSerialPort()
        rospy.on_shutdown(self.onShutdownEvents)

        self.repeatedlyPublishNodeStatus(nodeID=4, interv=2)

        self.mainloop()
Ejemplo n.º 23
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--file', required=True, help='input file')
    parser.add_argument('--output', required=True, help='output file name')
    arguments = parser.parse_args()
    
    input_file = open(arguments.file, 'r')
    output_file = open(arguments.output, 'w')
    output_file.write('$FormatUTM\n')
    lines = input_file.readlines()
    for line in lines:
        striped_line = line.strip()
        coordinates = striped_line[-11:]
        height = striped_line[-19:-15]
        x = ''.join([re.sub('\.', '', coordinates[-5:]),'00'])
        y = ''.join([re.sub('\.', '', coordinates[:5]),'00'])

        print '----------------------------------------'
        print 'SG      x: %s   y: %s height: %s' % (x,y,height)
        wgs_84 = lv3_to_wgs84(y, x, height)
        print 'WGS84 lat: %s lng: %s height: %s' % (wgs_84[0],wgs_84[1], wgs_84[2])
        utmcoords = utm.from_latlon(wgs_84[0], wgs_84[1])
        print 'UTM     z: %s%s e: %s      n: %s' % (utmcoords[2], utmcoords[3], utmcoords[0], utmcoords[1]) 
        tokens = striped_line.split(',')
        print 'DUMP:   %s   %s%s   %s   %s   %i   %s %s' % (striped_line[:6],utmcoords[2],utmcoords[3],utmcoords[0],utmcoords[1],wgs_84[2],striped_line[:6],tokens[1].strip())
        output_file.write('%s   %s%s   %i   %i   %i   %s %s\n' % (striped_line[:6],utmcoords[2],utmcoords[3],utmcoords[0],utmcoords[1],wgs_84[2],striped_line[:6],tokens[1].strip()))    
Ejemplo n.º 24
0
 def readFile(self, filename):
     """
     function that reads from netcdf file
     """
     nc = Dataset(filename, 'r')
     print "#### Reading ROMS output file !!!! ####\n"
     #print nc
     self.data = dict()
     x1 = 0; x2 = nc.variables['lon_psi'].shape[1]        
     y1 = 0; y2 = nc.variables['lon_psi'].shape[0]
     lon = nc.variables['lon_psi'][:]
     lat = nc.variables['lat_psi'][:]
     x = np.zeros_like(lon)
     y = np.zeros_like(lat)
     nx, ny = lon.shape    
     for i in range(nx):
         for j in range(ny):
             (y[i,j], x[i,j]) = utm.from_latlon(lat[i,j], lon[i,j])[0:2]
     
     self.data['lon'] = x
     self.data['lat'] = y
     self.data['lon_psi'] = lon
     self.data['lat_psi'] = lat
     u = nc.variables['u'][:,0,:,:]
     v = nc.variables['v'][:,0,:,:]
     self.data['mask'] = nc.variables['mask_psi'][:]
     
     ftime = nc.variables['ocean_time']
     time = num2date(ftime[:],ftime.units)
     self.ind0 = self.findNearest(self.starttime,time)
     self.ind1 = self.findNearest(self.endtime,time)
     self.data['time'] = time[self.ind0:self.ind1+1]
     
     self.data['u'] = u[self.ind0:self.ind1+1,y1:y2+1,x1:x2]  
     self.data['v'] = v[self.ind0:self.ind1+1,y1:y2,x1:x2+1] 
Ejemplo n.º 25
0
def main(): 
	# we need to specify our coordinates in UTM. 
	# Let's use a python library for this - because it is easy to work with Lat/Long
	# So we can just put in the lattitude and longitude and let Python do the work. 
	myUTMCoordinate = utm.from_latlon(53.3465404618, -6.29237818)
	myUTMCoordinateX = myUTMCoordinate[0]
	myUTMCoordinateY = myUTMCoordinate[1]
	distanceBufferKM = 2.56 ## remember this is the square of what we want. 
	amenityType = "post_office"
	
	pointsWithinBuffer = findPointsWithinDistanceBuffer("OSM_BritishIsles_Amenities.sqlite",myUTMCoordinateX,myUTMCoordinateY,distanceBufferKM,amenityType)
	
	# let's write the results to a CSV file - 
	outputCSVFile = open("PointsWithinBufferType.csv","w")
	
	outputCSVFile.write("Amenity,AmenityName,Latitude,Longitude\n")
	
	for p in pointsWithinBuffer: # remember our query returned all of the columns 
		# columns are zero-based indices - so latitude is the 5th column
		amenity = p[1]
		amenityName = p[2]
		latitude = p[4]
		longitude = p[3]
		outputCSVFile.write("{},\"{}\",{},{}\n".format(amenity,amenityName,latitude,longitude))
		
	outputCSVFile.close()
Ejemplo n.º 26
0
    def __calculateIcePoints(self,ice_dams, annotation):
        """
        internal function
        calculates for each picture the ice dam locations and gives a real lat lon
        :param ice_dams: located x pos of ice dams on a picture, units of pixels
        :param annotation: data for this specific picture
        :return:
        """
        width = 3.70419 * annotation.depth # calculated using super accurate picture taking
        meter_per_pixel = width/854.0
        origin = [float(x) for x in annotation.origin.split(",")]
        utm_origin = UTMPoint(utm.from_latlon(origin[0], origin[1]))

        # assumes that the drone is always perpendicular to the centroid of the house
        # we take the lidar measurement to the roof and turn the picture into a projection mapping that looks like a pyramid facing the house
        # using the pixel locations we can calculate how far left or right of the center of the projection map the ice dam is located
        for i in range(0, len(ice_dams)):
            vec = self.centroid.getVector(utm_origin).scalarMult(annotation.depth)
            x_loc = meter_per_pixel * ice_dams[i][0]
            x_offset = -1 * ((width/2) - x_loc) # left of the center is heading west, right is heading east
            vec.e += x_offset
            ice_loc = utm_origin.add(vec).toLatLon(7)
            annotation.ice_locations.append(str(ice_loc.lat) + "," + str(ice_loc.lon))

        annotation.detected = True
Ejemplo n.º 27
0
def llz2utm(lon,lat,projection_zone='None'):
    '''
    Convert lat,lon to UTM
    '''
    from numpy import zeros,where,chararray
    import utm
    from pyproj import Proj
    from scipy.stats import mode
    
    x=zeros(lon.shape)
    y=zeros(lon.shape)
    zone=zeros(lon.shape)
    b=chararray(lon.shape)
    if projection_zone==None:
        #Determine most suitable UTM zone
        for k in range(len(lon)):
            #x,y,zone[k],b[k]=utm.from_latlon(lat[k],lon[k]-360)
            x,y,zone[k],b[k]=utm.from_latlon(lat[k],lon[k])
        zone_mode=mode(zone)
        i=where(zone==zone_mode)[0]
        letter=b[i[0]]
        z=str(int(zone[0]))+letter
    else:
        z=projection_zone
    print z
    p = Proj(proj='utm',zone=z,ellps='WGS84')
    x,y=p(lon,lat)
    return x,y
Ejemplo n.º 28
0
def preprocess_dataset(sequence):
  sequence = list(map(lambda x: [x[0] * 1e-3] + list(utm.from_latlon(x[1], x[2]))[:2],
                      sequence))
  _, start_lat, start_lon = sequence[0]
  sequence = list(map(lambda x: [x[0], x[1] - start_lat, x[2] - start_lon],
                      sequence))
  return sequence
Ejemplo n.º 29
0
def local_to_global(origin, x, y, theta=0.0):
    """
    Converts from a local frame in meters into a global frame in lon/lat.
    Note that heading is in degrees!

    @param origin (lon, lat, heading)
    @param point shapely.geometry.Point() in local frame
    @return (longitude, latitude, heading) in WGS84
    """
    # Create local transformation frame.
    import utm
    ox, oy, zone, hemi = utm.from_latlon(origin[1], origin[0])
    o_theta = (90 - origin[2]) * (math.pi/180.0)

    # Translate and rotate point to origin.
    point = shapely.geometry.Point(x, y)
    point = shapely.affinity.rotate(point, o_theta,
                                    origin=ORIGIN, use_radians=True)
    point = shapely.affinity.translate(point, ox, oy)
    p_theta = theta + o_theta
    heading = 90 - (p_theta * 180.0/math.pi)

    # Return transformed point.
    lat, lon = utm.to_latlon(point.x, point.y, zone, hemi)
    return lon, lat, heading
Ejemplo n.º 30
0
    def from_waypoint_message(cls,wp):
        """Constructs a new UTMWaypoint from a waypoint message

           Parameters
           msg - waypoint in global frame

           Returns
           - A new UTMWaypoint with coordinates converted from msg
           - None if msg is not in the global coordinate frame
        """

        #**********************************************************************
        #   Don't accept waypoint if its not global.
        #   Return None to indicate failure
        #**********************************************************************
        if msg.Waypoint.FRAME_GLOBAL != wp.frame:
            return None

        #**********************************************************************
        #   Otherwise return a new representation, using mapping defined
        #   in mavros/Waypoint.msg
        #**********************************************************************
        (east, north, z_num, z_let) = from_latlon(wp.x,wp.y)       
        result = cls()  # a new UTMWaypoint instance
        result.easting = east
        result.northing = north
        result.altitude = wp.z
        result.zone_number = z_num
        result.zone_letter = z_let
        return result
Ejemplo n.º 31
0
 def DistGPS(self, gps0, gps1):
     e0, n0, _, _ = utm.from_latlon(*gps0)
     e1, n1, _, _ = utm.from_latlon(*gps1)
     return np.linalg.norm([e0 - e1, n0 - n1, self.alt0 - self.alt1])
Ejemplo n.º 32
0
def utm_of_point(point):
    return utm.from_latlon(point.y, point.x)[2]
Ejemplo n.º 33
0
    def check_for_invalid_positions(self, query_result):
        # we start by using the buildings that we found from the query that we didnt use and build polygons from them.
        #self.house_impression_positions
        #self.house_corner_positions

        #fig, ax = plt.figure(2)

        # Convert query result buildings into Shapely polygons
        surrounding_polygons = []
        counter = 0
        for way in query_result.ways:
            
            
            x, y, number, letter = utm.from_latlon(self.origin[0], self.origin[1])
            local_coords = []
            for node in way.nodes:
                pointlat = node.lat
                pointlon = node.lon
                pointx, pointy, number, letter = utm.from_latlon(float(pointlat), float(pointlon))
                difx = x - pointx
                dify = y - pointy
                
                #print("  x: %f, y: %f" % (difx, dify))
                local_coords.append(np.array([-difx, -dify]))

            

            # Simplify local coordinates so that we don't get multiple vertices on essentialy a single edge
            shapely_polygon = Polygon(local_coords)
            surrounding_polygons.append(shapely_polygon)
            counter += 1
        
        for i in range(len(surrounding_polygons)):
            xx, yy = surrounding_polygons[i].exterior.coords.xy
            
            plt.plot(xx, yy, marker='o', color='b')
        
        # Check if any of the impression positions and corner positions are invalid
        impression_issue = False
        for i in range(len(self.house_impression_positions)):
            p1 = Point(self.house_impression_positions[i][0], self.house_impression_positions[i][1])
            for i in range(len(surrounding_polygons)):
                poly = surrounding_polygons[i]
                
                if p1.within(poly):
                    way = query_result.ways[i]
                    print("Name: %s" % way.tags.get("name", "n/a"))
                    print("  Building: %s" % way.tags.get("building", "n/a"))
                    print("  Approximate height %s" % way.tags.get("building:height", "n/a"))
                    print("Impression pose number " + str(i) + " is invalid")
                    impression_issue = True

        if impression_issue:
            print("There is an issue with one or more of the impression coordinates of this building.")
        else:
            print("Impression pose list ready. No issues encountered.")

        # And the impression poses in a slightly different color
        xi = []
        yi = []
        u = []
        v = []
        for i in range(len(self.house_impression_positions)):
            xi.append(self.house_impression_positions[i][0])
            yi.append(self.house_impression_positions[i][1])
            u.append(self.house_impression_direction[i][0])
            v.append(self.house_impression_direction[i][1])
            
        #plt.scatter(xi, yi, marker='o', color='r')
        plt.quiver(xi, yi, u, v, color='r', label="Impression Poses")


        corner_issue = False
        for i in range(len(self.house_corner_positions.coords)):
            p1 = Point(self.house_corner_positions.coords[i][0], self.house_corner_positions.coords[i][1])
            for i in range(len(surrounding_polygons)):
                poly = surrounding_polygons[i]
                
                if p1.within(poly):
                    way = query_result.ways[i]
                    print("Name: %s" % way.tags.get("name", "n/a"))
                    print("  Building: %s" % way.tags.get("building", "n/a"))
                    print("  Approximate height %s" % way.tags.get("building:height", "n/a"))
                    print("Corner pose number " + str(i) + " is invalid")
                    corner_issue = True

        if corner_issue:
            print("There is an issue with one or more of the corner coordinates of this building.")
        else:
            print("Corner pose list ready. No issues encountered.")

        xxx, yyy = self.house_corner_positions.coords.xy
        plt.scatter(xxx, yyy, label="Intermediate positions")
            col[i].append(j)

# Save each column into arrays (Element 0 is unit), these can be changed to any data name in the csv(e.g. Throttle)
altArray = col['Alt']
latArray = col['Lat']
lonArray = col['Lon']

# Before the latLonArray can be filled, the 0th element of each array must be removed
altArray.pop(0)
latArray.pop(0)
lonArray.pop(0)

# Convert into UTM Coords
tempLatLonArray = []
for counter in range(len(altArray)):
    tempLatLonArray.append(utm.from_latlon(float(latArray[counter]), float(lonArray[counter])))

# Convert the new list of tuples into a list of strings that are formatted to be partitioned later
latLonArray = ["[%f] (%f) %d %s" % x for x in tempLatLonArray]

# Create new Lat and Lon arrays to store the UTM values (new Alt array to match new convention and covert to float)
utmLatArray = []
utmLonArray = []
utmAltArray = []

# Fill the new UTM arrays
for counter in range(len(latLonArray)):
    utmLatArray.append(float(latLonArray[counter].partition('[')[-1].rpartition(']')[0]))
    utmLonArray.append(float(latLonArray[counter].partition('(')[-1].rpartition(')')[0]))
    utmAltArray.append(float(altArray[counter]))
Ejemplo n.º 35
0
def readLineCoordsFromMultiLineShp(shapefileFn, transNum):
    '''
    	# TAKEN FROM M. FAHNESTOCK'S L8_sample_frames... code. 08 sep 2016
    	# Unfinished as of 08 sep 2016 wha
    	# 22 sep 2016 think it's finished wha
    	# Mark's notes	
    	# use fiona to read in points along centerline in local projection (called PS here)
    	# save the PS original points in sample_pts_PS, then reproject to 4326, push back into
    	# coordinates of input Point object (f) and make a list of these (out_pts_ll) and when done save
    	# them as a Shapely MultiPoint object
    	'''
    with fiona.open(shapefileFn, 'r') as c:

        # Initialize
        sample_pts_local = []
        sample_pts_lon_lat = []
        xList = []
        yList = []
        lonList = []
        latList = []

        # Get coordinate reference system from original shapefile
        original = Proj(c.crs)

        destination = Proj(init='EPSG:4326')  # dest is WGS84 = EPSG 4326

        f = c[transNum]  # open specified feature (transect)
        props = f['properties']

        for j in f['geometry']['coordinates']:
            x = j[0]
            y = j[1]
            xList.append(j[0])
            yList.append(j[1])
            sample_pts_local.append((x, y))
            lon, lat = transform(original, destination, x, y)
            lonList.append(lon)
            latList.append(lat)
            sample_pts_lon_lat.append((lon, lat))

        medLon = np.median(lonList)  # get meadian longitude
        utmZone = utm.from_latlon(51.2, medLon)[2]  # get utm zone

        epsgLocalUtm = 32600 + utmZone
        destination2 = Proj(init='EPSG:' + str(epsgLocalUtm))
        easting, northing = transform(destination, destination2, lonList,
                                      latList)
        # Stick it together in a dict for storage
        dataOut = {
            'inProps': props,
            'shpFn': shapefileFn,
            'xLocal': xList,
            'yLocal': yList,
            'lat': latList,
            'lon': lonList,
            'samplePtsXy': sample_pts_local,
            'samplePtsLatLon': sample_pts_lon_lat,
            'utmEasting': easting,
            'utmNorthing': northing,
            'utmZone': utmZone
        }

        return dataOut
Ejemplo n.º 36
0
 def test_right_of(self):
     self.assert_zone_equal(UTM.from_latlon(55.999999, 12), 33, 'U')
     self.assert_zone_equal(UTM.from_latlon(56, 12), 33, 'V')
     self.assert_zone_equal(UTM.from_latlon(60, 12), 33, 'V')
     self.assert_zone_equal(UTM.from_latlon(63.999999, 12), 33, 'V')
     self.assert_zone_equal(UTM.from_latlon(64, 12), 33, 'W')
Ejemplo n.º 37
0
def makeStringFromSeabedImage(tokval):
    no = len(tokval)
    # Interpolate between positions.  First I skip the samples outside center
    # sample on each side
    prevX = ""
    prevY = ""
    thisX = ""
    thisY = ""
    prevIdx = -1
    retval = ""
    for i in range(no):
        found = tokval[i].find('(')
        if (found >= 0):
            nxt = tokval[i + 1].find(')')
            if (nxt < 0):
                return "error"
            if (len(thisY) < 1):
                prevY = tokval[i]
                prevX = tokval[i + 1]
                prevIdx = i + 2  #First valid value
                thisX = prevX
                thisY = prevY
            else:
                thisY = tokval[i]
                thisX = tokval[i + 1]
                #Then interpolate the values between prevIdx and i; i-1 being the last
                #value
                #First convert string to float, then to utm
                px = float(prevX[:-1])
                py = float(prevY[1:])
                tx = float(thisX[:-1])
                ty = float(thisY[1:])
                u = utm.from_latlon(py, px)
                x1 = u[0]
                y1 = u[1]
                u = utm.from_latlon(ty, tx)
                x2 = u[0]
                y2 = u[1]
                noSamp = i - prevIdx
                xd = float(x2 - x1) / float(noSamp)
                yd = float(y2 - y1) / float(noSamp)
                for s in range(noSamp):
                    samp = float(tokval[prevIdx + s]) * 0.1
                    x = x1 + xd * s
                    y = y1 + yd * s
                    str = "%.2f %.2f %.1f\n" % (x, y, samp)
                    retval += str
                    break_me_here = 0  #debug purposes
                prevX = thisX
                prevY = thisY
                prevIdx = i + 2


#Then add the outer samples.  Mirror the second (last) position to find the
#direction and interpolation distance
#You can change the output order if necessary to get a nice output in the
#csv-file
    first = -1
    for i in range(no):
        a = tokval[i].find('(')
        if (a >= 0):
            first = i
            break

    second = -1
    for i in range(no):
        a = tokval[i].find('(')
        if (a >= 0):
            if (i != first):
                second = i
                break

    last = -1
    for i in range(no):
        a = tokval[no - 1 - i].find('(')
        if (a >= 0):
            last = i
            break

    second_last = -1
    for i in range(no):
        a = tokval[no - 1 - i].find('(')
        if (a >= 0):
            if (i != last):
                second_last = i
                break

    last = no - last - 1
    second_last = no - second_last - 1

    px = float(tokval[first + 1][:-1])
    py = float(tokval[first][1:])
    firstu = utm.from_latlon(py, px)
    firstx = firstu[0]
    firsty = firstu[1]
    px = float(tokval[second + 1][:-1])
    py = float(tokval[second][1:])
    secondu = utm.from_latlon(py, px)
    secondx = secondu[0]
    secondy = secondu[1]
    fxd = (secondx - firstx) / (second - first - 2)
    fyd = (secondy - firsty) / (second - first - 2)

    px = float(tokval[last + 1][:-1])
    py = float(tokval[last][1:])
    lastu = utm.from_latlon(py, px)
    lastx = lastu[0]
    lasty = lastu[1]
    px = float(tokval[second_last + 1][:-1])
    py = float(tokval[second_last][1:])
    second_lastu = utm.from_latlon(py, px)
    second_lastx = second_lastu[0]
    second_lasty = second_lastu[1]
    lxd = (second_lastx - lastx) / (second_last - last - 2)
    lyd = (second_lasty - lasty) / (second_last - last - 2)

    for i in range(no):
        if (i == first):
            break
        samp = float(tokval[i]) * 0.1
        x = firstx - i * fxd  # use - as we are moving outwards from second through first and beyond
        y = firsty - i * fyd
        str = "%.2f %.2f %.1f\n" % (x, y, samp)
        retval += str

    for i in range(no):
        k = no - 1 - i
        if (k == last + 1):
            break
        samp = float(tokval[last + i + 2]) * 0.1
        x = lastx + i * lxd
        y = lasty + i * lyd
        str = "%.2f %.2f %.1f\n" % (x, y, samp)
        retval += str

    return retval
Ejemplo n.º 38
0
 def test_left_of(self):
     self.assert_zone_equal(UTM.from_latlon(55.999999, 2.999999), 31, 'U')
     self.assert_zone_equal(UTM.from_latlon(56, 2.999999), 31, 'V')
     self.assert_zone_equal(UTM.from_latlon(60, 2.999999), 31, 'V')
     self.assert_zone_equal(UTM.from_latlon(63.999999, 2.999999), 31, 'V')
     self.assert_zone_equal(UTM.from_latlon(64, 2.999999), 31, 'W')
Ejemplo n.º 39
0
    def to_UTM_pythonic(self):
        """
        Converts the DEM to UTM coordinate system. Automatically checks the UTM zone using the python package utm. Uses GDAL command line to do this

        Returns:
            bil (bool): If true, convert to ENVI bil format

        Author: SMM

        Date: 06/07/2020
        """

        filename = self.path + self.prefix + "_" + self.source + ".tif"

        # First get the source coordinate system
        # open dataset
        ds = gdal.Open(filename)
        prj = ds.GetProjection()
        print("The projections is:")
        print(prj)
        ds = []

        print("And some extra projection information strings:")
        srs = osr.SpatialReference(wkt=prj)
        if srs.IsProjected:
            print(srs.GetAttrValue('projcs'))
        print(srs.GetAttrValue('geogcs'))

        # now do it with rasterio
        dem_data = rio.open(filename)
        print(dem_data.meta)

        temp_info = utm.from_latlon((self.latitude_S + self.latitude_N) / 2,
                                    (self.longitude_W + self.longitude_E) / 2)
        if (temp_info[3] in ['X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P',
                             'N']):
            south = False
        else:
            south = True

        res = 30.0
        if (self.source == "SRTM90"):
            res = 90.0

        UTMzone = temp_info[2]
        if south:
            EPSG = "327" + str(UTMzone)
        else:
            EPSG = "326" + str(UTMzone)

        res_tuple = (res, res)
        print("res tuple is:")
        print(res_tuple)

        dst_crs = 'EPSG:' + EPSG
        print("The destination CRS is: " + dst_crs)
        output_filename = self.path + self.prefix + "_" + self.source + "_UTM.tif"

        with rio.open(filename) as src:
            transform, width, height = calculate_default_transform(
                src.crs,
                dst_crs,
                src.width,
                src.height,
                resolution=res_tuple,
                *src.bounds)
            kwargs = src.meta.copy()
            kwargs.update({
                'crs': dst_crs,
                'transform': transform,
                'width': width,
                'height': height
            })

            with rio.open(output_filename, 'w', **kwargs) as dst:
                for i in range(1, src.count + 1):
                    reproject(source=rio.band(src, i),
                              destination=rio.band(dst, i),
                              src_transform=src.transform,
                              src_crs=src.crs,
                              dst_resolution=res_tuple,
                              dst_transform=transform,
                              dst_crs=dst_crs,
                              resampling=Resampling.cubic)
        dem_datam2 = rio.open(output_filename)

        print(dem_datam2.meta)
Ejemplo n.º 40
0
 def toUTM(self, coords):
     latitude = coords[0]
     longitude = coords[1]
     return utm.from_latlon(latitude,
                            longitude)  # see documentation for utm library
Ejemplo n.º 41
0
import shapefile
import utm
reader = shapefile.Reader(
    r"D:\Program Files\Python学习文档\samples\footprints\footprints_se.shp")
writer = shapefile.Writer(
    r"D:\Program Files\Python学习文档\samples\footprints\footprints_split.shp",
    reader.shapeType)
writer.fields = list(reader.fields[1:])
for shapeRecord in reader.iterShapeRecords():
    utmPoints = []
    for point in shapeRecord.shape.points:
        x, y, band, zone = utm.from_latlon(point[1], point[0])
        #latitude 纬度 longitude 经度,网格
        utmPoints.append([x, y])
    area = abs(
        shapefile.signed_area(utmPoints))  #abs()绝对值函数 signed_area()计算多边形面积
    if area <= 100:
        writer.shape(shapeRecord.shape)
        writer.record(*shapeRecord.record)
reader.close()
writer.close()
print("Shapefile文件分隔完成!")
'''
r=shapefile.Reader(r"D:\Program Files\Python学习文档\samples\footprints\footprints_se.shp")
subset=shapefile.Reader(r"D:\Program Files\Python学习文档\samples\footprints\footprints_split.shp")
r.numRecords
26447
subset.numRecords
13331
'''
Ejemplo n.º 42
0
def main(filename):

    print("Hello, starting GCP-OSM...")
    print("")

    # preparation
    gcp_list = load_your_gcp_list("my_gcp_list.txt")
    f = open("gcp_list.txt", "w")
    odm_gcp_header = 0

    # now working time
    if os.path.isdir(args.file):
        print("loading in all files in folder:", filename)
        processing_files = gcposm.utils.get_all_files(filename)

    elif os.path.isfile(args.file):
        print("loading in this file:", filename)
        processing_files = gcposm.utils.get_one_file(filename)

    else:
        print("neither file nor folder. ending programm.")
        return

    for file in processing_files:
        found_qr_codes = get_qr_codes(file)

        for found_qr_code in found_qr_codes:
            valid, parsed, point = found_qr_code

            if valid:
                print("qr found in", file, parsed, point)

                #do stuff now...

                ## type indicator cases
                if parsed.find("/n/") > -1:
                    print("type indicator: n/ = OSM Node ID with Basis 64")

                if parsed.find("/w/") > -1:
                    print("type indicator: w/ = OSM Way ID with Basis 64")

                if parsed.find("/a/") > -1:
                    print("type indicator: a/ = OSM Area ID with Basis 64")

                if parsed.find("/r/") > -1:
                    print("type indicator: r/ = OSM Relation ID with Basis 64")

                if parsed.find("/g/") > -1:
                    print(
                        "type indicator: g/ = OSM Shortlink quadtiles format")

                my_gcp_id = parsed
                # when the local id was found with a type indicator, it will be replaced.
                # otherwise the parsed text will directly be used in the next "simple locally defined payload" method.

                if parsed.find("/l/") > -1:
                    print("type indicator: l/ = locally defined payload")
                    my_gcp_id = parsed.split("l/")[1]
                    #print("gcp id", my_gcp_id, "in the local my_gcp_list.txt")

                ## simple locally defined payloads
                for item in gcp_list:
                    if my_gcp_id == item[0]:
                        print("\t found a known gcp (", item[1], "/", item[2],
                              "/", item[3], ") from your list")
                        location_utm = utm.from_latlon(float(item[1]),
                                                       float(item[2]))

                        # OpenDroneMap GCP
                        ## header
                        if odm_gcp_header == 0:
                            # this is still to understood, why ODM just allows one utm zone and that is in the header!
                            f.write("WGS84 UTM " + str(location_utm[2]) +
                                    str(location_utm[3]) + "\n")
                            odm_gcp_header = 1

                        ## line by line saving the coordinates
                        f.write((str(location_utm[0]) + " " +
                                 str(location_utm[1]) + " " + item[3] + " " +
                                 str(point[0]) + " " + str(point[1]) + " " +
                                 file.split(os.sep)[-1]) + "\n")

            else:
                print("qr not found in", file)

    f.close()
    print("")
    print("GCP-OSM is finished, Good Bye!")
Ejemplo n.º 43
0
def isrpLoadDem(demFilename,sensors,iRangex,iRangey):
    f = open(demFilename, 'r')
    whl = f.read()
    whl = whl.split('\n')
    # header
    hdr = whl[0:5]
    deminfo = np.dtype({'names': ('ncols', 'nrows', 'xllcorner', 'yllcorner', 'dx', 'dy'),
                        'formats': ('f8', 'f8', 'f8', 'f8', 'f8', 'f8')})
    nInfo = len(hdr)
    info = np.zeros(1, dtype=deminfo)
    s = hdr[0].split()
    info['ncols'] = float(s[1])
    s = hdr[1].split()
    info['nrows'] = float(s[1])
    s = hdr[2].split()
    info['xllcorner'] = float(s[1])
    s = hdr[3].split()
    info['yllcorner'] = float(s[1])
    s = hdr[4].split()
    info['dx'] = float(s[1])
   # s = hdr[5].split()
    info['dy'] = float(s[1])

    xllcu, yllcu, zonen, zonel = utm.from_latlon(info['yllcorner'], info['xllcorner'])
    print('utm zone: ' + str(zonen) + zonel)

    # Latitude:  1 deg = 110.54 km
    # Longitude: 1 deg = 111.320*cos(latitude) km
    dy = info['dy'] * 110.54 * 1000
    dx = info['dx'] * 111.32 * cos(info['yllcorner'] * pi / 180) * 1000

    xu = np.linspace(xllcu, xllcu + info['ncols'] * dx,info['ncols'])
    #print(info['ncols'])
    yu = np.linspace(yllcu + info['nrows'] * dy, yllcu, info['nrows'])

    xu=xu[:,0]
    yu = yu[:, 0]
    print(yu.min())
    print(yu.max())
    print(yu[0])
    #print(info['nrows'])

    # elevation matrix
    bb = whl[5:len(whl)]
    print(len(bb))
    idx = 0
    for i in range(0, len(yu)):
       # print(i);
        bw = bb[i].split(' ')
        bw.remove('')
        aa = np.asarray(bw, dtype=np.float32)
        if idx == 0:
            z = aa
        else:
            z = vstack((z, aa))
        idx += 1





    print('ncols: ', str(len(xu)))
    print('nrows: ', str(len(yu)))
    print('zmatrix: ' + z.shape[0].__str__() + ' x ' + z.shape[1].__str__())

    # int(np.amax(z))
    #plt.contourf(xu, yu, z, cmap="gray",
     #            levels=list(range(0, 5000, 50)))
    #plt.title("Elevation Contours Goms (CH) area")
    #cbar = plt.colorbar()
    #plt.gca().set_aspect('equal', adjustable='box')
    # lat=5146098.00 m N
    # lon=442227.00 m E
    print(len(sensors))
    #for i in range(0,len(sensors)):
    #    print(i)
    #    plt.plot(sensors['X'][i], sensors['Y'][i], marker='o', markersize=2, color='r', ls='')

    #plt.show()
    z[np.isnan(z)] = 0
    zDemOrg = z
    xDemOrg = xu
    yDemOrg = yu
    if (iRangex>0):
        xL = np.where(xu > (np.min(sensors['X']) - iRangex))[0][0]
        xR = np.where(xu < (np.max(sensors['X']) + iRangex))[0][-1]

        xu = xu[xL:xR]
        z = z[:, xL:xR]

    else:
        xL=0
        xR=len(xu)

    if (iRangey>0):

        yB = np.where(yu < (np.max(sensors['Y']) - iRangey))[0][0]
        yT = np.where(yu < (np.min(sensors['Y']) + iRangey))[0][0]

        yu = yu[yT:yB]
        z = z[yT:yB,:]
    else:
        yT=0
        yB=len(yu)

    return xu, yu, z,xDemOrg,yDemOrg,zDemOrg, xL,xR,yT,yB
Ejemplo n.º 44
0
import utm


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='extract trajectories from bag file')
    parser.add_argument('--bag', '-b', action='store', default=None, required=True,
                        help='name of bag file')

    topics = ['/gps/fix']
              
    args = parser.parse_args()

    fileNames = ['gps_traj.txt']
    
    gpsFile = open(fileNames[0], 'w')

    for bagfile in [args.bag]:
        bag = rosbag.Bag(bagfile, 'r')
        it = bag.read_messages(topics=topics)
        for (topic, msg, tros) in it:
            if (topic == '/gps/fix'):  # gps long/lat
                utm_pos = utm.from_latlon(msg.latitude, msg.longitude)
                a = msg.altitude # ignored here
                T = tf.transformations.identity_matrix()
                T[0:3,3] = np.array([utm_pos[0], utm_pos[1], 0])
                gpsFile.write("%.6f %s\n" % (msg.header.stamp.to_nsec()/1e9, ' '.join(map(str, T.ravel()[0:12].tolist()))))
                
    gpsFile.close()
    print "wrote to files: %s" % ", ".join(fileNames)
    
Ejemplo n.º 45
0
def scatter_with_utm(utmX,
                     utmY,
                     z,
                     zone_number,
                     zone_letter,
                     colors=None,
                     maptype=MAPTYPE,
                     **kargs):
    """Scatter plot over a map. Can be used to visualize clusters by providing the marker colors.

    :param pandas.Series latitudes: series of sample latitudes
    :param pandas.Series longitudes: series of sample longitudes
    :param pandas.Series colors: marker colors, as integers
    :param string maptype: type of maps, see GoogleStaticMapsAPI docs for more info

    :param string title: title of the plot
    :param string xlabel: x label
    :param string ylabel: y label
    :param boolean cbar: add color bar to the graphic
    :param string cLabel: Label in color bar
    :param boolean saveFig: save Figs
    :param string figname: name of the file to save the figure

    :return: None
    """
    ###########################
    # Default optional values #
    ###########################
    title = kargs.pop('title', ' ')
    xlabel = kargs.pop('xlabel', 'East - Local Cordinates (UTM-REF) [m]')
    ylabel = kargs.pop('ylabel', 'North - Local Cordinates (UTM-REF) [m]')
    cLabel = kargs.pop('cLabel', ' ')
    cbar = kargs.pop('cbar', False)
    saveFig = kargs.pop('saveFig', False)
    figName = kargs.pop('figName', 'googleMaps_wLines_utm')

    if kargs: raise TypeError('extra keywords: %s' % kargs)

    ############################################################################
    # Convert UTM to WGS84 because google maps api only acepts tuple (lat,lon) #
    ############################################################################
    lat = []
    lon = []

    for i in range(len(utmX)):
        latlon = pd.Series(
            utm.to_latlon(utmX[i], utmY[i], zone_number, zone_letter))
        lat.append(latlon[0])
        lon.append(latlon[1])

    ################################################
    # latitudes and longitudes to panda database #
    ################################################
    d_wgs = {'latitudes': lat, 'longitudes': lon}
    df_wgs = pd.DataFrame(data=d_wgs)

    latitudes = df_wgs['latitudes']
    longitudes = df_wgs['longitudes']

    #################################
    # Get google maps static image #
    ################################
    width = SCALE * MAX_SIZE
    img, pixels = background_and_pixels(latitudes, longitudes, MAX_SIZE,
                                        maptype)

    ##########################
    # get zoom normally = 16 #
    ##########################
    center_lat = (latitudes.max() + latitudes.min()) / 2
    center_long = (longitudes.max() + longitudes.min()) / 2
    zoom = GoogleStaticMapsAPI.get_zoom(latitudes, longitudes, MAX_SIZE, SCALE)

    ###################################
    # Center of image in pixel/meters #
    ###################################
    centerPixelY = round(img.height / 2)
    centerPixelX = round(img.width / 2)

    ##################################################################
    # lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913 #
    ##################################################################
    [mx, my] = GoogleStaticMapsAPI.LatLonToMeters(center_lat, center_long)
    curResolution = INITIALRESOLUTION / 2**zoom / SCALE

    ###################
    # x and y vector #
    ##################
    xVec = mx + (np.arange(img.width) - centerPixelX) * curResolution
    yVec = my + (np.arange(img.height) - centerPixelY) * curResolution

    #####################################
    # Convert from EPSG:900913 to WGS84 #
    #####################################
    lat_north, lon_east = GoogleStaticMapsAPI.MetersToLatLon(xVec, yVec)

    ########################
    # Convert WGS84 to UTM #
    ########################
    east = []
    north = []
    for i in range(len(lat_north)):
        eastNorth = utm.from_latlon(lat_north[i],
                                    lon_east[i],
                                    force_zone_number=zone_number)
        east.append(eastNorth[0])
        north.append(eastNorth[1])

    ############################################################
    # plot google map image and the values from the input data #
    ############################################################
    fig, ax = plt.subplots()
    ax.set_title(title, y=1.04)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)

    ax.imshow(np.flip(np.array(img), 0),
              extent=[east[0], east[-1], north[0], north[-1]],
              origin='lower')

    sc = ax.scatter(
        utmX,  # Scatter plot
        utmY,
        c=z,
        s=width / 400,
        linewidth=0,
        alpha=1,
        zorder=3)

    #############
    # Color Bar #
    #############
    if cbar:
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad=0.2, aspect=15)
        cbar = fig.colorbar(sc, cax=cax)
        cbar.ax.set_title(cLabel)

    ######################
    # Hide google footer #
    ######################
    ax.axis([east[50], east[-1], north[50], north[-1]])

    ###################
    # Save Fig option #
    ###################
    if saveFig:
        if os.path.exists('./Figs'):
            fig.savefig('./Figs/' + figName + '.png', format='png', dpi=600)
            fig.savefig('./Figs/' + figName + '.eps', format='eps', dpi=1000)
        else:
            os.makedirs('./Figs/')
            fig.savefig('./Figs/' + figName + '.png', format='png', dpi=600)
            fig.savefig('./Figs/' + figName + '.eps', format='eps', dpi=1000)
Ejemplo n.º 46
0
data_links_csv = os.path.join(data_path, 'Partition6467LinkData.csv')
data_probes_csv = os.path.join(data_path, 'Partition6467ProbePoints.csv')
clean_links_csv = os.path.join(data_path, 'CleanLinkData.csv')
clean_probes_csv = os.path.join(data_path, 'CleanProbePoints.csv')

INFO = '\033[1;32m[INFO]\033[0m'
ERROR = '\033[1;31m[ERROR]\033[0m'
WARNING = '\033[1;33m[WARNING]\033[0m'

# shapeInfo	contains an array of shape entries consisting of the latitude and longitude (in decimal degrees) and elevation (in decimal meters)
# info2wkt: 51.4965800/9.3862299/|51.4994700/9.3848799/ -> LINESTRING (9.3862299 51.49658, 9.3848799 51.49947)
info2wkt = lambda info: 'LINESTRING ({0})'.format(', '.join([
    "{0} {1}".format(node.split('/')[1],
                     node.split('/')[0]) for node in str(info).split('|')
]))
shape2utm = lambda shape: [(utm.from_latlon(c[1], c[0])[0],
                            utm.from_latlon(c[1], c[0])[1])
                           for c in shape.coords]
row2point = lambda row: "POINT ({0} {1})".format(str(row["longitude"]),
                                                 str(row["latitude"]))
caleast = lambda row: utm.from_latlon(row["latitude"], row["longitude"])[0]
calnorth = lambda row: utm.from_latlon(row["latitude"], row["longitude"])[1]


def deleteDuplicate():
    probes = pd.read_csv(data_probes_csv)
    print(probes.shape)
    probes.drop_duplicates(inplace=True)
    print(probes.shape)
    probes.to_csv(os.path.join(data_probes_csv))
Ejemplo n.º 47
0
    except Exception:
        print('File', file, 'not opened.')
        sys.exit(0)

    print(file)
    if (len(times) <= 0):
        print("no tides")
        sys.exit(1)

    line = f.readline()
    while (line):
        toks = line.split()
        if (len(toks) == 5):
            n = float(toks[0])
            e = float(toks[1])
            tm = int(int(toks[4]) / 1e3)
            tide = getTide(tm)
            toWlev = float(toks[3]) + tide
        else:
            cnt = 0
            while (cnt < len(toks)):
                lat = n + float(toks[cnt])
                lon = e + float(toks[cnt + 1])
                dpt = float(toks[cnt + 2]) + toWlev
                u = utm.from_latlon(lat, lon)
                outputstr = "%.2f %.2f %.2f\n" % (u[0], u[1], dpt * -1.0)
                outfile.write(outputstr)
                cnt += 5
        line = f.readline()

    f.close()
Ejemplo n.º 48
0
Archivo: bag.py Proyecto: feng1126/work
    def __init__(self, bag_path):
        self.bridge = CvBridge()
        # create bagfile
        #if os.path.isdir('./image_0'):
        #    pass
        #    else:
        #        os.mkdir('./image_0')
        #if os.path.isdir('./image_1'):
        #pass
        #else:
        #os.mkdir('./image_1')

        fobj = open('times.txt', 'w')
        gps = open('gps.txt', 'w')

        if not os.path.isdir('./left'):
            os.mkdir('./left')
        if not os.path.isdir('./right'):
            os.mkdir('./right')
        with rosbag.Bag(bag_path, 'r') as bag:  #要读取的bag文件;
            index1 = 0
            index2 = 0
            for topic, msg, t in bag.read_messages():
                if topic == "/left/image_raw_color":  #图像的topic;
                    try:
                        cv_image = self.bridge.imgmsg_to_cv2(msg, "mono8")
                    except CvBridgeError as e:
                        print e
                    timestr = "%.6f" % msg.header.stamp.to_sec()
                    #%.6f表示小数点后带有6位,可根据精确度需要修改;
                    #image_name = "./image_0/L_" + timestr+ ".jpg" #图像命名:时间戳.jpg
                    #timestr = "%.6d" % index1
                    #index1 = index1 + 1
                    image_name = "./left/" + timestr + ".png"
                    cv2.imwrite(image_name, cv_image)  #保存;
                    fobj.write(timestr + '\n')
                if topic == "/right/image_raw_color":  #图像的topic;
                    try:
                        #cv_image = self.bridge.imgmsg_to_cv2(msg,"bgr8")
                        cv_image = self.bridge.imgmsg_to_cv2(msg, "mono8")
                    except CvBridgeError as e:
                        print e
                    timestr = "%.6f" % msg.header.stamp.to_sec()
                    #%.6f表示小数点后带有6位,可根据精确度需要修改;
                    #image_name = "./image_1/R_" + timestr+ ".jpg" #图像命名:时间戳.jpg
                    #timestr = "%.6d" % index2
                    #index2 = index2 + 1
                    image_name = "./right/" + timestr + ".png"
                    cv2.imwrite(image_name, cv_image)  #保存;
                if topic == "/fix":  #图像的topic;
                    timestr = "%.6f" % msg.header.stamp.to_sec()
                    #print(timestr)
                    longitude = "%.10f" % msg.longitude
                    latitude = "%.10f" % msg.latitude
                    altitude = "%.10f" % msg.altitude
                    #print(latitude+' '+longitude)
                    utm_xyz = utm.from_latlon(float(latitude),
                                              float(longitude))
                    #print(utm_xyz)

                    # GC2KGPRC(float(longitude),float(latitude))
                    gps.write(timestr + ' ' + str("%.10f" % utm_xyz[0]) + ' ' +
                              str("%.10f" % utm_xyz[1]) + ' ' + altitude +
                              ' 0 0 0 1' + '\n')
Ejemplo n.º 49
0
    0, '/Users/nijiang/Documents/JavaWorkSpace/pyTest/terrautils-master/')
from terrautils.betydb import get_site_boundaries

# Scanalyzer -> MAC formular @ https://terraref.gitbooks.io/terraref-documentation/content/user/geospatial-information.html
# Mx = ax + bx * Gx + cx * Gy
# My = ay + by * Gx + cy * Gy
# Gx = ( (My/cy - ay/cy) - (Mx/cx - ax/cx) ) / (by/cy - bx/cx)
# Gy = ( (My/by - ay/by) - (Mx/bx - ax/bx) ) / (cy/by - cx/bx)
SE_latlon = (33.07451869, -111.97477775)
ay = 3659974.971
by = 1.0002
cy = 0.0078
ax = 409012.2032
bx = 0.009
cx = -0.9986
SE_utm = utm.from_latlon(SE_latlon[0], SE_latlon[1])
lng_shift = 0.000020308287
lat_shift = 0.000015258894

"reference coordinates of ranges from TERRA_sorghum_field_book"
# comes from https://docs.google.com/spreadsheets/d/1eQSeVMPfrWS9Li4XlJf3qs2F8txmddbwZhjOfMGAvt8/edit#gid=1765508846
_x_range = [
    (0, 0, 0), (2.3, 3.8, 5.3), (6.3, 7.8, 9.3), (10.26, 11.76, 13.26),
    (14.22, 15.72, 17.22), (18.12, 19.62, 21.12), (22.21, 23.71, 25.21),
    (26.26, 27.76, 29.26), (30.16, 31.66, 33.16), (33.97, 35.47, 36.97),
    (38.14, 39.64, 41.14), (42.12, 43.62, 45.12), (46.24, 47.74, 49.24),
    (50.21, 51.71, 53.21), (54.16, 55.66, 57.16), (57.98, 59.48, 60.98),
    (61.86, 63.36, 64.86), (66.03, 67.53, 69.03), (70.03, 71.53, 73.03),
    (73.89, 75.39, 76.89), (77.85, 79.35, 80.85), (81.62, 83.12, 84.62),
    (85.86, 87.36, 88.86), (89.68, 91.18, 92.68), (93.86, 95.36, 96.86),
    (97.64, 99.14, 100.64), (101.72, 103.22, 104.72), (105.94, 107.44, 108.94),
Ejemplo n.º 50
0
plot_y = dfCoor['Y_Plot'].values.tolist()
longi = dfCoor['Longitude'].values.tolist()
lati = dfCoor['Latitude'].values.tolist()
alti = dfCoor['Altitude'].values.tolist()
try:
    # Create final output file
    writer = csv.writer(finalFile, delimiter=',', lineterminator='\n')
    # Header row if needed
    writer.writerow(
        ('Plot_ID', 'Mean', 'Mode', 'Range', 'Column', 'NonZeroCount',
         'H_Distance', 'H_Degree', 'V_Degree', 'Original_File',
         'Plot_Center_Longitude', 'Plot_Center_Latitude', 'Camera_Longitude',
         'Camera_Latitude', 'Time_Stamp'))
    for i in range(len(pID)):
        [utmPlotX, utmPlotY, latPlotZone,
         longPlotZone] = utm.from_latlon(plot_y[i], plot_x[i])
        # print(utm.from_latlon(plot_y[i],plot_x[i]))
        [utmCamX, utmCamY, latCamZone,
         longCamZone] = utm.from_latlon(lati[i], longi[i])
        point_plot = np.array((utmPlotX, utmPlotY))
        point_cam = np.array((utmCamX, utmCamY))
        diffDist = np.sqrt(np.sum((point_plot - point_cam)**2))
        vDist = 18  #np.abs(alti[i] - ground_ref)
        h_dg = np.arctan2(utmCamY - utmPlotY, utmCamX - utmPlotX) * 180 / np.pi
        v_dg = np.arctan2(diffDist, vDist) * 180 / np.pi
        #         rangei = pID[i].split("$")[1]
        #         rowi = pID[i].split("$")[2]
        #         pID[i] = pID[i].split("$")[0]
        ts = imgFile[i].split("_")[1]
        writer.writerow(
            (pID[i], '{0:.3f}'.format(np.double(vi[i])),
	slope = -65.0/90
	dist=pdist([srcxy,destxy])
	dist = dist/3
	print ("dist scaled by 3: %f" %dist)
	dist = min(dist, 500)
	print ("dist : %f" %dist)
	rssi = 65 + dist*slope
	print ("rssi : %f" %rssi)
	#rssi = dist 
	rssi = rssi + 2*random.random()
	print ("rssi with noise : %f" %rssi)
 	return rssi

gps_ref_lat=-35.371
gps_ref_lon=149.177
utm_ref = utm.from_latlon(gps_ref_lat, gps_ref_lon)
cmd_rssisim.loadrssi_map_f()
print (cmd_rssisim.rssilist_map[0])

def rssi_xygps(srcxy, destgps):
	destxy = gps_to_local(destgps[0], destgps[1])
	print("dest x,y: %f %f" %(destxy[0],destxy[1]))
	rssiv = rssi_internal(srcxy, destxy)
 	return rssiv, srcxy

def local_to_gps(x, y):
	lat, lon = utm.to_latlon(utm_ref[0]+x, utm_ref[1]+y, utm_ref[2], utm_ref[3])
 	return [lat,lon]

def gps_to_local(lat, lon):
#  the haversine is not consistent with the conversion funcs in utm package 
Ejemplo n.º 52
0
    count += 1
elif (geom.GetGeometryName() == 'POLYGON'):
  ring = geom.GetGeometryRef(0)
  numpoints = ring.GetPointCount()
  pointsX = []; pointsY = []
  for p in range(numpoints):
    lon, lat, z = ring.GetPoint(p)
    pointsX.append(lon)
    pointsY.append(lat)
else:
  sys.exit("ERROR: Geometry needs to be either Polygon or Multipolygon")

testlon = pointsX[0]
testlat = pointsY[0]

utmzone = utm.from_latlon(testlat, testlon)[2]

targetSR = osr.SpatialReference()
targetSR.ImportFromEPSG(32600 + utmzone)
coordTrans = osr.CoordinateTransformation(sourceSR ,targetSR)

geom.Transform(coordTrans)

# Get extent of feature
geom = feat.GetGeometryRef()
if (geom.GetGeometryName() == 'MULTIPOLYGON'):
  count = 0
  pointsX = []; pointsY = []
  for polygon in geom:
    geomInner = geom.GetGeometryRef(count)
    ring = geomInner.GetGeometryRef(0)
def latlong_to_utm_center(center):
    lat, long = center[0], center[1]
    center_utm = utm.from_latlon(lat, long)

    return center_utm[:2]
Ejemplo n.º 54
0
    def merge_and_crop(self,
                       others,
                       bands,
                       bbox,
                       as_float=False,
                       out_dir=None,
                       verbose=True):

        if out_dir is None:
            out_dir = './'

        ul_x, ul_y, lr_x, lr_y = bbox

        assert ul_x < lr_x
        assert ul_y > lr_y

        # determine UTM coordinate system of top left corner
        ul_e, ul_n, utm_number, utm_letter = utm.from_latlon(latitude=ul_y,
                                                             longitude=ul_x)

        # bottom right
        lr_e, lr_n, _, _ = utm.from_latlon(latitude=lr_y,
                                           longitude=lr_x,
                                           force_zone_number=utm_number)

        utm_proj4 = "+proj=utm +zone={zone} +{hemisphere} +datum=WGS84 +ellps=WGS84" \
                    .format(zone=utm_number, hemisphere=('south', 'north')[ul_y > 0])

        acquisition_date = self.acquisition_date
        sat = self.sat
        # proj4 = self.proj4

        for other in others:
            assert isinstance(other, HLS2), other
            assert acquisition_date == other.acquisition_date, (
                acquisition_date, other.acquisition_date)
            assert sat == other.sat, (sat, other.sat)

        for band in bands:
            srcs = []

            srcs.append(
                self.export_band(band,
                                 as_float=as_float,
                                 out_dir=out_dir,
                                 force_utm_zone=utm_number))
            assert _exists(srcs[-1]), srcs[-1]

            for other in others:
                srcs.append(
                    other.export_band(band,
                                      as_float=as_float,
                                      out_dir=out_dir,
                                      force_utm_zone=utm_number))
                assert _exists(srcs[-1]), srcs[-1]

            vrt_fn = self.identifier.split('.')
            vrt_fn[2] = 'XXXXXX'
            vrt_fn[-1] = 'vrt'

            vrt_fn.insert(-1, '_{}_UTM{}'.format(band, utm_number))
            vrt_fn = '.'.join(vrt_fn)
            vrt_fn = _join(out_dir, vrt_fn)
            fname = vrt_fn[:-4] + '.tif'

            cmd = ['gdalbuildvrt', vrt_fn] + srcs

            print('cmd', cmd)

            _log = open(vrt_fn + '.err', 'w')
            p = Popen(cmd, stdout=_log, stderr=_log)
            p.wait()
            _log.close()

            if _exists(vrt_fn):
                os.remove(vrt_fn + '.err')

            cmd = [
                'gdalwarp', '-te', ul_e, lr_n, lr_e, ul_n, '-t_srs', utm_proj4,
                '-co', 'compress=DEFLATE', '-co', 'zlevel=9', vrt_fn, fname
            ]
            cmd = [str(v) for v in cmd]

            print('cmd', cmd)

            _log = open(fname + '.err', 'w')
            p = Popen(cmd, stdout=_log, stderr=_log)
            p.wait()
            _log.close()

            if _exists(fname):
                os.remove(fname + '.err')
                for src in srcs:
                    os.remove(src)
                os.remove(vrt_fn)
Ejemplo n.º 55
0
def talker():
    pub = rospy.Publisher('gps_chat', yu, queue_size=10)
    rospy.init_node('gps_xy', anonymous=True)

    portgps = "$GPGGA,134658.00,5106.9792,N,11402.3003,W,2,09,1.0,1048.47,M,-16.27,M,08,AAAA*60"
    # portgps = serial.Serial('/dev/xxxxxxxxx', baudrate=4800)
    # portimu = serial.Serial('/dev/xxxxxxxxx', baudrate=115200)
    portimu = "VNYMR,-045.286,-003.093,+000.408,+00.2007,+00.1929,+00.3346,-00.498,-00.047,-09.705,-00.00143,+00.012139,-00.000167*67"

    while not rospy.is_shutdown():

        # datagps = portgps.readline()
        # dataimu = portimu.readline()

        datagps = portgps
        dataimu = portimu

        if (datagps == "" or dataimu == ""):
            rospy.logwarn("no data")
        else:
            if (datagps.startswith('$GPGGA') and dataimu.startswith('VNYMR')):
                datasplitgps = datagps.split(",")
                datasplitimu = dataimu.split(",")
                ##############gps##############
                latitute = datasplitgps[2]
                latitute_deg = float(latitute[0:2])
                latitute_min = float(latitute[2:])
                latitute = float(latitute)
                latitute_utm = latitute_deg + latitute_min
                longitite = datasplitgps[4]
                longitite_deg = float(longitite[0:3])
                longitite_min = float(longitite[3:])
                longitite = float(longitite)
                longitite_utm = longitite_deg + longitite_min
                altt = float(datasplitgps[9])
                # REF : https://github.com/Turbo87/utm
                utm_res = utm.from_latlon(latitute_utm, longitite_utm)
                ##############gps##############
                ##############imu##############
                yaw = float(datasplitimu[1])
                pitch = float(datasplitimu[2])
                roll = float(datasplitimu[3])
                magnetic_x = float(datasplitimu[4])
                magnetic_y = float(datasplitimu[5])
                magnetic_z = float(datasplitimu[6])
                acc_x = float(datasplitimu[7])
                acc_y = float(datasplitimu[8])
                acc_z = float(datasplitimu[9])
                angular_x = float(datasplitimu[10])
                angular_y = float(datasplitimu[11])
                l = len(datasplitimu[12])
                temp = datasplitimu[12]
                angular_z = float(temp[:l - 3])

                cy = math.cos(yaw * 0.5)
                sy = math.sin(yaw * 0.5)
                cp = math.cos(pitch * 0.5)
                sp = math.sin(pitch * 0.5)
                cr = math.cos(roll * 0.5)
                sr = math.sin(roll * 0.5)

                w = cy * cp * cr + sy * sp * sr
                x = cy * cp * sr - sy * sp * cr
                y = sy * cp * sr + cy * sp * cr
                z = sy * cp * cr - cy * sp * sr
                ##############imu##############
                msg = yu()
                msg.header = "gpgga"
                msg.latitude = latitute
                msg.longitude = longitite
                msg.alt = altt
                msg.utm_easting = utm_res[0]
                msg.utm_northing = utm_res[1]
                msg.zone = utm_res[2]
                msg.letter = utm_res[3]

                imu_x = x
                imu_y = y
                imu_z = z
                imu_w = w
                msg.magnetic_x = magnetic_x
                msg.magnetic_y = magnetic_y
                msg.magnetic_z = magnetic_z
                msg.acc_x = acc_x
                msg.acc_y = acc_y
                msg.acc_z = acc_z
                msg.angular_x = angular_x
                msg.angular_y = angular_y
                msg.angular_z = angular_z

                rospy.loginfo(msg)
                pub.publish(msg)
Ejemplo n.º 56
0
 def proj(x, y):
     return utm.from_latlon(y, x)[:2]
Ejemplo n.º 57
0
Archivo: grid.py Proyecto: silvioda/rtm
def define_grid(lon_0,
                lat_0,
                x_radius,
                y_radius,
                spacing,
                projected=False,
                plot_preview=False):
    """
    Define the spatial grid of trial source locations. Grid can be defined in
    either a latitude/longitude or projected UTM (i.e., Cartesian) coordinate
    system.

    Args:
        lon_0: [deg] Longitude of grid center
        lat_0: [deg] Latitude of grid center
        x_radius: [deg or m] Distance from lon_0 to edge of grid (i.e., radius)
        y_radius: [deg or m] Distance from lat_0 to edge of grid (i.e., radius)
        spacing: [deg or m] Desired grid spacing
        projected: Boolean controlling whether a latitude/longitude or UTM grid
                   is used. If True, a UTM grid is used and the units of
                   x_radius, y_radius, and spacing are interpreted in meters
                   instead of in degrees (default: False)
        plot_preview: Toggle plotting a preview of the grid for reference and
                      troubleshooting (default: False)
    Returns:
        grid_out: xarray.DataArray object containing the grid coordinates and
                  metadata
    """

    print('-------------')
    print('DEFINING GRID')
    print('-------------')

    # Make coordinate vectors
    if projected:
        x_0, y_0, zone_number, _ = utm.from_latlon(lat_0, lon_0)
    else:
        x_0, y_0, = lon_0, lat_0
    # Using np.linspace() in favor of np.arange() due to precision advantages
    x, dx = np.linspace(x_0 - x_radius,
                        x_0 + x_radius,
                        int(2 * x_radius / spacing) + 1,
                        retstep=True)
    y, dy = np.linspace(y_0 - y_radius,
                        y_0 + y_radius,
                        int(2 * y_radius / spacing) + 1,
                        retstep=True)

    # Basic grid checks
    if dx != spacing:
        warnings.warn(
            f'Requested spacing of {spacing} does not match '
            f'np.linspace()-returned x-axis spacing of {dx}.', RTMWarning)
    if dy != spacing:
        warnings.warn(
            f'Requested spacing of {spacing} does not match '
            f'np.linspace()-returned y-axis spacing of {dy}.', RTMWarning)
    if not (x_0 in x and y_0 in y):
        warnings.warn(
            '(x_0, y_0) is not located in grid. Check for numerical '
            'precision problems (i.e., rounding error).', RTMWarning)
    if projected:
        # Create list of grid corners
        corners = dict(SW=(x.min(), y.min()),
                       NW=(x.min(), y.max()),
                       NE=(x.max(), y.max()),
                       SE=(x.max(), y.min()))
        for label, corner in corners.items():
            # "Un-project" back to latitude/longitude
            lat, lon = utm.to_latlon(*corner,
                                     zone_number,
                                     northern=lat_0 >= 0,
                                     strict=False)
            # "Re-project" to UTM
            _, _, new_zone_number, _ = utm.from_latlon(lat, lon)
            if new_zone_number != zone_number:
                warnings.warn(
                    f'{label} grid corner locates to UTM zone '
                    f'{new_zone_number} instead of origin UTM zone '
                    f'{zone_number}. Consider reducing grid extent '
                    'or using an unprojected grid.', RTMWarning)

    # Create grid
    data = np.full((y.size, x.size), np.nan)  # Initialize an array of NaNs
    # Add grid "metadata"
    attrs = dict(grid_center=(lon_0, lat_0),
                 x_radius=x_radius,
                 y_radius=y_radius,
                 spacing=spacing)
    if projected:
        # Add the projection information to the grid metadata
        attrs['UTM'] = dict(zone=zone_number, southern_hemisphere=lat_0 < 0)
    else:
        attrs['UTM'] = None
    grid_out = DataArray(data, coords=[('y', y), ('x', x)], attrs=attrs)

    print('Done')

    # Plot grid preview, if specified
    if plot_preview:
        print('Generating grid preview plot...')
        if projected:
            proj = ccrs.UTM(**grid_out.UTM)
            transform = proj
        else:
            # This is a good projection to use since it preserves area
            proj = ccrs.AlbersEqualArea(central_longitude=lon_0,
                                        central_latitude=lat_0,
                                        standard_parallels=(y.min(), y.max()))
            transform = ccrs.PlateCarree()

        fig, ax = plt.subplots(figsize=(10, 10),
                               subplot_kw=dict(projection=proj))

        _plot_geographic_context(ax=ax, utm=projected)

        # Note that trial source locations are at the CENTER of each plotted
        # grid box
        grid_out.plot.pcolormesh(ax=ax,
                                 transform=transform,
                                 edgecolor='black',
                                 add_colorbar=False)

        # Plot the center of the grid
        ax.scatter(lon_0,
                   lat_0,
                   s=50,
                   color='limegreen',
                   edgecolor='black',
                   label='Grid center',
                   transform=ccrs.Geodetic())

        # Add a legend
        ax.legend(loc='best')

        fig.canvas.draw()  # Needed to make fig.tight_layout() work
        fig.tight_layout()
        fig.show()

        print('Done')

    return grid_out
Ejemplo n.º 58
0
def background_map_utm(utmX,
                       utmY,
                       zone_number,
                       zone_letter,
                       maptype='satellite',
                       **kargs):
    """Queries the proper background map and translate geo coordinated into pixel locations on this map.

    :param pandas.Series utmX: series of sample easting
    :param pandas.Series utmY: series of sample northing
    :param int zone_number: number of the zone in the globe
    :param string zone_letter: string with 'S' or 'N'
    :param string maptype: type of maps, see GoogleStaticMapsAPI docs for more info
    :param string xlabel: x label
    :param string ylabel: y label
    :param int zoom level: 14 to get extra area

    :return: fig and ax
    :rtype: (PIL.Image, pandas.DataFrame)
        """
    ############################################################################
    # Convert UTM to WGS84 because google maps api only acepts tuple (lat,lon) #
    ############################################################################
    lat = []
    lon = []

    for i in range(len(utmX)):
        latlon = pd.Series(
            utm.to_latlon(utmX[i], utmY[i], zone_number, zone_letter))
        lat.append(latlon[0])
        lon.append(latlon[1])

    ###########################
    # Default optional values #
    ###########################
    xlabel = kargs.pop('xlabel', 'East - Local Cordinates (UTM-REF) [m]')
    ylabel = kargs.pop('ylabel', 'North - Local Cordinates (UTM-REF) [m]')

    ################################################
    # latitudes and longitudes from panda database #
    ################################################
    d_wgs = {'latitudes': lat, 'longitudes': lon}
    df_wgs = pd.DataFrame(data=d_wgs)

    latitudes = df_wgs['latitudes']
    longitudes = df_wgs['longitudes']

    ##########################################################
    # From lat/long to pixels, zoom and position in the tile #
    ##########################################################
    center_lat = (latitudes.max() + latitudes.min()) / 2
    center_long = (longitudes.max() + longitudes.min()) / 2
    zoom = kargs.pop('zoom',
                     GoogleStaticMapsAPI.get_zoom(latitudes, longitudes,
                                                  MAX_SIZE,
                                                  SCALE))  # default zoom value

    ##############
    # Google Map #
    ##############
    img = GoogleStaticMapsAPI.map(
        center=(center_lat, center_long),
        zoom=zoom,
        scale=SCALE,
        size=(MAX_SIZE, MAX_SIZE),
        maptype=maptype,
    )

    centerPixelY = round(img.height / 2)
    centerPixelx = round(img.width / 2)

    ##################################################################
    # lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913 #
    ##################################################################
    [mx, my] = GoogleStaticMapsAPI.LatLonToMeters(center_lat, center_long)
    curResolution = INITIALRESOLUTION / 2**zoom / SCALE

    # x and y vector
    xVec = mx + (np.arange(img.width) - centerPixelx) * curResolution
    yVec = my + (np.arange(img.height) - centerPixelY) * curResolution

    ######################
    # Convert to lat lon #
    ######################
    lat_north, lon_east = GoogleStaticMapsAPI.MetersToLatLon(xVec, yVec)

    #Convert to UTM
    east = []
    north = []
    for i in range(len(lat_north)):
        eastNorth = utm.from_latlon(lat_north[i],
                                    lon_east[i],
                                    force_zone_number=zone_number)
        east.append(eastNorth[0])
        north.append(eastNorth[1])

    #################
    # fig,ax output #
    #################
    fig, ax = plt.subplots()
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)

    ax.imshow(np.flip(np.array(img), 0),
              extent=[east[0], east[-1], north[0], north[-1]],
              origin='lower')
    ax.axis([east[50], east[-1], north[50], north[-1]])

    return fig, ax
Ejemplo n.º 59
0
    def test_inside(self):
        self.assert_zone_equal(UTM.from_latlon(56, 3), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(56, 6), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(56, 9), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(56, 11.999999), 32, 'V')

        self.assert_zone_equal(UTM.from_latlon(60, 3), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(60, 6), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(60, 9), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(60, 11.999999), 32, 'V')

        self.assert_zone_equal(UTM.from_latlon(63.999999, 3), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(63.999999, 6), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(63.999999, 9), 32, 'V')
        self.assert_zone_equal(UTM.from_latlon(63.999999, 11.999999), 32, 'V')
Ejemplo n.º 60
0
                if cv2.contourArea(c)>=contour_min_size and cv2.contourArea(c)<=contour_max_size and shape == "square": # or shape == "rectangle"):
                    break
            # Matching GCPs
            M = cv2.moments(c)
            if M["m00"] != 0:
                # Calculate the center of the tile
                cX = int(round((M["m10"] / M["m00"]) * ratio))  # * ratio
                cY = int(round((M["m01"] / M["m00"]) * ratio))  # * ratio
                # print("Image: %s, cX: %d, cY: %d, cArea: %.1f" % (imf, cX, cY, contourSize))
            else:
                cX = 0
                cY = 0
            if cX != 0 and cY != 0 and (cX>int(img_width*0.1) and cX<int(img_width*0.9)) and (cY>int(img_height*0.1) and cY<int(img_height*0.9)):
                # get image EXIF
                [longi,lati,alti] = getExifFromImage(srcImagePath+imf)
                [uTMx,uTMy,longZone,latZone] = utm.from_latlon(lati, longi)
                pointCamCenter = numpy.array((uTMx,uTMy))
                pointImgCenter = numpy.array((sh_width-1, sh_height-1))
                pointGcpCenter = numpy.array((cX, cY))
                pixelDist2ImgCenter = numpy.sqrt(numpy.sum((pointGcpCenter - pointImgCenter) ** 2))
                thDist = 12*2.54*0.01*4*0.9/contourSize*pixelDist2ImgCenter
#                 print("Pixel distance: %.2f, Contour size: %f, Real distance: %.2f" 
#                       % (pixelDist2ImgCenter, contourSize, thDist))
                print (imf)
                [gcpLongitude, gcpLatitude, gcpAltitude, gcpLab] = matachGCP(thDist, pointCamCenter, srcGCPFile)
                print("gcplong: %f, gcpLat: %f, gcpAlt: %f" % (gcpLongitude, gcpLatitude, gcpAltitude))
                # Output
                if gcpLongitude != 0 and gcpLatitude != 0 and gcpAltitude != 0:
                    writer.writerow((imf, gcpLab, cX, cY, gcpLongitude, gcpLatitude, gcpAltitude))
                
        elif snum == 2: