def _get_boundbox_corners(self): easts = [x.easting for x in self.corners] norths = [x.northing for x in self.corners] mineast = min(easts) maxeast = max(easts) minnorth = min(norths) maxnorth = max(norths) zone_number = self.corners[0].zone_number zone_letter = self.corners[0].zone_letter corner0 = utm.to_latlon(maxeast, minnorth, zone_number, zone_letter=zone_letter) self.sec = MapCoords(float(corner0[0]), float(corner0[1])) corner1 = utm.to_latlon(mineast, maxnorth, zone_number, zone_letter=zone_letter) self.nwc = MapCoords(float(corner1[0]), float(corner1[1])) corner2 = utm.to_latlon(mineast, minnorth, zone_number, zone_letter=zone_letter) self.swc = MapCoords(float(corner2[0]), float(corner2[1])) corner3 = utm.to_latlon(maxeast, maxnorth, zone_number, zone_letter=zone_letter) self.nec = MapCoords(float(corner3[0]), float(corner3[1])) return np.ceil(maxeast - mineast), np.ceil(maxnorth - minnorth)
def __init__(self, lat_deg, lon_deg, zoom, size): self.size = size self.zoom = zoom self.res = get_res(self.size, self.zoom, lat_deg) self.limits = [] self.centre = MapCoords(lat_deg, lon_deg) self.base_image = getImageCluster(self.centre.lat, self.centre.lon, self.zoom, self.size)
def _load_limits(self, limits_fn): limits = [] f = open(limits_fn, 'r') for line in f: line = line.strip('\n') a = line.split(',') limits.append(MapCoords(float(a[0]), float(a[1]))) self.set_limits(limits)
def _get_grid_corners(self): self.grid = [] mineast = self.limits[0].easting minnorth = self.limits[0].northing maxeast = self.limits[0].easting maxnorth = self.limits[0].northing zone_number = self.limits[0].zone_number zone_letter = self.limits[0].zone_letter for i in self.limits: if i.easting < mineast: mineast = i.easting if i.northing < minnorth: minnorth = i.northing if i.easting > maxeast: maxeast = i.easting if i.northing > maxnorth: maxnorth = i.northing corner0 = utm.to_latlon(maxeast, minnorth, zone_number, zone_letter=zone_letter) self.sec = MapCoords(float(corner0[0]), float(corner0[1])) corner1 = utm.to_latlon(mineast, maxnorth, zone_number, zone_letter=zone_letter) self.nwc = MapCoords(float(corner1[0]), float(corner1[1])) corner2 = utm.to_latlon(mineast, minnorth, zone_number, zone_letter=zone_letter) self.swc = MapCoords(float(corner2[0]), float(corner2[1])) corner3 = utm.to_latlon(maxeast, maxnorth, zone_number, zone_letter=zone_letter) self.nec = MapCoords(float(corner3[0]), float(corner3[1])) self.corners.append(corner0) self.corners.append(corner1) self.corners.append(corner2) self.corners.append(corner3) return np.ceil(maxeast - mineast), np.ceil(maxnorth - minnorth)
def get_div_h_lines(self, number): divis = [] print("Finding %d horizontal lines" % number) dnorth = abs(self.nwc.northing - self.swc.northing) / (number + 1) #deast = abs(self.swc.easting - self.sec.easting)/(number+1) mineast = min(self.swc.easting, self.sec.easting) minnorth = min(self.swc.northing, self.sec.northing) #maxnorth = max(self.nwc.northing, self.nec.northing) maxeast = max(self.swc.easting, self.sec.easting) zone_number = self.swc.zone_number zone_letter = self.swc.zone_letter a = utm.to_latlon(mineast, minnorth, zone_number, zone_letter) east_orig = MapCoords(a[0], a[1]) a = utm.to_latlon(maxeast, minnorth, zone_number, zone_letter) west_orig = MapCoords(a[0], a[1]) for i in range(number): dif = dnorth * (i + 1) pointa = east_orig._get_rel_point(-100.0, dif) pointb = west_orig._get_rel_point(100.0, dif) divis.append([pointa, pointb]) return divis
def get_div_v_lines(self, number): divis = [] print("Finding %d vertical lines" % number) #dnorth = abs(self.swc.northing - self.sec.northing)/(number+1) deast = abs(self.swc.easting - self.sec.easting) / (number + 1) mineast = min(self.swc.easting, self.sec.easting) minnorth = min(self.swc.northing, self.sec.northing) maxnorth = max(self.nwc.northing, self.nec.northing) zone_number = self.swc.zone_number zone_letter = self.swc.zone_letter a = utm.to_latlon(mineast, minnorth, zone_number, zone_letter) east_orig = MapCoords(a[0], a[1]) a = utm.to_latlon(mineast, maxnorth, zone_number, zone_letter) north_orig = MapCoords(a[0], a[1]) for i in range(number): pointa = east_orig._get_rel_point(deast * (i + 1), -30.0) pointb = north_orig._get_rel_point(deast * (i + 1), 30.0) divis.append([pointa, pointb]) return divis
def _get_poly_centre(self): x = [] y = [] for i in self.corners: x.append(i.easting) y.append(i.northing) xc = np.average(np.asarray(x)) yc = np.average(np.asarray(y)) zone_number = self.corners[0].zone_number zone_letter = self.corners[0].zone_letter centreI = utm.to_latlon(xc, yc, zone_number, zone_letter=zone_letter) self.centre = MapCoords(float(centreI[0]), float(centreI[1]))
def _get_intersection(self, line1, line2): l1 = [[line1[0].northing, line1[0].easting], [line1[1].northing, line1[1].easting]] l2 = [[line2[0].northing, line2[0].easting], [line2[1].northing, line2[1].easting]] res, point = line_intersection(l1, l2) if res: #print point, line1[0] a = utm.to_latlon(point[1], point[0], line1[0].zone_number, line1[0].zone_letter) return MapCoords(a[0], a[1]) else: return None
def _get_poly_centroid(self): pol = [] for i in self.corners: d = {} d['x'] = i.easting d['y'] = i.northing pol.append(d) zone_number = self.corners[0].zone_number zone_letter = self.corners[0].zone_letter centreI = centroid_for_polygon(pol, self.area_size) centreI = utm.to_latlon(centreI['x'], centreI['y'], zone_number, zone_letter=zone_letter) self.centroid = MapCoords(float(centreI[0]), float(centreI[1]))
def _load_model_from_file(self, data_fn, name='default'): data = [] vals = [] print "open: " + data_fn f = open(data_fn, 'r') for line in f: line = line.strip('\n') a = line.split(';') b = MapCoords(float(a[0]), float(a[1])) # cx = int(np.floor((b.easting - self.swc.easting)/self.cell_size)) # cy = int(np.floor(((b.northing - self.swc.northing)/self.cell_size))) cx, cy = self.get_cell_inds_from_coords(b) data.append(KriggingDataPoint(b, (cx, cy), float(a[2]))) vals.append(float(a[2])) #lims= [np.min(vals), np.max(vals)] #a = KriggingData(self.shape, lims, name) a = KriggingData(self.shape, name) a.add_data(data) self.models.append(a)
def _sort_corners(self, polygon): polygon2 = [] #polygon[:] angles = [] mde = np.average([x.easting for x in polygon]) mdn = np.average([x.northing for x in polygon]) a = utm.to_latlon(mde, mdn, polygon[0].zone_number, polygon[0].zone_letter) mda = MapCoords(a[0], a[1]) for i in polygon: rr = mda - i angles.append(rr[1] + 180) angles2 = angles[:] angles.sort() for i in angles: ind = angles2.index(i) polygon2.append(polygon[ind]) return polygon2
def load_data_from_yaml(self, filename): with open(filename, 'r') as f: a = yaml.load(f) for i in range(len(a['names'])): print "---------------------------------------" print "creating model " + str(i) + " of " + str(len( a['names'])) + " " + a['names'][i] kd = KriggingData(self.shape, a['names'][i]) dt = [] for j in a['data']: if j['data'][i] > 0: #print j['position'] b = MapCoords(j['position']['lat'], j['position']['lon']) cx, cy = self.get_cell_inds_from_coords(b) if cx >= 0 and cy >= 0: dt.append(KriggingDataPoint(b, (cx, cy), j['data'][i])) print cx, cy #print b print j['data'][i] kd.add_data(dt) self.models.append(kd)
def _latlong2pix(self, lat, lon): point = MapCoords(lat, lon) deast, dnorth = self._coord2pix(point) return deast, dnorth
class SatelliteImage(object): def __init__(self, lat_deg, lon_deg, zoom, size): self.size = size self.zoom = zoom self.res = get_res(self.size, self.zoom, lat_deg) self.limits = [] self.centre = MapCoords(lat_deg, lon_deg) self.base_image = getImageCluster(self.centre.lat, self.centre.lon, self.zoom, self.size) def _coord2pix(self, point): dnorth = ((self.centre.northing - point.northing) / self.res) + (self.size / 2) deast = ( (point.easting - self.centre.easting) / self.res) + (self.size / 2) return deast, dnorth def _pix2coord(self, x, y): xcord = (x - (self.size / 2)) * self.res ycord = -(y - (self.size / 2)) * self.res click_coord = self.centre._get_rel_point(xcord, ycord) return click_coord def _latlong2pix(self, lat, lon): point = MapCoords(lat, lon) deast, dnorth = self._coord2pix(point) return deast, dnorth def draw_grid(self, grid, cell_size, colour, thickness=2): nx = len(grid) - 1 ny = len(grid[0]) - 1 for i in range(0, len(grid[0])): mx0, my0 = self._coord2pix(grid[0][i]._get_rel_point( -cell_size / 2, -cell_size / 2)) mx1, my1 = self._coord2pix(grid[nx][i]._get_rel_point( -cell_size / 2, cell_size / 2)) cv2.line(self.base_image, (int(mx0), int(my0)), (int(mx1), int(my1)), colour, thickness=thickness) mx0, my0 = self._coord2pix(grid[0][ny]._get_rel_point( cell_size / 2, -cell_size / 2)) mx1, my1 = self._coord2pix(grid[nx][ny]._get_rel_point( cell_size / 2, cell_size / 2)) cv2.line(self.base_image, (int(mx0), int(my0)), (int(mx1), int(my1)), colour, thickness=thickness) for i in range(0, len(grid)): mx0, my0 = self._coord2pix(grid[i][0]._get_rel_point( -cell_size / 2, -cell_size / 2)) mx1, my1 = self._coord2pix(grid[i][ny]._get_rel_point( cell_size / 2, -cell_size / 2)) cv2.line(self.base_image, (int(mx0), int(my0)), (int(mx1), int(my1)), colour, thickness=thickness) mx0, my0 = self._coord2pix(grid[nx][0]._get_rel_point( -cell_size / 2, cell_size / 2)) mx1, my1 = self._coord2pix(grid[nx][ny]._get_rel_point( cell_size / 2, cell_size / 2)) cv2.line(self.base_image, (int(mx0), int(my0)), (int(mx1), int(my1)), colour, thickness=thickness) def draw_cell(self, cell, cell_size, colour, thickness=2): mx0, my0 = self._coord2pix( cell._get_rel_point(-cell_size / 2, -cell_size / 2)) mx1, my1 = self._coord2pix( cell._get_rel_point(cell_size / 2, cell_size / 2)) cv2.rectangle(self.base_image, (int(mx0), int(my0)), (int(mx1), int(my1)), colour, thickness=thickness) def draw_coordinate(self, coord, colour, size=6, thickness=2): mx, my = self._coord2pix(coord) cv2.circle(self.base_image, (int(mx), int(my)), size, colour, thickness) def draw_list_of_coords(self, list_of_coords, colour, size=6, thickness=2): for i in list_of_coords: mx, my = self._coord2pix(i) cv2.circle(self.base_image, (int(mx), int(my)), size, colour, thickness) def draw_polygon(self, list_of_coords, colour, thickness=2): for i in range(1, len(list_of_coords)): mx0, my0 = self._coord2pix(list_of_coords[i - 1]) mx1, my1 = self._coord2pix(list_of_coords[i]) cv2.line(self.base_image, (int(mx0), int(my0)), (int(mx1), int(my1)), colour, thickness=thickness) mx0, my0 = self._coord2pix(list_of_coords[0]) cv2.line(self.base_image, (int(mx0), int(my0)), (int(mx1), int(my1)), colour, thickness=thickness)