Example #1
0
 def create_raster_worldfile(self, path, xy_range=None):
     from globalmaptiles import GlobalMercator
     x_y = xy_range or self.xy_range
     im = Image.open(path)
     gw_path = ''.join(os.path.split(path)[-1].split('.')[:-1])
     world_file_path = os.path.join(
         os.path.curdir, os.path.join(self.output_dir, "%s.jgw" % gw_path))
     with open(world_file_path, 'w') as world:
         min_y, min_x = num2deg(x_y['xMin'], x_y['yMax'] + 1, self.zoom)
         max_y, max_x = num2deg(x_y['xMax'] + 1, x_y['yMin'], self.zoom)
         gm = GlobalMercator()
         min_x, min_y = gm.LatLonToMeters(min_y, min_x)
         max_x, max_y = gm.LatLonToMeters(max_y, max_x)
         x_pixel_size = (max_x - min_x) / im.size[0]
         y_pixel_size = (max_y - min_y) / im.size[1]
         world.write(b"%f\n" % x_pixel_size
                     )  # pixel size in the x-direction in map units/pixel
         world.write(b"%f\n" % 0)  # rotation about y-axis
         world.write(b"%f\n" % 0)  # rotation about x-axis
         world.write(
             b"%f\n" % -(abs(y_pixel_size))
         )  # pixel size in the y-direction in map units. Always negative
         world.write(
             b"%f\n" %
             min_x)  # x-coordinate of the center of the upper left pixel
         world.write(
             b"%f\n" %
             max_y)  # y-coordinate of the center of the upper left pixel
Example #2
0
    def __init__(self, renderer, cache_dir):
        super(CustomMapLayer, self).__init__(renderer)
        self.cache_dir = cache_dir
        self.mercator = GlobalMercator()
        self.tileloader = None
        if self.tiles is not None:
            map_envelope = self.m.envelope()
            # map_envelope is in mercator projection, convert it to
            # long/lat projection
            envelope = renderer.merc_to_lnglat(map_envelope)
            min_lon = envelope.minx
            min_lat = envelope.miny
            max_lon = envelope.maxx
            max_lat = envelope.maxy

            width = self.m.width
            indexing = self.tiles.get('indexing')
            max_zoom = self.tiles.get('maxZoom')
            if indexing == 'google':
                self.tileloader = GoogleTileLoader(min_lat, min_lon, max_lat,
                                                   max_lon, width, max_zoom)
            elif indexing == 'tms':
                self.tileloader = TMSTileLoader(min_lat, min_lon, max_lat,
                                                max_lon, width, max_zoom)
            elif indexing == 'f':
                self.tileloader = FTileLoader(min_lat, min_lon, max_lat,
                                              max_lon, width, max_zoom)
Example #3
0
    def __init__(self,
                 bearing=0.0,
                 zoomlevel=16,
                 lat=decimal.Decimal('32.018300'),
                 lon=decimal.Decimal('34.898161'),
                 parent=None):
        #set initial values
        self.parent = parent
        self.bearingSensitivity = decimal.Decimal('0.00001')
        self.bearing = bearing
        self.zoomlevel = zoomlevel
        self.lat = lat
        self.lon = lon
        self.gx, self.gy = None, None
        self.velocity = 0.0

        self.sysPath = os.path.join(sys.path[0], "")

        self.mapPath = self.sysPath
        self.maxZoomLevel = 16

        self.destlat = decimal.Decimal('32.776250')
        self.destlon = decimal.Decimal('35.028946')

        self.distance = 0

        self.setBounds(parent.geometry().width(), parent.geometry().height())

        self.halfboundx = math.ceil(self.boundx / 2)
        self.halfboundy = math.ceil(self.boundy / 2)

        #make GlobalMercator instance
        self.mercator = GlobalMercator()
        #        create pathways
        self.refresh()
Example #4
0
def main(tiles_path, db_file, groups, zoom_levels):
    merc = GlobalMercator()

    # Set-up the output db
    
    conn = sqlite3.connect(db_file)
    c = conn.cursor()

    for zoom in [zoom_levels]: #TODO zoom levels
        results_set = c.execute("select x, y, quadkey, group_type from people_by_group order by quadkey asc, rand asc" )
        use_ellipse, radius_rel, gamma, os_scale = STYLE[zoom]
        radius = os_scale*radius_rel/4/2
        quadkey = None
        img = None

        for i,r in enumerate(results_set):
            if (i % 1000 == 0):
                print i
    
            x = float(r[0])
            y = float(r[1])
            next_quadkey = r[2][:zoom]
            group = r[3]
    
            if next_quadkey != quadkey:
                #finish last tile
                if img:
                    save_tile(img, tiles_path, zoom, gtx, gty)
                
                quadkey = next_quadkey
                tx, ty = merc.MetersToTile(x, y, zoom)
                gtx, gty = merc.GoogleTile(tx,ty,zoom)
        
                img = Image.new("RGB", (TILE_X*os_scale, TILE_Y*os_scale), "white")
                draw = ImageDraw.Draw(img)
                
            minx, miny, maxx, maxy = (c/A for c in merc.TileBounds(tx, ty, zoom))
            xscale = (TILE_X*os_scale)/(maxx - minx)
            yscale = (TILE_Y*os_scale)/(maxy - miny)


            #print 'minx', minx, 'miny', miny, 'maxx', maxx, 'maxy', maxy
            #print 'xscale',xscale,'yscale',yscale
            #print 'x',x,'y',y,'tx',tx,'ty',ty
        
            # Translate coordinates to tile-relative, google ready coordinates
            rx = (x/A - minx)*xscale
            ry = (maxy - y/A)*yscale
    
            fill=ImageColor.getrgb(groups[group]['color'])
            if use_ellipse:
                draw.ellipse((rx-radius,ry-radius,rx+radius,ry+radius), fill=fill)
            else:
                draw.point((rx, ry), fill=fill)
            #print "Draw at ", (rx-radius,ry-radius,rx+radius,ry+radius), ImageColor.getrgb(groups[group]['color'])

        save_tile(img, tiles_path, zoom, gtx, gty)
    
    save_defined_tiles(tiles_path)
Example #5
0
 def __init__(self, min_lat, min_lon, max_lat, max_lon, width, max_zoom=18):
     self.tiles = []
     self.min_lat = min_lat
     self.min_lon = min_lon
     self.max_lat = max_lat
     self.max_lon = max_lon
     self.mercator = GlobalMercator()
     self.downloader = Downloader()
     # count how many horizontal tiles we need
     self.x_tiles_needed = math.ceil(width / self.TILE_WIDTH)
     self.max_zoom = max_zoom
    def __init__(self, client):
        self.proj = GlobalMercator()
        self.nodeRecords = []
        self.wayRecords = []
        self.relationRecords = []
        self.record = {}
        self.nodeLocations = {}
        self.client = client

        self.stats = {'nodes': 0, 'ways': 0, 'relations': 0}
        self.lastStatString = ""
        self.statsCount = 0
Example #7
0
    def process_vectors_in_dir(self, rootdir):

        self.gm = GlobalMercator()

        num_images = self.count_rasters_in_dir(rootdir) * pow(
            self.tile_size / self.thumb_size, 2)
        print("num_images is {} in {}".format(num_images, rootdir))
        labels = None
        if self.train_vector_tiles_dir == rootdir:
            self.train_labels = numpy.zeros(num_images * 2,
                                            dtype=numpy.float32)
            self.train_labels = self.train_labels.reshape(num_images, 2)
            labels = self.train_labels
        else:
            self.test_labels = numpy.zeros(num_images * 2, dtype=numpy.float32)
            self.test_labels = self.test_labels.reshape(num_images, 2)
            labels = self.test_labels

        index = 0
        for folder, subs, files in os.walk(rootdir):
            for filename in files:
                if not filename.endswith('.json'):
                    continue
                has_ways = False
                with open(os.path.join(folder, filename), 'r') as src:
                    linestrings = self.linestrings_for_vector_tile(src)
                tile_matrix = self.empty_tile_matrix()
                tile = self.tile_for_folder_and_filename(
                    folder, filename, rootdir)
                for linestring in linestrings:
                    # check if tile has any linestrings to set it's one-hot
                    tile_matrix = self.add_linestring_to_matrix(
                        linestring, tile, tile_matrix)
                # self.print_matrix(tile_matrix)
                # print '\n\n\n'

                # Now set the one_hot value for this label
                for y in range(int(self.tile_size / self.thumb_size)):
                    for x in range(int(self.tile_size / self.thumb_size)):
                        for tmy in range(self.thumb_size):
                            for tmx in range(self.thumb_size):
                                if tile_matrix[tmx][tmy] == 1:
                                    has_ways = True

                        if has_ways:
                            labels[index][0] = 1
                        else:
                            labels[index][1] = 1

                        index += 1
Example #8
0
 def __init__(self, mapdir, minzoom, maxzoom):
     self.mercator = GlobalMercator(256)
     self.minzoom = minzoom
     self.maxzoom = maxzoom
     self.TopRightLat = None
     self.TopRightLon = None
     self.BottomLeftLat = None
     self.BottomLeftLon = None
     self.mminx = None
     self.mminy = None
     self.mmaxx = None
     self.mmaxy = None
     self.mapdir = mapdir
     self.jobs = Queue.Queue()
def GetGridID(Coord):
  lat=Coord[0]/1000
  lon=Coord[1]/1000

  tz=8

  mercator = GlobalMercator()
  mx, my = mercator.LatLonToMeters( Coord[0]/1000.0, Coord[1]/1000.0 )
  tx, ty = mercator.MetersToTile( mx, my, tz )

  gx, gy = mercator.GoogleTile(tx, ty, tz)
	#print "\tGoogle:", gx, gy

  #print tx, ty

  return ("%03d" % gx)+("%03d" % gy)
Example #10
0
def main():

    merc = GlobalMercator()

    file = open('pts1990.csv', 'rb')
    reader = csv.DictReader(file, delimiter=',')
    print "x,y,quad,category"

    for row in reader:
        lat = float(row['lat'])
        long = float(row['long'])
        x, y = merc.LatLonToMeters(lat, long)
        tx, ty = merc.MetersToTile(x, y, 21)

        # Create a unique quadkey for each point object

        quadkey = merc.QuadTree(tx, ty, 21)

        # Create categorical variable for the race category

        # Export data to the database file

        print "{},{},{},{}".format(x, y, quadkey, row['group'])
Example #11
0
def ImageryRequest(tileStr):
    tile = tileRequest(tileStr)
    z = tile.zoom
    x = tile.tx
    y = tile.ty
    
    downloadedTileList = os.listdir('DownloadedTiles/')
    tileFileName = str(z)+'.'+str(y)+'.'+str(x)+'.png'
    
    print(x,y,z)    
    tilesize = 256
    tx = tile.tx
    ty =tile.ty

    zoom = tile.zoom
    px = tx*tilesize 
    py = ty*tilesize 
    gm = GlobalMercator()

    mx1,my1 = gm.PixelsToMeters(px, py, zoom)

    mx2,my2 = gm.PixelsToMeters(px+tilesize, py+tilesize, zoom)
    print(mx1,-my2,mx2,-my1)

    os.system('rm Subset.TIF')
    os.system('gdalwarp -q -t_srs epsg:3857 -te '+str(mx1)+' '+str(-my2)+' '+str(mx2)+' '+str(-my1)+' -r Lanczos -ts 256 256 Warped.TIF Subset.TIF')
    

    #Open the image
    tileImage = Image.open('Subset.TIF')
   
    #Turn the image into a string
    buffer_image = StringIO()
    tileImage.save(buffer_image, 'png')
    buffer_image.seek(0)
    #Send the string
    return(send_file(buffer_image, mimetype='image/png'))
Example #12
0
parser.add_option(
    '-f',
    '--format',
    action='store',
    dest='format',
    default='',
    help='tile image format',
)
(options, args) = parser.parse_args()
#parse the bounds
boundsarr = options.bounds.split(';')
lonarr = sorted([float(boundsarr[0]), float(boundsarr[2])])
latarr = sorted([float(boundsarr[1]), float(boundsarr[3])])
z = int(options.zoom)

gm = GlobalMercator()
#Convert bounds to meters
mx0, my0 = gm.LatLonToMeters(latarr[0], lonarr[0])
mx1, my1 = gm.LatLonToMeters(latarr[1], lonarr[1])
#get TMS tile address range
tx0, ty0 = gm.MetersToTile(mx0, my0, z)
tx1, ty1 = gm.MetersToTile(mx1, my1, z)
#sort the tile addresses low to high
xarr = sorted([tx0, tx1])
yarr = sorted([ty0, ty1])
#figure out relevant extensions
extension = "." + options.format  #getExtension(options.template)
wf = getWorldFileExtension(extension)
#create the destination location using the z value
root = options.destination + '/' + str(z)
try:
Example #13
0
def pdfer(data, page_size=PAGE_SIZES['letter'], output='pdf'):

    shape_overlays = data.get('shape_overlays')
    point_overlays = data.get('point_overlays')

    grid = {'zoom': data.get('zoom')}
    center_lon, center_lat = data['center']
    center_tile_x, center_tile_y = tileXY(float(center_lat), float(center_lon),
                                          int(data['zoom']))

    dim_across, dim_up = data['dimensions']

    if dim_across > dim_up:
        page_height, page_width, tiles_up, tiles_across = page_size
    else:
        page_width, page_height, tiles_across, tiles_up = page_size

    min_tile_x = center_tile_x - (tiles_across / 2)
    min_tile_y = center_tile_y - (tiles_up / 2)
    max_tile_x = min_tile_x + tiles_across
    max_tile_y = min_tile_y + tiles_up

    # Get base layer tiles
    base_pattern = 'http://d.tile.stamen.com/toner/{z}/{x}/{y}.png'
    if data.get('base_tiles'):
        base_pattern = data['base_tiles']

    base_links = generateLinks(base_pattern, grid['zoom'], min_tile_x,
                               min_tile_y, max_tile_x, max_tile_y)

    base_names = dl_write_all(base_links, 'base')

    # Get overlay tiles
    overlay_pattern = None
    if data.get('overlay_tiles'):
        overlay_pattern = data['overlay_tiles']
        overlay_links = generateLinks(overlay_pattern, grid['zoom'],
                                      min_tile_x, min_tile_y, max_tile_x,
                                      max_tile_y)

        overlay_names = dl_write_all(overlay_links, 'overlay')

    now = datetime.now()
    date_string = datetime.strftime(now, '%Y-%m-%d_%H-%M-%S')
    outp_name = os.path.join('/tmp', '{0}.png'.format(date_string))
    base_image_names = ['-'.join(l.split('/')[-3:]) for l in base_names]
    base_image_names = sorted([i.split('-')[-3:] for i in base_image_names],
                              key=itemgetter(1))

    for parts in base_image_names:
        z, x, y = parts
        y = y.rstrip('.png').rstrip('.jpg')
        z = z.rsplit('_', 1)[1]
        key = '-'.join([z, x, y])
        grid[key] = {'bbox': tileEdges(float(x), float(y), int(z))}

    keys = sorted(grid.keys())

    mercator = GlobalMercator()
    bb_poly = None

    bmin_rx = None
    bmin_ry = None

    if shape_overlays or point_overlays:
        polys = []
        for k, v in grid.items():
            try:
                one, two, three, four = grid[k]['bbox']
                polys.append(box(two, one, four, three))
            except TypeError:
                pass
        mpoly = MultiPolygon(polys)
        bb_poly = box(*mpoly.bounds)
        min_key = keys[0]
        max_key = keys[-2]
        bminx, bminy = grid[min_key]['bbox'][0], grid[min_key]['bbox'][1]
        bmaxx, bmaxy = grid[max_key]['bbox'][2], grid[max_key]['bbox'][3]
        bmin_mx, bmin_my = mercator.LatLonToMeters(bminx, bminy)
        bmax_mx, bmax_my = mercator.LatLonToMeters(bmaxx, bmaxy)
        bmin_px, bmin_py = mercator.MetersToPixels(bmin_mx, bmin_my,
                                                   float(grid['zoom']))
        bmax_px, bmax_py = mercator.MetersToPixels(bmax_mx, bmax_my,
                                                   float(grid['zoom']))
        bmin_rx, bmin_ry = mercator.PixelsToRaster(bmin_px, bmin_py,
                                                   int(grid['zoom']))

        if shape_overlays:
            all_polys = []
            for shape_overlay in shape_overlays:
                shape_overlay = json.loads(shape_overlay)
                if shape_overlay.get('geometry'):
                    shape_overlay = shape_overlay['geometry']
                coords = shape_overlay['coordinates'][0]
                all_polys.append(Polygon(coords))
            mpoly = MultiPolygon(all_polys)

            one, two, three, four, five = list(
                box(*mpoly.bounds).exterior.coords)

            left, right = LineString([one, two]), LineString([three, four])
            top, bottom = LineString([two, three]), LineString([four, five])

            left_to_right = left.distance(right)
            top_to_bottom = top.distance(bottom)

            if left_to_right > top_to_bottom:
                page_height, page_width, _, _ = page_size
            else:
                page_width, page_height, _, _ = page_size

            center_lon, center_lat = list(mpoly.centroid.coords)[0]

        if point_overlays:
            all_points = []

            for point_overlay in point_overlays:
                point_overlay = json.loads(point_overlay)
                for p in point_overlay['points']:
                    if p[0] and p[1]:
                        all_points.append(p)

            mpoint = MultiPoint(all_points)
            center_lon, center_lat = list(mpoint.centroid.coords)[0]

            one, two, three, four, five = list(
                box(*mpoint.bounds).exterior.coords)

            left, right = LineString([one, two]), LineString([three, four])
            top, bottom = LineString([two, three]), LineString([four, five])

            left_to_right = left.distance(right)
            top_to_bottom = top.distance(bottom)

            if left_to_right > top_to_bottom:
                page_height, page_width, _, _ = page_size
            else:
                page_width, page_height, _, _ = page_size

            center_lon, center_lat = list(mpoint.centroid.coords)[0]

            print(center_lon, center_lat)

    arrays = []
    for k, g in groupby(base_image_names, key=itemgetter(1)):
        images = list(g)
        fnames = ['/tmp/%s' % ('-'.join(f)) for f in images]
        array = []
        for img in fnames:
            i = cv2.imread(img, -1)
            if isinstance(i, type(None)):
                i = np.zeros((256, 256, 4), np.uint8)
            elif i.shape[2] != 4:
                i = cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2BGRA)
            array.append(i)
        arrays.append(np.vstack(array))
    outp = np.hstack(arrays)
    cv2.imwrite(outp_name, outp)
    if overlay_pattern:
        overlay_outp_name = os.path.join('/tmp',
                                         'overlay_{0}.png'.format(date_string))
        overlay_image_names = [
            '-'.join(l.split('/')[-3:]) for l in overlay_names
        ]
        overlay_image_names = sorted(
            [i.split('-')[-3:] for i in overlay_image_names],
            key=itemgetter(1))
        arrays = []
        for k, g in groupby(overlay_image_names, key=itemgetter(1)):
            images = list(g)
            fnames = ['/tmp/%s' % ('-'.join(f)) for f in images]
            array = []
            for img in fnames:
                i = cv2.imread(img, -1)
                if isinstance(i, type(None)):
                    i = np.zeros((256, 256, 4), np.uint8)
                elif i.shape[2] != 4:
                    i = cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2BGRA)
                array.append(i)
            arrays.append(np.vstack(array))
            nuked = [os.remove(f) for f in fnames]
        outp = np.hstack(arrays)
        cv2.imwrite(overlay_outp_name, outp)
        base = cv2.imread(outp_name, -1)
        overlay = cv2.imread(overlay_outp_name, -1)
        overlay_g = cv2.cvtColor(overlay, cv2.COLOR_BGR2GRAY)
        ret, mask = cv2.threshold(overlay_g, 10, 255, cv2.THRESH_BINARY)
        inverted = cv2.bitwise_not(mask)
        overlay = cv2.bitwise_not(overlay, overlay, mask=inverted)

        base_alpha = 0.55
        overlay_alpha = 1

        for channel in range(3):
            x, y, d = overlay.shape
            base[:,:,channel] = (base[:,:,channel] * base_alpha + \
                                     overlay[:,:,channel] * overlay_alpha * \
                                     (1 - base_alpha)) / \
                                     (base_alpha + overlay_alpha * (1 - base_alpha))

        cv2.imwrite(outp_name, base)

    ###########################################################################
    # Code below here is for drawing vector layers within the PDF             #
    # Leaving it in just because it was a pain to come up with the first time #
    ###########################################################################

    if shape_overlays or point_overlays:

        im = cairo.ImageSurface.create_from_png(outp_name)
        ctx = cairo.Context(im)

        if shape_overlays:
            for shape_overlay in shape_overlays:
                shape_overlay = json.loads(shape_overlay)
                if shape_overlay.get('geometry'):
                    shape_overlay = shape_overlay['geometry']
                color = hex_to_rgb('#f06eaa')
                coords = shape_overlay['coordinates'][0]
                x, y = get_pixel_coords(coords[0], grid['zoom'], bmin_rx,
                                        bmin_ry)
                ctx.move_to(x, y)
                ctx.set_line_width(4.0)
                red, green, blue = [float(c) for c in color]
                ctx.set_source_rgba(red / 255, green / 255, blue / 255, 0.3)
                for p in coords[1:]:
                    x, y = get_pixel_coords(p, grid['zoom'], bmin_rx, bmin_ry)
                    ctx.line_to(x, y)
                ctx.close_path()
                ctx.fill()
                ctx.set_source_rgba(red / 255, green / 255, blue / 255, 0.5)
                for p in coords[1:]:
                    x, y = get_pixel_coords(p, grid['zoom'], bmin_rx, bmin_ry)
                    ctx.line_to(x, y)
                ctx.close_path()
                ctx.stroke()
        ctx.set_line_width(2.0)

        if point_overlays:
            for point_overlay in point_overlays:
                point_overlay = json.loads(point_overlay)
                color = hex_to_rgb(point_overlay['color'])
                for p in point_overlay['points']:
                    if p[0] and p[1]:
                        pt = Point((float(p[0]), float(p[1])))
                        if bb_poly.contains(pt):
                            nx, ny = get_pixel_coords(p, grid['zoom'], bmin_rx,
                                                      bmin_ry)
                            red, green, blue = [float(c) for c in color]
                            ctx.set_source_rgba(red / 255, green / 255,
                                                blue / 255, 0.6)
                            ctx.arc(
                                nx, ny, 5.0, 0,
                                50)  # args: center-x, center-y, radius, ?, ?
                            ctx.fill()
                            ctx.arc(nx, ny, 5.0, 0, 50)
                            ctx.stroke()
        im.write_to_png(outp_name)
    scale = 1

    # Crop image from center

    center_point_x, center_point_y = latlon2xy(float(center_lat),
                                               float(center_lon),
                                               float(data['zoom']))

    offset_x = (center_point_x - float(center_tile_x)) + 50
    offset_y = (center_point_y - float(center_tile_y)) - 50

    outp_image = cv2.imread(outp_name, -1)
    pixels_up, pixels_across, channels = outp_image.shape
    center_x, center_y = (pixels_across / 2) + offset_x, (pixels_up /
                                                          2) + offset_y
    start_y, end_y = center_y - (page_height / 2), center_y + (page_height / 2)
    start_x, end_x = center_x - (page_width / 2), center_x + (page_width / 2)

    cv2.imwrite(outp_name, outp_image[start_y:end_y, start_x:end_x])

    if output == 'pdf':
        outp_file_name = outp_name.rstrip('.png') + '.pdf'

        pdf = cairo.PDFSurface(outp_file_name, page_width, page_height)
        ctx = cairo.Context(pdf)
        image = cairo.ImageSurface.create_from_png(outp_name)
        ctx.set_source_surface(image)
        ctx.paint()
        pdf.finish()
    elif output == 'jpeg':
        outp_file_name = outp_name.rstrip('.png') + '.jpg'
        jpeg = cv2.cvtColor(cv2.imread(outp_name, -1), cv2.COLOR_RGBA2RGB)
        cv2.imwrite(outp_file_name, jpeg)
    return outp_file_name
Example #14
0
def main(shapes_file_list, db_file, groups):
    field_ids = {}
    # Create a GlobalMercator object for later conversions

    merc = GlobalMercator()

    # Set-up the output db

    conn = sqlite3.connect(db_file)
    c = conn.cursor()
    #c.execute("drop table if exists people_by_group")
    c.execute(
        "create table if not exists people_by_group (x real, y real, quadkey text, rand real, group_type text)"
    )
    c.execute("drop index if exists i_quadkey")

    # Open the shapefiles

    for input_filename in shapes_file_list:
        print "Processing file {0}".format(input_filename)
        ds = ogr.Open(input_filename)

        if ds is None:
            print "Open failed.\n"
            sys.exit(1)

        # Obtain the first (and only) layer in the shapefile

        lyr = ds.GetLayerByIndex(0)

        lyr.ResetReading()

        # Obtain the field definitions in the shapefile layer

        feat_defn = lyr.GetLayerDefn()
        field_defns = [
            feat_defn.GetFieldDefn(i) for i in range(feat_defn.GetFieldCount())
        ]

        # Set up a coordinate transformation to latlon
        wgs84 = osr.SpatialReference()
        wgs84.SetWellKnownGeogCS("WGS84")
        sr = lyr.GetSpatialRef()
        xformer = osr.CoordinateTransformation(sr, wgs84)

        # Obtain the index of the group fields
        for i, defn in enumerate(field_defns):
            if defn.GetName() in groups:
                field_ids[defn.GetName()] = i

        # Obtain the number of features (Census Blocks) in the layer
        n_features = len(lyr)

        # Iterate through every feature (Census Block Ploygon) in the layer,
        # obtain the population counts, and create a point for each person within
        # that feature.
        start_time = time.time()
        for j, feat in enumerate(lyr):

            # Print a progress read-out for every 1000 features and export to hard disk
            if j % 1000 == 0:
                conn.commit()
                perc_complete = (j + 1) / float(n_features)
                time_left = (1 - perc_complete) * (
                    (time.time() - start_time) / perc_complete)
                print "%s/%s (%0.2f%%) est. time remaining %0.2f mins" % (
                    j + 1, n_features, 100 * perc_complete, time_left / 60)

            # Obtain total population, racial counts, and state fips code of the individual census block

            counts = {}
            for f in field_ids:
                val = feat.GetField(field_ids[f])
                if val:
                    counts[f] = int(val)
                else:
                    counts[f] = 0

            # Obtain the OGR polygon object from the feature
            geom = feat.GetGeometryRef()
            if geom is None:
                continue

            # Convert the OGR Polygon into a Shapely Polygon
            poly = loads(geom.ExportToWkb())

            if poly is None:
                continue

            # Obtain the "boundary box" of extreme points of the polygon
            bbox = poly.bounds

            if not bbox:
                continue

            leftmost, bottommost, rightmost, topmost = bbox

            # Generate a point object within the census block for every person by race

            for f in field_ids:
                for i in range(counts[f]):
                    # Choose a random longitude and latitude within the boundary box
                    # and within the orginial ploygon of the census block
                    while True:
                        samplepoint = Point(uniform(leftmost, rightmost),
                                            uniform(bottommost, topmost))
                        if samplepoint is None:
                            break
                        if poly.contains(samplepoint):
                            break

                    # Convert the longitude and latitude coordinates to meters and
                    # a tile reference

                    try:
                        # In general we don't know the coordinate system of input data
                        # so transform it to latlon
                        lon, lat, z = xformer.TransformPoint(
                            samplepoint.x, samplepoint.y)
                        x, y = merc.LatLonToMeters(lat, lon)
                    except:
                        print "Failed to convert ", lat, lon
                        sys.exit(-1)
                    tx, ty = merc.MetersToTile(x, y, 21)

                    # Create a unique quadkey for each point object
                    quadkey = merc.QuadTree(tx, ty, 21)

                    # Create categorical variable for the race category
                    group_type = f

                    # Export data to the database file
                    try:
                        c.execute(
                            "insert into people_by_group values (?,?,?,random(),?)",
                            (x, y, quadkey, group_type))
                    except:
                        print "Failed to insert ", x, y, tx, ty, group_type
                        sys.exit(-1)

        c.execute(
            "create index if not exists i_quadkey on people_by_group(x, y, quadkey, rand, group_type)"
        )
        conn.commit()
    if google_image_folder is None or output_jpeg_file is None or map_type is None or format is None or tz is None or lon is None or lat is None or radius is None or bottom_crop is None or KEY is None or image_size is None or scale is None or resume is None or debug is None or tif_output is None:
        print("invalid parameter exists!")
        exit()
    actual_tile_size = image_size * scale
    debug_print("actual tile size %d" % actual_tile_size)

    if not resume:
        if os.path.exists(google_image_folder):
            shutil.rmtree(google_image_folder)
        if os.path.exists(output_jpeg_file):
            os.unlink(output_jpeg_file)

    if not os.path.exists(google_image_folder):
        os.makedirs(google_image_folder)

    mercator = GlobalMercator()
    cx, cy = mercator.LatLonToMeters(lat, lon)
    minx = cx - radius
    maxx = cx + radius
    miny = cy - radius
    maxy = cy + radius
    debug_print('minx = %f, miny = %f, maxx = %f, maxy = %f\n' %
                (minx, miny, maxx, maxy))

    tminx, tminy = mercator.MetersToTile(minx, miny, tz)
    tmaxx, tmaxy = mercator.MetersToTile(maxx, maxy, tz)

    total_tiles = (tmaxx - tminx + 1) * (tmaxy - tminy + 1)
    debug_print('count = %d' % total_tiles)

    # progress bar
Example #16
0
 def __init__(self):
     self.client = Connection()
     self.proj = GlobalMercator()
Example #17
0
def main(input_filename, wac_filename, output_filename):
    
    wac = pd.io.parsers.read_csv(wac_filename)
    wac.set_index(wac['w_geocode'],inplace = True)
    
    #Create columns for four megasectors
    
    wac['makers'] = wac['CNS01']+wac['CNS02']+wac['CNS03']+wac['CNS04']+wac['CNS05']+wac['CNS06']+wac['CNS08']
    wac['services'] = wac['CNS07']+wac['CNS14'] + wac['CNS17'] + wac['CNS18']
    wac['professions'] = wac['CNS09'] + wac['CNS10'] + wac['CNS11'] + wac['CNS12'] + wac['CNS13']
    wac['support'] = wac['CNS15'] + wac['CNS16'] + wac['CNS19'] + wac['CNS20']

    assert sum(wac['C000'] -(wac['makers']+wac['services']+wac['professions']+wac['support'])) == 0 or rw[1]['abbrev'] == 'ny'

    #In NY there's one block in Brooklyn with 177000 jobs. It appears to be rounding entries > 100k, which is making the assertion fail.
    #This is the Brooklyn Post Office + Brooklyn Law School + Borough Hall. So maybe weirdness around post office? 

    #Set up outfile as csv
    outf = open(output_filename,'w')
    outf.write('x,y,sect,inctype,quadkey\n')
    
    # Create a GlobalMercator object for later conversions
    
    merc = GlobalMercator()

    # Open the shapefile
    
    ds = ogr.Open(input_filename)
    
    if ds is None:
        print "Open failed.\n"
        sys.exit( 1 )

    # Obtain the first (and only) layer in the shapefile
    
    lyr = ds.GetLayerByIndex(0)

    lyr.ResetReading()

    # Obtain the field definitions in the shapefile layer

    feat_defn = lyr.GetLayerDefn()
    field_defns = [feat_defn.GetFieldDefn(i) for i in range(feat_defn.GetFieldCount())]

    # Obtain the index of the field for the count for whites, blacks, Asians, 
    # Others, and Hispanics.
    
    for i, defn in enumerate(field_defns):
        print defn.GetName()
        #GEOID is what we want to merge on
        if defn.GetName() == "GEOID10":
            fips = i

    # Set-up the output file
    
    #conn = sqlite3.connect( output_filename )
    #c = conn.cursor()
    #c.execute( "create table if not exists people_by_race (statefips text, x text, y text, quadkey text, race_type text)" )

    # Obtain the number of features (Census Blocks) in the layer
    
    n_features = len(lyr)

    # Iterate through every feature (Census Block Ploygon) in the layer,
    # obtain the population counts, and create a point for each person within
    # that feature.

    for j, feat in enumerate( lyr ):
        # Print a progress read-out for every 1000 features and export to hard disk
        
        if j % 1000 == 0:
            #conn.commit()
            print "%s/%s (%0.2f%%)"%(j+1,n_features,100*((j+1)/float(n_features)))
            
        # Obtain total population, racial counts, and state fips code of the individual census block
        blkfips = int(feat.GetField(fips))
        
        try:
            jobs = {'m':wac.loc[blkfips,'makers'],'s':wac.loc[blkfips,'services'],'p':wac.loc[blkfips,'professions'],'t':wac.loc[blkfips,'support']}
        except KeyError:
            #print "no"
#            missing.append(blkfips) #Missing just means no jobs there. Lots of blocks have this.
            continue            
        income = {'l':wac.loc[blkfips,'CE01'],'m':wac.loc[blkfips,'CE02'],'h':wac.loc[blkfips,'CE03']}
        # Obtain the OGR polygon object from the feature

        geom = feat.GetGeometryRef()
        
        if geom is None:
            continue
        
        # Convert the OGR Polygon into a Shapely Polygon
        
        poly = loads(geom.ExportToWkb())
        
        if poly is None:
            continue        
            
        # Obtain the "boundary box" of extreme points of the polygon

        bbox = poly.bounds
        
        if not bbox:
            continue
     
        leftmost,bottommost,rightmost,topmost = bbox
    
        # Generate a point object within the census block for every person by race
        inccnt = 0
        incord = ['l','m','h']
        shuffle(incord)
        
        for sect in ['m','s','p','t']:
            for i in range(int(jobs[sect])):

                # Choose a random longitude and latitude within the boundary box
                # and within the orginial ploygon of the census block
                    
                while True:
                        
                    samplepoint = Point(uniform(leftmost, rightmost),uniform(bottommost, topmost))
                        
                    if samplepoint is None:
                        break
                    
                    if poly.contains(samplepoint):
                        break
        
                x, y = merc.LatLonToMeters(samplepoint.y,samplepoint.x)
                tx,ty = merc.MetersToTile(x, y, 21)
                    
                    
                #Determine the right income
                inccnt += 1
                inctype = ''
                assert inccnt <= income[incord[0]] + income[incord[1]] + income[incord[2]] or rw[1]['abbrev'] == 'ny'
                if inccnt <= income[incord[0]]:
                    inctype = incord[0]
                elif inccnt <= income[incord[0]] + income[incord[1]]:
                    inctype = incord[1]
                elif inccnt <= income[incord[0]] + income[incord[1]] + income[incord[2]]:
                    inctype = incord[2]
                        
                # Create a unique quadkey for each point object
                    
                quadkey = merc.QuadTree(tx, ty, 21)       
                 
                outf.write("%s,%s,%s,%s,%s\n" %(x,y,sect,inctype,quadkey))
                # Convert the longitude and latitude coordinates to meters and
                # a tile reference

    outf.close() 
Example #18
0
 def __init__(self):
     self.client = Connection(host="mongomaster")
     self.proj = GlobalMercator()
Example #19
0
 def getTile(self, zoomlevel):
     mercator = GlobalMercator()
     mx, my = mercator.LatLonToMeters(self.lat, self.lon)
     tminx, tminy = mercator.MetersToTile(mx, my, zoomlevel)
     gx, gy = mercator.GoogleTile(tminx, tminy, zoomlevel)  #+1?
     return gx, gy, zoomlevel
Example #20
0
def main(input_filename, output_filename):

    # Create a GlobalMercator object for later conversions

    merc = GlobalMercator()

    # Open the shapefile

    ds = ogr.Open(input_filename)

    if ds is None:
        print "Open failed.\n"
        sys.exit(1)

    # Obtain the first (and only) layer in the shapefile

    lyr = ds.GetLayerByIndex(0)

    lyr.ResetReading()

    # Obtain the field definitions in the shapefile layer

    feat_defn = lyr.GetLayerDefn()
    field_defns = [
        feat_defn.GetFieldDefn(i) for i in range(feat_defn.GetFieldCount())
    ]

    # Obtain the index of the field for the count for whites, blacks, Asians,
    # Others, and Hispanics.

    for i, defn in enumerate(field_defns):

        if defn.GetName() == "POP10":
            pop_field = i

        if defn.GetName() == "nh_white_n":
            white_field = i

        if defn.GetName() == "nh_black_n":
            black_field = i

        if defn.GetName() == "nh_asian_n":
            asian_field = i

        if defn.GetName() == "hispanic_n":
            hispanic_field = i

        if defn.GetName() == "NH_Other_n":
            other_field = i

        if defn.GetName() == "STATEFP10":
            statefips_field = i

    # Set-up the output file

    conn = sqlite3.connect(output_filename)
    c = conn.cursor()
    c.execute(
        "create table if not exists people_by_race (statefips text, x text, y text, quadkey text, race_type text)"
    )

    # Obtain the number of features (Census Blocks) in the layer

    n_features = len(lyr)

    # Iterate through every feature (Census Block Ploygon) in the layer,
    # obtain the population counts, and create a point for each person within
    # that feature.

    for j, feat in enumerate(lyr):

        # Print a progress read-out for every 1000 features and export to hard disk

        if j % 1000 == 0:
            conn.commit()
            print "%s/%s (%0.2f%%)" % (j + 1, n_features, 100 *
                                       ((j + 1) / float(n_features)))

        # Obtain total population, racial counts, and state fips code of the individual census block

        pop = int(feat.GetField(pop_field))
        white = int(feat.GetField(white_field))
        black = int(feat.GetField(black_field))
        asian = int(feat.GetField(asian_field))
        hispanic = int(feat.GetField(hispanic_field))
        other = int(feat.GetField(other_field))
        statefips = feat.GetField(statefips_field)

        # Obtain the OGR polygon object from the feature

        geom = feat.GetGeometryRef()

        if geom is None:
            continue

        # Convert the OGR Polygon into a Shapely Polygon

        poly = loads(geom.ExportToWkb())

        if poly is None:
            continue

        # Obtain the "boundary box" of extreme points of the polygon

        bbox = poly.bounds

        if not bbox:
            continue

        leftmost, bottommost, rightmost, topmost = bbox

        # Generate a point object within the census block for every person by race

        for i in range(white):

            # Choose a random longitude and latitude within the boundary box
            # and within the orginial ploygon of the census block

            while True:

                samplepoint = Point(uniform(leftmost, rightmost),
                                    uniform(bottommost, topmost))

                if samplepoint is None:
                    break

                if poly.contains(samplepoint):
                    break

            # Convert the longitude and latitude coordinates to meters and
            # a tile reference

            x, y = merc.LatLonToMeters(samplepoint.y, samplepoint.x)
            tx, ty = merc.MetersToTile(x, y, 21)

            # Create a unique quadkey for each point object

            quadkey = merc.QuadTree(tx, ty, 21)

            # Create categorical variable for the race category

            race_type = 'w'

            # Export data to the database file

            c.execute("insert into people_by_race values (?,?,?,?,?)",
                      (statefips, x, y, quadkey, race_type))

        for i in range(black):

            # Choose a random longitude and latitude within the boundary box
            # points and within the orginial ploygon of the census block

            while True:

                samplepoint = Point(uniform(leftmost, rightmost),
                                    uniform(bottommost, topmost))

                if samplepoint is None:
                    break

                if poly.contains(samplepoint):
                    break

            # Convert the longitude and latitude coordinates to meters and
            # a tile reference

            x, y = merc.LatLonToMeters(samplepoint.y, samplepoint.x)
            tx, ty = merc.MetersToTile(x, y, 21)

            # Create a unique quadkey for each point object

            quadkey = merc.QuadTree(tx, ty, 21)

            # Create categorical variable for the race category

            race_type = 'b'

            # Export data to the database file

            c.execute("insert into people_by_race values (?,?,?,?,?)",
                      (statefips, x, y, quadkey, race_type))

        for i in range(asian):

            # Choose a random longitude and latitude within the boundary box
            # points and within the orginial ploygon of the census block

            while True:

                samplepoint = Point(uniform(leftmost, rightmost),
                                    uniform(bottommost, topmost))

                if samplepoint is None:
                    break

                if poly.contains(samplepoint):
                    break

            # Convert the longitude and latitude coordinates to meters and
            # a tile reference

            x, y = merc.LatLonToMeters(samplepoint.y, samplepoint.x)
            tx, ty = merc.MetersToTile(x, y, 21)

            # Create a unique quadkey for each point object

            quadkey = merc.QuadTree(tx, ty, 21)

            # Create categorical variable for the race category

            race_type = 'a'

            # Export data to the database file

            c.execute("insert into people_by_race values (?,?,?,?,?)",
                      (statefips, x, y, quadkey, race_type))

        for i in range(hispanic):

            # Choose a random longitude and latitude within the boundary box
            # points and within the orginial ploygon of the census block

            while True:

                samplepoint = Point(uniform(leftmost, rightmost),
                                    uniform(bottommost, topmost))

                if samplepoint is None:
                    break

                if poly.contains(samplepoint):
                    break

            # Convert the longitude and latitude coordinates to meters and
            # a tile reference

            x, y = merc.LatLonToMeters(samplepoint.y, samplepoint.x)
            tx, ty = merc.MetersToTile(x, y, 21)

            # Create a unique quadkey for each point object

            quadkey = merc.QuadTree(tx, ty, 21)

            # Create categorical variable for the race category

            race_type = 'h'

            # Export data to the database file

            c.execute("insert into people_by_race values (?,?,?,?,?)",
                      (statefips, x, y, quadkey, race_type))

        for i in range(other):

            # Choose a random longitude and latitude within the boundary box
            # points and within the orginial ploygon of the census block

            while True:

                samplepoint = Point(uniform(leftmost, rightmost),
                                    uniform(bottommost, topmost))

                if samplepoint is None:
                    break

                if poly.contains(samplepoint):
                    break

            # Convert the longitude and latitude coordinates to meters and
            # a tile reference

            x, y = merc.LatLonToMeters(samplepoint.y, samplepoint.x)
            tx, ty = merc.MetersToTile(x, y, 21)

            # Create a unique quadkey for each point object

            quadkey = merc.QuadTree(tx, ty, 21)

            # Create categorical variable for the race category

            race_type = 'o'

            # Export data to the database file

            c.execute("insert into people_by_race values (?,?,?,?,?)",
                      (statefips, x, y, quadkey, race_type))

    conn.commit()
Example #21
0
def main(input_filename, output_filename):
        print "Processing: %s - Ctrl-Z to cancel"%input_filename
        merc = GlobalMercator()

        # open the shapefile
        ds = ogr.Open( input_filename )
        if ds is None:
                print "Open failed.\n"
                sys.exit( 1 )

        lyr = ds.GetLayerByIndex( 0 )

        lyr.ResetReading()

        feat_defn = lyr.GetLayerDefn()
        field_defns = [feat_defn.GetFieldDefn(i) for i in range(feat_defn.GetFieldCount())]

        # look up the index of the field we're interested in
        for i, defn in enumerate( field_defns ):
                if defn.GetName()=="POP10":
                        pop_field = i

        # set up the output file
        # if it already exists, ask for confirmation to delete and remake it
        if os.path.isfile(output_filename):
                if not confirm("  Database %s exists, overwrite?"%output_filename, False):
                        return False
                else:
                        os.system("rm %s"%output_filename)
        
        # if file removal failed, the file may be locked:
        # ask for confirmation to unlock it
        if os.path.isfile(output_filename):
                if not confirm("  Attempt to unlock database %s?"%output_filename, False):
                        return False
                else:
                        unlock(output_filename)
                # if it's still there, there's a problem, bail
                if os.path.isfile(output_filename):
                        print "Trouble - exiting."
                        sys.exit()
                else:
                        print "Success - continuing:"

        conn = sqlite3.connect( output_filename )
        c = conn.cursor()
        c.execute( "create table if not exists people (x real, y real, quadkey text)" )
        
        n_features = len(lyr)

        for j, feat in enumerate( lyr ):
                if j%1000==0:
                        conn.commit()
                        if j%10000==0:
                                print " %s/%s (%0.2f%%)"%(j+1,n_features,100*((j+1)/float(n_features)))
                        else:
                                sys.stdout.write(".")
                                sys.stdout.flush()

                pop = feat.GetField(pop_field)

                geom = feat.GetGeometryRef()
                if geom is None:
                        continue

                bbox = get_bbox( geom )
                if not bbox:
                        continue
                ll,bb,rr,tt = bbox

                # generate a sample within the geometry for every person
                for i in range(pop):
                        while True:
                                samplepoint = make_ogr_point( uniform(ll,rr), uniform(bb,tt) )
                                if geom.Intersects( samplepoint ):
                                        break

                        x, y = merc.LatLonToMeters( samplepoint.GetY(), samplepoint.GetX() )
                        tx,ty = merc.MetersToTile( x, y, 21)
                        quadkey = merc.QuadTree( tx, ty, 21 )

                        c.execute( "insert into people values (?,?,?)", (x, y, quadkey) )
        
        conn.commit()
        print "Finished processing %s"%output_filename
Example #22
0
def main():
    # Code to open an image, and apply a transform
    # open the image
    # image = Image.open("Ally.jpg")
    # w = image.width
    # h = image.height
    # print((w, h))
    # Create a transformation to apply to the image
    # shift = Shift(-w/2, -h/2)
    # rotate = Rotation(math.pi/2)
    # scale = Scale(2)
    # shift2 = Shift(h/2, w/2)
    # combined = PositionTransform()
    # combined = combined.combine(shift)
    # combined = combined.combine(rotate)
    # combined = combined.combine(scale)
    # combined = combined.combine(shift2)
    # inverse the transformation (to apply it)
    # t = combined.inverse()

    # Image.transform(size, method, data=None, resample=0, fill=1, fillcolor=None)
    # img2 = image.transform((h, w), Image.AFFINE, _get_image_transform(t))
    # img2.save("Test.jpg")

    # Code to create a mosaic 4x4 tile world map at level 2
    # tm = TileMosaic(OSMTileRequester(), 2, 0, 3, 0, 3)
    # tm.save("world2.png")

    # Sample coordinates to avoid caring about the transformation just now
    bng_coords = [(300000, 600000), (300000, 601000), (301000, 601000),
                  (301000, 600000)]
    gwm_coords = [(-398075.709110655, 7417169.44503078),
                  (-398115.346383602, 7418925.37709793),
                  (-396363.034574031, 7418964.91393662),
                  (-396323.792660911, 7417208.95976453)]

    bng_x = [bng[0] for bng in bng_coords]
    bng_y = [bng[1] for bng in bng_coords]
    bng_box = (min(bng_x), min(bng_y), max(bng_x), max(bng_y))

    gwm_x = [gwm[0] for gwm in gwm_coords]
    gwm_y = [gwm[1] for gwm in gwm_coords]
    gwm_box = (min(gwm_x), min(gwm_y), max(gwm_x), max(gwm_y))

    # If the coords above relate to a 400x400 map, calculate the resolution
    bng_map_size = 600
    bng_res = (bng_box[2] - bng_box[0]) / bng_map_size
    print(bng_res)

    # Use the GlobalMercator class to calculate the optimal zoom level to use
    gwm = GlobalMercator()
    gwm_zoom = gwm.ZoomForPixelSize(bng_res)
    print(gwm_zoom)

    # Calculate the min/max tile x and y for the given area at the calculates zoom level
    tiles_x = []
    tiles_y = []
    for coord in gwm_coords:
        tx, ty = gwm.MetersToTile(coord[0], coord[1], gwm_zoom)
        tiles_x.append(tx)
        tiles_y.append(ty)
        print(f"{gwm_zoom} {tx} {ty}")
        # print(OSMTileRequester().request_tile(gwm_zoom, tx, ty))

    # Create a mosaic image from these tiles
    start_x = min(tiles_x)
    end_x = max(tiles_x)
    start_y = min(tiles_y)
    end_y = max(tiles_y)
    gwm_mosaic = TileMosaic(OSMTileRequester(), gwm_zoom, start_x, end_x,
                            start_y, end_y)
    gwm_mosaic.save("mosaic.png")

    # Get the bbox for these tiles
    gwm_mosaic_box_tl = gwm.TileBounds(start_x, start_y, gwm_zoom)
    gwm_mosaic_box_tr = gwm.TileBounds(end_x, start_y, gwm_zoom)
    gwm_mosaic_box_bl = gwm.TileBounds(start_x, end_y, gwm_zoom)
    gwm_mosaic_box_br = gwm.TileBounds(end_x, end_y, gwm_zoom)
    gwm_mosaic_box = (min(gwm_mosaic_box_tl[0], gwm_mosaic_box_bl[0]),
                      min(gwm_mosaic_box_bl[3], gwm_mosaic_box_br[3]),
                      max(gwm_mosaic_box_tr[2], gwm_mosaic_box_br[2]),
                      max(gwm_mosaic_box_tl[1], gwm_mosaic_box_tr[1]))

    print(gwm_mosaic_box)

    test = [(0, 0), (400, 0), (400, 400), (0, 400)]

    # Create a transformation to convert pixels of the target BNG image to the GWM mosaic image
    bng_img_to_gwm_image = PositionTransform()
    # Translate/scale image px to BNG
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(HorizontalFlip())
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(Scale(bng_res))
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(
        Shift(bng_box[0], bng_box[3]))
    test_coords(test, bng_img_to_gwm_image)

    # Transform BNG to GWM coords
    bng_gwm_transform = SamplePointTransform(bng_coords[0], bng_coords[1],
                                             bng_coords[2], gwm_coords[0],
                                             gwm_coords[1], gwm_coords[2])
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(bng_gwm_transform)
    test_coords(test, bng_img_to_gwm_image)

    # Translate/scale GWM coords to GWM mosaic image coords
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(
        Shift(-gwm_mosaic_box[0], -gwm_mosaic_box[3]))
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(
        Scale(1 / gwm.Resolution(gwm_zoom)))
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(HorizontalFlip())

    test_coords(test, bng_img_to_gwm_image)

    bng_result = gwm_mosaic.image.transform(
        (bng_map_size, bng_map_size), Image.AFFINE,
        _get_image_transform(bng_img_to_gwm_image))

    bng_result.save("BNG.jpg")