# Path: s2p/common.py
# def remove(target):
# def garbage_cleanup():
# def tmpfile(ext=''):
# def run(cmd, env=os.environ):
# def mkdir_p(path):
# def shellquote(s):
# def matrix_translation(x, y):
# def image_size_gdal(im):
# def gdal_read_as_array_with_nans(im):
# def image_zoom_out_morpho(im, f):
# def rasterio_write(path, array, profile={}, tags={}):
# def image_apply_homography(out, im, H, w, h):
# def image_qauto(im, out=None):
# def points_apply_homography(H, pts):
# def bounding_box2D(pts):
# def image_crop_gdal(im, x, y, w, h, out=None):
# def run_binary_on_list_of_points(points, binary, option=None, env_var=None):
# def get_rectangle_coordinates(im):
# def get_roi_coordinates(img, preview):
# def cargarse_basura(inputf, outputf):
# def print_elapsed_time(since_first_call=False):
# class RunFailure(Exception):
# 
# Path: s2p/initialization.py
# def workaround_json_int64(o):
# def dict_has_keys(d, l):
# def check_parameters(d):
# def build_cfg(user_cfg):
# def make_dirs():
# def adjust_tile_size():
# def compute_tiles_coordinates(rx, ry, rw, rh, tw, th):
# def get_tile_dir(x, y, w, h):
# def create_tile(coords, neighborhood_coords_dict):
# def tiles_full_info(tw, th, tiles_txt, create_masks=False):


import os.path
import json
import argparse
import numpy as np
import datetime
import s2p
from s2p.config import cfg
from s2p import common
from s2p import initialization
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function




def write_svg_tilemap(filename, cfg, tiles):
    '''
    draw tiles boundaries with names in an svg file
    '''
    with open(filename,'w') as f:
        f.write('<?xml version="1.0" standalone="no"?>\
        <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \
         "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">')
        f.write('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" \
               xmlns:xlink="http://www.w3.org/1999/xlink" \
               width="1000px" height="1000px" viewBox="%d %d %d %d">'%(
           cfg['roi']['x'], cfg['roi']['y'], cfg['roi']['w'], cfg['roi']['h']))
        for t in tiles:
            x, y ,w ,h = t['coordinates']
            dir = os.path.abspath(t['dir']).split(os.path.abspath(cfg['out_dir']))[-1]
            try:

# Path: s2p/common.py
# def remove(target):
# def garbage_cleanup():
# def tmpfile(ext=''):
# def run(cmd, env=os.environ):
# def mkdir_p(path):
# def shellquote(s):
# def matrix_translation(x, y):
# def image_size_gdal(im):
# def gdal_read_as_array_with_nans(im):
# def image_zoom_out_morpho(im, f):
# def rasterio_write(path, array, profile={}, tags={}):
# def image_apply_homography(out, im, H, w, h):
# def image_qauto(im, out=None):
# def points_apply_homography(H, pts):
# def bounding_box2D(pts):
# def image_crop_gdal(im, x, y, w, h, out=None):
# def run_binary_on_list_of_points(points, binary, option=None, env_var=None):
# def get_rectangle_coordinates(im):
# def get_roi_coordinates(img, preview):
# def cargarse_basura(inputf, outputf):
# def print_elapsed_time(since_first_call=False):
# class RunFailure(Exception):
# 
# Path: s2p/initialization.py
# def workaround_json_int64(o):
# def dict_has_keys(d, l):
# def check_parameters(d):
# def build_cfg(user_cfg):
# def make_dirs():
# def adjust_tile_size():
# def compute_tiles_coordinates(rx, ry, rw, rh, tw, th):
# def get_tile_dir(x, y, w, h):
# def create_tile(coords, neighborhood_coords_dict):
# def tiles_full_info(tw, th, tiles_txt, create_masks=False):


# Path: utils/svg_tilemap.py
import os.path
import json
import argparse
import numpy as np
import datetime
import s2p
from s2p.config import cfg
from s2p import common
from s2p import initialization

#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function




def write_svg_tilemap(filename, cfg, tiles):
    '''
    draw tiles boundaries with names in an svg file
    '''
    with open(filename,'w') as f:
        f.write('<?xml version="1.0" standalone="no"?>\
        <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \
         "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">')
        f.write('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" \
               xmlns:xlink="http://www.w3.org/1999/xlink" \
               width="1000px" height="1000px" viewBox="%d %d %d %d">'%(
           cfg['roi']['x'], cfg['roi']['y'], cfg['roi']['w'], cfg['roi']['h']))
        for t in tiles:
            x, y ,w ,h = t['coordinates']
            dir = os.path.abspath(t['dir']).split(os.path.abspath(cfg['out_dir']))[-1]
            try:

               common.image_qauto("%s/dsm.tif"%t['dir'], "%s/dsm.tif.png"%t['dir'])