def create_domain_crs(self) -> CRS: proj = self.get_proj_kwargs() if proj is None: raise UserError('Incomplete projection definition') map_proj = proj['map_proj'] if map_proj == 'lambert': if self.center_lat.is_valid(): origin_lat = self.center_lat.value() else: origin_lat = 0 crs = CRS.create_lambert(proj['truelat1'], proj['truelat2'], LonLat(proj['stand_lon'], origin_lat)) elif map_proj == 'polar': crs = CRS.create_polar(proj['truelat1'], proj['stand_lon']) elif map_proj == 'mercator': if self.center_lon.is_valid(): origin_lon = self.center_lon.value() else: origin_lon = 0 crs = CRS.create_mercator(proj['truelat1'], origin_lon) elif map_proj == 'lat-lon': crs = CRS.create_lonlat() else: assert False, 'unknown proj: ' + map_proj return crs
def set_domain_to_extent(self, crs: QgsCoordinateReferenceSystem, extent: QgsRectangle) -> None: resolution = self.resolution.value() bbox = rect_to_bbox(extent) extent_crs = CRS(crs.toProj4()) domain_crs = self.create_domain_crs() domain_srs = domain_crs.srs domain_bbox = domain_crs.transform_bbox(bbox, domain_srs) # TODO disallow creation of bounding box outside projection range (e.g. for lat-lon 360-180) xmin, xmax, ymin, ymax = domain_bbox.minx, domain_bbox.maxx, domain_bbox.miny, domain_bbox.maxy center_x = xmin + (xmax - xmin) / 2 center_y = ymin + (ymax - ymin) / 2 center_lonlat = domain_crs.to_lonlat(Coordinate2D(center_x, center_y)) self.center_lat.set_value(center_lonlat.lat) self.center_lon.set_value(center_lonlat.lon) self.resolution.set_value(resolution) cols = ceil((xmax - xmin) / resolution) rows = ceil((ymax - ymin) / resolution) self.cols.set_value(cols) self.rows.set_value(rows) self.on_change_any_field(zoom_out=True)
def test_projection_origin(crs_name: str): origin_lonlat = LonLat(lon=4, lat=0) if crs_name == 'lambert': crs = CRS.create_lambert(truelat1=3.5, truelat2=7, origin=origin_lonlat) elif crs_name == 'mercator': crs = CRS.create_mercator(truelat1=3.5, origin_lon=origin_lonlat.lon) elif crs_name == 'polar': origin_lonlat = LonLat(lon=origin_lonlat.lon, lat=90) crs = CRS.create_polar(truelat1=3.5, origin_lon=origin_lonlat.lon) elif crs_name == 'albers_nad83': crs = CRS.create_albers_nad83(truelat1=3.5, truelat2=7, origin=origin_lonlat) origin_xy = crs.to_xy(origin_lonlat) assert origin_xy.x == pytest.approx(0) assert origin_xy.y == pytest.approx(0, abs=1e-9)
def on_extent_from_active_layer_button_clicked(self): layer = self.iface.activeLayer() # type: Optional[QgsMapLayer] if layer is None: return layer_crs = CRS(layer.crs().toProj4()) target_crs = CRS('+proj=latlong +datum=WGS84') extent = layer.extent() # type: QgsRectangle bbox = rect_to_bbox(extent) bbox_geo = layer_crs.transform_bbox(bbox, target_crs.srs) padding = 5 # degrees lat_south = max(bbox_geo.miny - 5, -90) lat_north = min(bbox_geo.maxy + 5, 90) lon_west = max(bbox_geo.minx - 5, -180) lon_east = min(bbox_geo.maxx + 5, 180) self.bottom.set_value(lat_south) self.top.set_value(lat_north) self.left.set_value(lon_west) self.right.set_value(lon_east)
def create_domain_crs(self) -> CRS: proj = self.get_proj_kwargs() if proj is None: raise ValueError('Incomplete projection definition') origin_valid = all( map(lambda w: w.is_valid(), [self.center_lat, self.center_lon])) if origin_valid: origin = LonLat(self.center_lon.value(), self.center_lat.value()) else: origin = LonLat(0, 0) if proj['map_proj'] == 'lambert': crs = CRS.create_lambert(proj['truelat1'], proj['truelat2'], origin) elif proj['map_proj'] == 'lat-lon': crs = CRS.create_lonlat() else: raise NotImplementedError('unknown proj: ' + proj['map_proj']) return crs
def test_geo_roundtrip(crs_name: str): if crs_name == 'lonlat': crs = CRS.create_lonlat() elif crs_name == 'lambert': crs = CRS.create_lambert(truelat1=3.5, truelat2=7, origin=LonLat(lon=4, lat=0)) elif crs_name == 'mercator': crs = CRS.create_mercator(truelat1=3.5, origin_lon=4) elif crs_name == 'polar': crs = CRS.create_polar(truelat1=3.5, origin_lon=4) elif crs_name == 'albers_nad83': crs = CRS.create_albers_nad83(truelat1=3.5, truelat2=7, origin=LonLat(lon=4, lat=0)) lonlat = LonLat(lon=10, lat=30) xy = crs.to_xy(lonlat) lonlat2 = crs.to_lonlat(xy) assert lonlat.lon == pytest.approx(lonlat2.lon) assert lonlat.lat == pytest.approx(lonlat2.lat)