Question: How can we convert a .tif with .tfw .htm into a raster KML/KMZ file to open in google earth? I have a .tif file along with a .tfw and .htm files. I wanted to open it in Google Earth. Converting the .tif into a raster in KML/KMZ format will solve the problem. I was working in MATLAB mapping toolbox and did not find a solution in there. A Matlab or any opensource solution will work.

Answer: 
You can use [`ST_Dimension`](https://postgis.net/docs/ST_Dimension.html), which returns the largest dimension of a GeometryCollection's components.

For an undoubtedly excellent reason that's well-rooted in thoughtfully-drafted specifications, [`ST_Dimension`](https://postgis.net/docs/ST_Dimension.html) produces dimensions starting at `0`, while [`ST_CollectionExtract`](https://postgis.net/docs/ST_CollectionExtract.html) accepts dimensions beginning with `1`. (In other words, [`ST_Dimension`](https://postgis.net/docs/ST_Dimension.html) uses `2` to indicate a polygon, while [`ST_CollectionExtract`](https://postgis.net/docs/ST_CollectionExtract.html) uses `3` to indicate a polygon.) So to extract the highest-dimension geometries from a GeometryCollection, you would use:

```
SELECT ST_CollectionExtract(geom, 1 + ST_Dimension(geom))
FROM my_data;

```
I think there must be a more elegant way of doing this, but one approach would be to combine three UNION SELECTS with three CASE statements testing for each geomety type, combined with an ST\_IsEmpty test returning -1 if the type is not found, and then use the max of the result of that in a final select. This returns a MultiPolygon, as I used the union of the test input geometries in the subquery extracts. This would need tweaking to deal with @user30148's question

```
WITH geoms (geom) AS 
     (VALUES 
          (ST_Makepoint(0,0)), 
          (ST_Buffer(ST_Makepoint(1, 10), 2)), 
          (ST_Buffer(ST_Makepoint(50, 50), 5))), 
    collection(geom) AS (
       SELECT ST_Union(geom) 
         FROM geoms
   ),
   extracts(geom_type) AS (
      SELECT CASE 
               WHEN NOT ST_Isempty(ST_CollectionExtract(geom, 3)) 
               THEN 3 
               ELSE -1 END 
       FROM collection 
        UNION 
      SELECT CASE 
               WHEN NOT ST_Isempty(ST_CollectionExtract(geom, 2)) 
               THEN 2 
               ELSE -1 
               END 
        FROM collection 
        UNION
      SELECT CASE 
               WHEN NOT ST_Isempty(ST_CollectionExtract(geom, 1)) 
               THEN 1 
               ELSE -1 END 
         FROM collection
  ), 
  max_type(geom_type) AS (
       SELECT max(geom_type) FROM extracts
  )
  SELECT ST_CollectionExtract(geom, geom_type)
    FROM collection, max_type;

```