Ejemplo n.º 1
0
    def _reproject(self, eopatch, src_raster):
        """
        Reprojects the raster data from Geopedia's CRS (POP_WEB) to EOPatch's CRS.
        """
        height, width = src_raster.shape

        dst_raster = np.ones((height, width), dtype=self.raster_dtype)

        src_bbox = transform_bbox(eopatch.bbox, CRS.POP_WEB)
        src_transform = rasterio.transform.from_bounds(*src_bbox,
                                                       width=width,
                                                       height=height)

        dst_bbox = eopatch.bbox
        dst_transform = rasterio.transform.from_bounds(*dst_bbox,
                                                       width=width,
                                                       height=height)

        rasterio.warp.reproject(
            src_raster,
            dst_raster,
            src_transform=src_transform,
            src_crs={'init': CRS.ogc_string(CRS.POP_WEB)},
            src_nodata=0,
            dst_transform=dst_transform,
            dst_crs={'init': CRS.ogc_string(eopatch.bbox.crs)},
            dst_nodata=self.no_data_val)

        return dst_raster
Ejemplo n.º 2
0
    def execute(self, eopatch):
        # convert to 3857 CRS
        bbox_3857 = transform_bbox(eopatch.bbox, CRS.POP_WEB)
        
        # get iterator over features
        gpd_iter = GeopediaFeatureIterator(layer=self.layer, bbox=bbox_3857)

        features = list(gpd_iter)
        if len(features):
            gdf = gpd.GeoDataFrame.from_features(features)
            gdf.crs = {'init': 'epsg:4326'}
            # convert back to EOPatch CRS
            gdf = gdf.to_crs({'init': f'epsg:{eopatch.bbox.crs.value}'})

            if self.year:
                # Filter by years
                gdf = gdf.loc[gdf[self.year_col_name].isin([self.year])]
            
            if self.drop_duplicates:
                sel = gdf.drop('geometry', axis=1)
                sel = sel.drop_duplicates()
                gdf = gdf.loc[sel.index]
                
            eopatch[self.feature_type][self.feature_name] = gdf 

        return eopatch
Ejemplo n.º 3
0
    def _get_wms_request(self, bbox, size_x, size_y):
        """
        Returns WMS request.
        """
        bbox_3857 = transform_bbox(bbox, CRS.POP_WEB)

        return GeopediaWmsRequest(layer=self.layer,
                                  theme=self.theme,
                                  bbox=bbox_3857,
                                  width=size_x,
                                  height=size_y,
                                  image_format=self.image_format,
                                  custom_url_params={CustomUrlParam.TRANSPARENT: True})
Ejemplo n.º 4
0
def show_splitter(splitter, alpha=0.2, area_buffer=0.2, show_legend=False):
    area_bbox = splitter.get_area_bbox()
    minx, miny, maxx, maxy = area_bbox
    lng, lat = area_bbox.get_middle()
    w, h = maxx - minx, maxy - miny
    minx = minx - area_buffer * w
    miny = miny - area_buffer * h
    maxx = maxx + area_buffer * w
    maxy = maxy + area_buffer * h

    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)

    base_map = Basemap(projection='mill',
                       lat_0=lat,
                       lon_0=lng,
                       llcrnrlon=minx,
                       llcrnrlat=miny,
                       urcrnrlon=maxx,
                       urcrnrlat=maxy,
                       resolution='l',
                       epsg=4326)
    base_map.drawcoastlines(color=(0, 0, 0, 0))

    area_shape = splitter.get_area_shape()
    if isinstance(area_shape, Polygon):
        area_shape = [area_shape]
    for polygon in area_shape:
        if isinstance(polygon.boundary, MultiLineString):
            for linestring in polygon.boundary:
                ax.add_patch(
                    plt_polygon(np.array(linestring),
                                closed=True,
                                facecolor=(0, 0, 0, 0),
                                edgecolor='red'))
        else:
            ax.add_patch(
                plt_polygon(np.array(polygon.boundary),
                            closed=True,
                            facecolor=(0, 0, 0, 0),
                            edgecolor='red'))

    bbox_list = splitter.get_bbox_list()
    info_list = splitter.get_info_list()

    cm = plt.get_cmap('jet', len(bbox_list))
    legend_shapes = []
    for i, (bbox, info) in enumerate(zip(bbox_list, info_list)):
        wgs84_bbox = transform_bbox(bbox, CRS.WGS84).get_polygon()

        tile_color = tuple(list(cm(i))[:3] + [alpha])
        ax.add_patch(
            plt_polygon(np.array(wgs84_bbox),
                        closed=True,
                        facecolor=tile_color,
                        edgecolor='green'))

        if show_legend:
            legend_shapes.append(plt.Rectangle((0, 0), 1, 1, fc=cm(i)))

    if show_legend:
        plt.legend(legend_shapes, [
            '{},{}'.format(info['index_x'], info['index_y'])
            for info in info_list
        ])
    plt.tight_layout()
    plt.show()