Example #1
0
def make_order_3(request, image_collection_id, scene_id):
  import voxel_globe.tools.enu as enu
  from voxel_globe.tools.camera import get_llh
  import numpy as np
  
  image_collection = models.ImageCollection.objects.get(id=image_collection_id)
  image_list = image_collection.images.all()
  scene = models.Scene.objects.get(id=scene_id);

  geolocated = scene.geolocated

  # #if min==max, they are probably all zeros and the bounding box isn't set yet
  # if scene.bbox_min == scene.bbox_max:
  #   #TODO: Replace this with bundle2scene in visualsfm task
  #   llhs = []
  
  #   for image in image_list:
  #     llhs.append(get_llh(image.history()))

  #   llhs = np.array(llhs)
  #   bbox_min = llhs.min(axis=0)
  #   bbox_max = llhs.max(axis=0)

  #   #The above code calculated the bounding box of the cameras. Set the 
  #   #bounding box of the scene to be from the bottom of the lowest camera to
  #   #down to sea level. Hard to do anything else intelligently
  #   bbox_max[2] = bbox_min[2]
  #   bbox_min[2] = 0
  #   voxel_size = 1
  # else:
  bbox_min = scene.bbox_min
  bbox_max = scene.bbox_max
  voxel_size = sum(scene.default_voxel_size.coords)/3.0

  #if geolocated, convert lvcs to lla
  if geolocated:
    from vpgl_adaptor import create_lvcs, convert_local_to_global_coordinates
    origin = scene.origin.coords
    lvcs = create_lvcs(origin[1], origin[0], origin[2], "wgs84");
    (bbox_min[1], bbox_min[0], bbox_min[2]) = convert_local_to_global_coordinates(lvcs,bbox_min[1], bbox_min[0], bbox_min[2])
    (bbox_max[1], bbox_max[0], bbox_max[2]) = convert_local_to_global_coordinates(lvcs,bbox_max[1], bbox_max[0], bbox_max[2])

  bbox = {'x_min':bbox_min[0],
          'x_max':bbox_max[0],
          'y_min':bbox_min[1],
          'y_max':bbox_max[1],
          'z_min':bbox_min[2],
          'z_max':bbox_max[2]}

  
  return render(request, 'order/build_voxel_world/html/make_order_3.html',
                {'scene_id':scene_id, 'bbox':bbox, 'geolocated':geolocated,
                 'voxel_size': voxel_size,
                 'image_collection_id':image_collection_id})
Example #2
0
def make_order_3(request, image_collection_id, scene_id):
  import voxel_globe.tools.enu as enu
  from voxel_globe.tools.camera import get_llh
  import numpy as np
  
  image_collection = models.ImageCollection.objects.get(id=image_collection_id)
  image_list = image_collection.images.all()
  scene = models.Scene.objects.get(id=scene_id);

  geolocated = scene.geolocated

  # #if min==max, they are probably all zeros and the bounding box isn't set yet
  # if scene.bbox_min == scene.bbox_max:
  #   #TODO: Replace this with bundle2scene in visualsfm task
  #   llhs = []
  
  #   for image in image_list:
  #     llhs.append(get_llh(image.history()))

  #   llhs = np.array(llhs)
  #   bbox_min = llhs.min(axis=0)
  #   bbox_max = llhs.max(axis=0)

  #   #The above code calculated the bounding box of the cameras. Set the 
  #   #bounding box of the scene to be from the bottom of the lowest camera to
  #   #down to sea level. Hard to do anything else intelligently
  #   bbox_max[2] = bbox_min[2]
  #   bbox_min[2] = 0
  #   voxel_size = 1
  # else:
  bbox_min = scene.bbox_min
  bbox_max = scene.bbox_max
  voxel_size = sum(scene.default_voxel_size.coords)/3.0

  #if geolocated, convert lvcs to lla
  if geolocated:
    from vpgl_adaptor import create_lvcs, convert_local_to_global_coordinates
    origin = scene.origin.coords
    lvcs = create_lvcs(origin[1], origin[0], origin[2], "wgs84");
    (bbox_min[1], bbox_min[0], bbox_min[2]) = convert_local_to_global_coordinates(lvcs,bbox_min[1], bbox_min[0], bbox_min[2])
    (bbox_max[1], bbox_max[0], bbox_max[2]) = convert_local_to_global_coordinates(lvcs,bbox_max[1], bbox_max[0], bbox_max[2])

  bbox = {'x_min':bbox_min[0],
          'x_max':bbox_max[0],
          'y_min':bbox_min[1],
          'y_max':bbox_max[1],
          'z_min':bbox_min[2],
          'z_max':bbox_max[2]}

  
  return render(request, 'order/build_voxel_world/html/make_order_3.html',
                {'scene_id':scene_id, 'bbox':bbox, 'geolocated':geolocated,
                 'voxel_size': voxel_size,
                 'image_collection_id':image_collection_id})
Example #3
0
def create_height_map(self, voxel_world_id, render_height, history=None):
    import shutil
    import urllib

    import numpy as np

    from boxm2_scene_adaptor import boxm2_scene_adaptor, scene_bbox
    from boxm2_adaptor import ortho_geo_cam_from_scene, scene_lvcs
    from vpgl_adaptor import convert_local_to_global_coordinates, geo2generic, save_geocam_to_tfw
    from vil_adaptor import save_image, scale_and_offset_values, stretch_image, image_range

    import vsi.io.image

    import voxel_globe.tools
    import voxel_globe.tools.hash
    import voxel_globe.tools.camera
    import voxel_globe.meta.models as models
    import voxel_globe.ingest.payload.tools

    with voxel_globe.tools.task_dir('height_map', cd=True) as processing_dir:
        voxel_world = models.VoxelWorld.objects.get(
            id=voxel_world_id).history(history)
        scene = boxm2_scene_adaptor(
            os.path.join(voxel_world.directory, 'scene.xml'),
            env['VIP_OPENCL_DEVICE'])
        ortho_camera, cols, rows = ortho_geo_cam_from_scene(scene.scene)
        tfw_camera = os.path.join(processing_dir, 'cam.tfw')
        save_geocam_to_tfw(ortho_camera, tfw_camera)
        with open(tfw_camera, 'r') as fid:
            geo_transform = [float(x) for x in fid.readlines()]

        (x0, y0, z0), (x1, y1, z1) = scene_bbox(scene.scene)
        lvcs = scene_lvcs(scene.scene)
        #lvcs = vpgl_adaptor.create_lvcs(lat=origin[1], lon=origin[0], el=origin[2],
        #                                csname="wgs84")
        _, _, min_height = convert_local_to_global_coordinates(
            lvcs, x0, y0, z0)
        if render_height is None:
            render_height = z1 + (z1 - z0) / 1000
            #z1+(z1-z0)/1000 is basically to say "just a little above the top" *2 is
            #1) overkill and 2) doesn't work with sign, +1 could go crazy in an
            #arbitrarily scaled system, so this just calculates ".1% more" which is
            #more than good enough
        else:
            render_height = render_height - voxel_world.origin[2]
        logger.critical("Render Height is %f (%s)", render_height,
                        type(render_height))
        generic_camera = geo2generic(ortho_camera, cols, rows, render_height,
                                     0)

        z_exp_img, z_var_img = scene.render_z_image(generic_camera, cols, rows)

        #This is TECHNICALLY wrong, it assumes the earth is flat.
        scale_and_offset_values(z_exp_img, 1, min_height)

        height_filename = os.path.join(processing_dir, 'height.tif')

        save_image(z_exp_img, height_filename)

        checksum = voxel_globe.tools.hash.sha256_file(height_filename)

        with voxel_globe.tools.image_sha_dir(checksum) as image_dir:
            original_filename = os.path.join(image_dir, 'original.tif')

            #If the exact file exist already, don't ingest it again. Unlikely
            if not os.path.exists(original_filename):
                img = vsi.io.image.imread(height_filename)
                vsi.io.image.imwrite_geotiff(
                    img.raster(), original_filename,
                    [geo_transform[x] for x in [4, 0, 1, 5, 2, 3]])

                zoomify_filename = os.path.join(image_dir, 'zoomify.tif')
                img_min, img_max = image_range(z_exp_img)
                zoomify_image = stretch_image(z_exp_img, img_min, img_max,
                                              'byte')
                save_image(zoomify_image, zoomify_filename)

                zoomify_name = os.path.join(image_dir, 'zoomify')
                voxel_globe.ingest.payload.tools.zoomify_image(
                    zoomify_filename, zoomify_name)

                relative_file_path = urllib.pathname2url(
                    os.path.relpath(original_filename,
                                    env['VIP_IMAGE_SERVER_ROOT']))
                relative_zoom_path = urllib.pathname2url(
                    os.path.relpath(zoomify_name,
                                    env['VIP_IMAGE_SERVER_ROOT']))

                img = voxel_globe.meta.models.Image.create(
                    name="Height Map %s (%s)" %
                    (voxel_world.name, voxel_world.id),
                    imageWidth=cols,
                    imageHeight=rows,
                    numberColorBands=1,
                    pixelFormat='f',
                    fileFormat='zoom',
                    imageUrl='%s://%s:%s/%s/%s/' %
                    (env['VIP_IMAGE_SERVER_PROTOCOL'],
                     env['VIP_IMAGE_SERVER_HOST'],
                     env['VIP_IMAGE_SERVER_PORT'],
                     env['VIP_IMAGE_SERVER_URL_PATH'], relative_zoom_path),
                    originalImageUrl='%s://%s:%s/%s/%s' %
                    (env['VIP_IMAGE_SERVER_PROTOCOL'],
                     env['VIP_IMAGE_SERVER_HOST'],
                     env['VIP_IMAGE_SERVER_PORT'],
                     env['VIP_IMAGE_SERVER_URL_PATH'], relative_file_path),
                    service_id=self.request.id,
                    original_filename='height_map.tif')
            img.save()

            image_collection = models.ImageCollection.create(
                name="%s Height Map:" % (voxel_world.name, ),
                service_id=self.request.id)
            image_collection.save()
            image_collection.images.add(img)

            gsd = scene.description['voxelLength']
            camera_center = ((x0 + x1) / 2, (y0 + y1) / 2, z1 + 10000)
            d = z1 - z0 + 10000
            k = np.eye(3)
            k[0, 2] = cols / 2
            k[1, 2] = rows / 2
            k[0, 0] = k[1, 1] = d / gsd
            r = np.eye(3)
            r[0, 0] = -1
            t = -r.T.dot(camera_center)

            voxel_globe.tools.camera.save_krt(self.request.id, img, k, r, t,
                                              voxel_world.origin)
Example #4
0
def create_height_map(self, voxel_world_id, render_height, history=None):
  import shutil
  import urllib

  import numpy as np

  from boxm2_scene_adaptor import boxm2_scene_adaptor, scene_bbox
  from boxm2_adaptor import ortho_geo_cam_from_scene, scene_lvcs
  from vpgl_adaptor import convert_local_to_global_coordinates, geo2generic, save_geocam_to_tfw
  from vil_adaptor import save_image, scale_and_offset_values, stretch_image, image_range

  import vsi.io.image

  import voxel_globe.tools
  import voxel_globe.tools.hash
  import voxel_globe.tools.camera
  import voxel_globe.meta.models as models
  import voxel_globe.ingest.payload.tools

  with voxel_globe.tools.task_dir('height_map', cd=True) as processing_dir:
    voxel_world = models.VoxelWorld.objects.get(id=voxel_world_id).history(history)
    scene = boxm2_scene_adaptor(os.path.join(voxel_world.directory, 'scene.xml'), env['VIP_OPENCL_DEVICE'])
    ortho_camera, cols, rows = ortho_geo_cam_from_scene(scene.scene)
    tfw_camera = os.path.join(processing_dir, 'cam.tfw')
    save_geocam_to_tfw(ortho_camera, tfw_camera)
    with open(tfw_camera, 'r') as fid:
      geo_transform = [float(x) for x in fid.readlines()]

    (x0,y0,z0),(x1,y1,z1) = scene_bbox(scene.scene)
    lvcs = scene_lvcs(scene.scene)
    #lvcs = vpgl_adaptor.create_lvcs(lat=origin[1], lon=origin[0], el=origin[2],
    #                                csname="wgs84")
    _,_,min_height = convert_local_to_global_coordinates(lvcs, x0, y0, z0)
    if render_height is None:
      render_height = z1+(z1-z0)/1000
      #z1+(z1-z0)/1000 is basically to say "just a little above the top" *2 is
      #1) overkill and 2) doesn't work with sign, +1 could go crazy in an 
      #arbitrarily scaled system, so this just calculates ".1% more" which is
      #more than good enough
    else:
      render_height = render_height - voxel_world.origin[2]
    logger.critical("Render Height is %f (%s)", render_height, type(render_height))
    generic_camera = geo2generic(ortho_camera, cols, rows, render_height, 0)

    z_exp_img, z_var_img = scene.render_z_image(generic_camera, cols, rows)

    #This is TECHNICALLY wrong, it assumes the earth is flat. 
    scale_and_offset_values(z_exp_img, 1, min_height)
    
    height_filename = os.path.join(processing_dir, 'height.tif')
    
    save_image(z_exp_img, height_filename)

    checksum = voxel_globe.tools.hash.sha256_file(height_filename)
    
    with voxel_globe.tools.image_sha_dir(checksum) as image_dir:
      original_filename = os.path.join(image_dir, 'original.tif')
      
      #If the exact file exist already, don't ingest it again. Unlikely
      if not os.path.exists(original_filename):
        img = vsi.io.image.imread(height_filename)
        vsi.io.image.imwrite_geotiff(img.raster(), original_filename, 
                                     [geo_transform[x] for x in [4,0,1,5,2,3]])
  
        zoomify_filename = os.path.join(image_dir, 'zoomify.tif')
        img_min, img_max = image_range(z_exp_img)
        zoomify_image = stretch_image(z_exp_img, img_min, img_max, 'byte')
        save_image(zoomify_image, zoomify_filename)

        zoomify_name = os.path.join(image_dir, 'zoomify')
        voxel_globe.ingest.payload.tools.zoomify_image(zoomify_filename, zoomify_name)        
  
        relative_file_path = urllib.pathname2url(os.path.relpath(original_filename, 
            env['VIP_IMAGE_SERVER_ROOT']))
        relative_zoom_path = urllib.pathname2url(os.path.relpath(zoomify_name, 
          env['VIP_IMAGE_SERVER_ROOT']))


        img = voxel_globe.meta.models.Image.create(
            name="Height Map %s (%s)" % (voxel_world.name, 
                                         voxel_world.id), 
            imageWidth=cols, imageHeight=rows, 
            numberColorBands=1, pixelFormat='f', fileFormat='zoom', 
            imageUrl='%s://%s:%s/%s/%s/' % (env['VIP_IMAGE_SERVER_PROTOCOL'], 
                                           env['VIP_IMAGE_SERVER_HOST'], 
                                           env['VIP_IMAGE_SERVER_PORT'], 
                                           env['VIP_IMAGE_SERVER_URL_PATH'], 
                                           relative_zoom_path),
            originalImageUrl='%s://%s:%s/%s/%s' % (
                env['VIP_IMAGE_SERVER_PROTOCOL'], 
                env['VIP_IMAGE_SERVER_HOST'], 
                env['VIP_IMAGE_SERVER_PORT'], 
                env['VIP_IMAGE_SERVER_URL_PATH'], 
                relative_file_path),
            service_id=self.request.id,
            original_filename='height_map.tif')
      img.save()

      image_collection = models.ImageCollection.create(
        name="%s Height Map:" % (voxel_world.name,),
        service_id = self.request.id)
      image_collection.save()
      image_collection.images.add(img)

      gsd = scene.description['voxelLength']
      camera_center = ((x0+x1)/2, (y0+y1)/2, z1+10000)
      d = z1-z0+10000
      k=np.eye(3)
      k[0,2] = cols/2
      k[1,2] = rows/2
      k[0,0] = k[1,1] = d/gsd
      r=np.eye(3)
      r[0,0]=-1
      t = -r.T.dot(camera_center)

      voxel_globe.tools.camera.save_krt(self.request.id, img, k, r, t, voxel_world.origin)