Planet Modding/Real World Data

From Medieval Engineers Wiki
Jump to navigation Jump to search

Lots of real world data exists for the real planet Earth, including worldwide infrastructure data, elevation data, land cover (plant) data], and satellite imagery. This data is typically distributed as very large images (sometimes thousands of images) that contain georeferencing data that ties those images to points on the earth. It is possible to convert from these real world locations into Medieval Engineers' planet data files, allowing for using real world data for modded planets without distortion.

Georeferencing

Medieval Engineers' planet surfaces are defined by six separate faces, each one using a specially distorted Gnomonic projection such that pixels are as close to a constant area as possible. This special distortion can be represented in GDAL using a geolocation array, and is written in code as the Sandbox.Engine.Voxels.MyCubemapHelpers.UniformToProjection method.

ME to Real World

The arguments to gdalwarp required to convert to/from this method vary for each face that is being converted, and reference the geolocation array at me-geolocation-16k.png. These options are correct for converting from ME data to real world data.

REM Forward
-to "SRC_SRS=+proj=gnom +lat_0=0 +lon_0=0 +R=16000 +x_0=16000 +y_0=16000" -to "SRC_GEOLOC_ARRAY=me-geolocation-16k.png"
REM Left
-to "SRC_SRS=+proj=gnom +lat_0=0 +lon_0=-90 +R=16000 +x_0=16000 +y_0=16000" -to "SRC_GEOLOC_ARRAY=me-geolocation-16k.png"
REM Back
-to "SRC_SRS=+proj=gnom +lat_0=0 +lon_0=-180 +R=16000 +x_0=16000 +y_0=16000" -to "SRC_GEOLOC_ARRAY=me-geolocation-16k.png"
REM Right
-to "SRC_SRS=+proj=gnom +lat_0=0 +lon_0=90 +R=16000 +x_0=16000 +y_0=16000" -to "SRC_GEOLOC_ARRAY=me-geolocation-16k.png"
REM Up
-to "SRC_SRS=+proj=gnom +lat_0=90 +lon_0=0 +R=16000 +x_0=16000 +y_0=16000" -to "SRC_GEOLOC_ARRAY=me-geolocation-16k.png"
REM Down
-to "SRC_SRS=+proj=gnom +lat_0=-90 +lon_0=180 +R=16000 +x_0=16000 +y_0=16000" -to "SRC_GEOLOC_ARRAY=me-geolocation-16k.png"

Real World to ME

For converting real world data to ME data conceptually the above options must just be inverted (SRC becomes DST) however GDAL does not support setting output georeferencing using transformer options. Instead the output file must be manually created, and then updated by using gdalwarp. Create template files front.vrt, left.vrt, right.vrt, back.vrt, down.vrt, and up.vrt using the following.

<!-- Set these to the power of two of the output file resolution -->
<VRTDataset rasterXSize="8192" rasterYSize="8192">

  <!-- band layout for elevation data -->
  <VRTRasterBand dataType="UInt16" band="1" />
  
  <!-- band layout for material maps -->
  <VRTRasterBand dataType="Byte" band="1" />
  <VRTRasterBand dataType="Byte" band="2" />
  <VRTRasterBand dataType="Byte" band="3" />

  <Metadata domain="GEOLOCATION">
    <!-- Set the SRS here to the corresponding SRC_SRS for the face mentioned previously -->
    <MDI key="SRS">+proj=gnom +lat_0=0 +lon_0=0 +R=16000 +x_0=16000 +y_0=16000</MDI>
    <!-- The path to the me-geolocation-16k.png file -->
    <MDI key="X_DATASET">me-geolocation-16k.png</MDI>
    <MDI key="Y_DATASET">me-geolocation-16k.png</MDI>
    <MDI key="X_BAND">1</MDI>
    <MDI key="Y_BAND">2</MDI>
    <MDI key="PIXEL_OFFSET">0</MDI>
    <MDI key="LINE_OFFSET">0</MDI>
    <!-- Output file resolution divided by 1024 (the geolocation file resolution) -->
    <MDI key="PIXEL_STEP">8</MDI>
    <MDI key="LINE_STEP">8</MDI>
  </Metadata>
</VRTDataset>

Convert these template files into output files using gdal_translate front.vrt front.tif -co COPY_SRC_MDD=YES, edit the output file using any geospatial tool, then convert the intermediate output into a game compatible file using gdal_translate front.tif front.png.

Examples

These examples assume you've downloaded and installed GDAL and the geolocation file.

Real World Elevation

This example will use the ETOPO1 dataset to provide global elevation data. Manually create the template files for elevation data.

REM Scale elevation values so the elevations are all positive, convert to float, and set the SRS to long/lat
gdal_translate ETOPO1_Bed_g_geotiff.tif -scale -12000 12000 0 65535 -ot Float32 -a_srs "+proj=longlat" ETOPO1_Unsigned.vrt
REM Ignore translation from Earth to 16km sphere
set PROJ_IGNORE_CELESTIAL_BODY=YES
REM Disable metadata files
set GDAL_PAM_ENABLED=NO

for %%x in (front, left, back, right, up, down) do (
	echo Create %%x
	del %%x.tif
	gdal_translate %%x.vrt %%x.tif -co COPY_SRC_MDD=YES
	gdalwarp -dstnodata None -r average -wt Float32 -multi -wo SOURCE_EXTRA=100 -wo SAMPLE_GRID=YES ETOPO1_Unsigned.vrt %%x.tif
	gdal_translate %%x.tif %%x.png
)