# Layer data base64

**URL:** https://discourse.mapeditor.org/t/layer-data-base64/4772
**Category:** Uncategorized
**Created:** [October 11, 2020, 12:21am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772 "2020-10-11T00:21:38Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 11, 2020, 12:21am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/1 "2020-10-11T00:21:38Z")

</div>

Hi, why does tiled encode the layer data in base64?  
How I can avoid this?

![Captura](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/b/bc2ede88884e1a701ae2c8767bf6f0248d4a89b0.png)

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [October 11, 2020, 2:42am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/2 "2020-10-11T02:42:48Z")

</div>

This is done for compressed data, because otherwise the binary data could contain characters which would be invalid in XML and JSON.

If you want to use uncompressed, unencoded data, set the Tile Layer Format format to CSV in the Map Properties. This will make your files much larger though, so I recommend sticking with the compressed formats unless your engine is for some reason lacking any ability to decompress the data. If you do decide to use compression, I regommend gzip or zlib rather than the Zstandard you currently have selected, as they’re more widely supported.

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 11, 2020, 9:33am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/3 "2020-10-11T09:33:04Z")

</div>

Ah, but exporting as CSV does not save the state of the tiles (flipped horizontal or vertical).

So how do I select the type of compression? If I want to use zlib, how do I do it?

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [October 11, 2020, 1:02pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/4 "2020-10-11T13:02:21Z")

</div>

If you select CSV as the _Tile Layer Format_ in Map Properties, as I said, it’ll include flipped tiles. It’s only the CSV format export that does not, but that is an entirely separate thing.

You can access the Map Properties via `Map > Map Properties...`. You’ll find the Tile Layer Format field there, with the options I mentioned in my previous post.

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 15, 2020, 5:02pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/5 "2020-10-15T17:02:39Z")

</div>

Thanks for the help, ok I am using base64 for the data.

Now I get the data correctly but … how do I find out if the tile is rotated or flipped? Where do I get that information?

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [October 15, 2020, 5:13pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/6 "2020-10-15T17:13:24Z")

</div>

That data is included in the tileID.

The tileID consists of the global tile ID and the flip flags. I recently explained how to extract them here:

> [@Tiled csv format has exceptionally large tile values (SOLVED)](https://discourse.mapeditor.org/t/tiled-csv-format-has-exceptionally-large-tile-values-solved/4765/2):
>
> Those large values are tiles with flip flags set, they’re telling you which way(s) the tile is flipped. To extract the flip flags and then extract just the tileID, you’ll need to do some bitwise logic: horizontalFlip = tile & 0x80000000; verticalFlip = tile & 0x40000000; diagonalFlip = tile & 0x20000000; tileID = tile & ~(0x80000000 | 0x40000000 | 0x20000000); //clear the flags Edit: Documentation: [https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tile-flipping](https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tile-flipping)

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 16, 2020, 2:48pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/7 "2020-10-16T14:48:16Z")

</div>

Well i do not understand.  
The values I get in CSV shouldn’t they be different? since the tile is in various shapes flipped horizontally and vertically but still I get the same value “452”.  
 ![Captura](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/f/f2a2c1afb9d86803715db06c20ccdd5bb944c993.png) ![Captura2](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/2/23c28152fd099cd506aab09e5339c724084ab556.png) ![Captura3](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/8/8303d9f2f4cc83b397f646a5e1e3d0d6d4912a33.png)

I want to export for CSV and obtain the values that indicate the state of each tile.  
It’s not possible?

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [October 16, 2020, 3:42pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/8 "2020-10-16T15:42:28Z")

</div>

It looks like you exported as CSV, which does not keep the flips. The CSV export is **completely separate** from the Tile Layer Format CSV. If you want CSV _with_ the flip data impact, you need to export as TMX with Tile Layer Format CSV and copy the data from there, or write a custom CSV format that keeps the flip data.

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 16, 2020, 4:26pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/9 "2020-10-16T16:26:57Z")

</div>

Ok but does it make any sense that the CSV export doesn’t save the state of the tiles?  
That is to say:  
-If you export in CSV the state of the tiles is not obtained  
-If you use CSV to export tile layers then the state of the tiles is obtained

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [October 16, 2020, 5:11pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/10 "2020-10-16T17:11:22Z")

</div>

Presumably the CSV export was designed to solve a specific export problem rather than to be a generic file format, just like any other custom file format. It’s possible the CSV export was designed for some engine that doesn’t flipping tiles. Just like CSV doesn’t include object layers and map metadata, it doesn’t include tile flipping.

The CSV tile data format, on the other hand, _is not an export format_, it’s simply the way the tile data is stored _within the TMX_, the file format native to Tiled. Because it’s still TMX, it has to support all the features of TMX, and this includes flipping.

CSV export and CSV data format are just using the same data representation (CSV) to achieve different goals. That’s also why, for example, TMX with CSV format is still just one file, while CSV has to export multiple files (one per layer). They’re different in what they do and how they do it, they just happen to both use comma-separated values (CSV).

_That said_, if the current CSV format is included with Tiled, I don’t see why a version of it that includes flip flags cannot be. @bjorn Any chance of this being added?

In the meantime, @The_Mnk, what are you actually trying to achieve? You started this thread asking what the base64 data is, now you’re asking about CSV. What do you actually want to do? Are you looking into parsing TMX? Do you want just the layer data without all the other stuff TMX includes? I suspect we can get your problems resolved much faster if you explain what your end goal is.

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 16, 2020, 5:52pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/11 "2020-10-16T17:52:13Z")

</div>

Ok I understand.  
I need to get the state data of each tile with the export in CSV, that’s all.  
“File\>Export as CSV”

I tried with JSON but JSON includes a lot of data that I don’t need. Also the issue got complicated when unzipping base64 due to a datatype issue.

I am trying to modify this code to get what I want but I don’t know how to do it.  
Apparently the code is already saving the ID of each tile but that ID does not include the state of the tile (flipped horizontally or vertically etc …)

> **[Python Scripts — Tiled 1.4.2 documentation](https://doc.mapeditor.org/en/latest/manual/python/)**

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [October 16, 2020, 6:52pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/12 "2020-10-16T18:52:48Z")

</div>

When you decode the base64 data, you get a compressed bunch of binary data. If you decompress that, you get a binary bunch of `int`s. In short, a ready-made array of tile IDs that you can put directly into memory, without ever parsing it as a string beyond the initial base64 part. IMO it’s actually the easiest option for using Tiled layer data in a program, as it requires the least manual parsing, you get your data in a form your program can use directly.

If you require a CSV with flip data and without any extra data, you’ll need to write a custom export format. If all your maps have only one layer, this is very simple. If you have multiple layers, you’ll need to decide whether you want to store them in one file or multiple, and how to tell your program what the layers are.

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 16, 2020, 7:47pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/13 "2020-10-16T19:47:41Z")

</div>

I’d rather do it in CSV. I don’t know javascript or python, the only thing I could do is modify the python example script that I put before so that it exports the information I need. I think it will be easy to modify it but I do not know python. Hope bjorn can help out.

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [October 16, 2020, 8:53pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/14 "2020-10-16T20:53:09Z")

</div>

I’m more familiar with JavaScript than Python, so here’s a JavaScript CSV exporter that includes the flip flags:

```auto
var customMapFormat = {
    name = "CSV with Flip Data",
    extension = "csv",
    
    write: function(map, fileName) {
		var prefix = fileName.search(".csv");
		if(prefix >= 0) prefix = fileName.substr(0, prefix);
		else prefix = fileName;
		var numLayers = 0;
		for (var i = 0; i < map.layerCount; ++i) {
            var layer = map.layerAt(i);
            if (layer.isTileLayer) {
				var file = new TextFile(prefix+"_"+numLayers+"_"+layer.name+".csv", TextFile.WriteOnly);
				var output;
                for (y = 0; y < layer.height; ++y) {
                    output = "";
                    for (x = 0; x < layer.width; ++x) {
						if(x > 0) output += ",";
						var cell = layer.cellAt(x, y);
						var tile = cell.tileId;
						if(cell.flippedHorizontally) tile = tile | 0x80000000;
						if(cell.flippedVertically) tile = tile | 0x40000000;
						if(cell.flippedAntiDiagonally) tile = tile | 0x20000000;
						if(cell.rotatedHexagonal120) tile = tile | 0x10000000;
                        output += tile;
					}
					file.writeLine(output);
                }
				file.commit();
				numLayers++;
            }
        }
	}
}

tiled.registerMapFormat("CSV with Flip Data", customMapFormat);

```

Just like the included CSV exporter, it exports a separate CSV file per tile layer, and uses 0-based indexing for tiles, i.e. the same index they’d have in a tileset, and -1 denotes empty cells.  
Unlike the included CSV exporter, it includes the layer number in the filenames so that the layer order can be preserved when you load the files, and it includes the flip flags. Extract them and the tile ID the way shown in the post I linked earlier in the thread.  
This code only includes a “write” method, it does not include any way to read the data back into Tiled. So, only use this as an export format, don’t use this as your main format for WIP maps.

To use this, save the code above to a file with the extension `.js`, and put it in your `extensions` directory. The location of that depends on your OS, the documentation will tell you where to look: [https://doc.mapeditor.org/en/stable/reference/scripting/#scripted-extensions](https://doc.mapeditor.org/en/stable/reference/scripting/#scripted-extensions)

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 17, 2020, 9:44am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/15 "2020-10-17T09:44:33Z")

</div>

Ok I have it! Thanks for this, now I can continue my little project.  
There is no place to publish this extension ?.  
I mean, the Tiled website doesn’t have a section to publish these user extensions?

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [October 17, 2020, 1:32pm UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/16 "2020-10-17T13:32:05Z")

</div>

There’s the [Tiled extensions Github repo](https://github.com/mapeditor/tiled-extensions), but that’s a hassle to submit to.

---

<div class="post-metadata">

### Author: ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)
#### Post date: [October 20, 2020, 9:01am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/17 "2020-10-20T09:01:14Z")

</div>

> [@eishiya](#):
>
> There’s the [Tiled extensions Github repo](https://github.com/mapeditor/tiled-extensions), but that’s a hassle to submit to.

Is it really? Is there something I could do to make it easier? I think your custom CSV format example would be a great addition!

> [@eishiya](#):
>
> _That said_ , if the current CSV format is included with Tiled, I don’t see why a version of it that includes flip flags cannot be. @bjorn Any chance of this being added?

The CSV export was included originally for somebody who wanted to export maps to Excel I believe, and it supports setting a name on each tile so that you can export using those rather than tile IDs. Indeed support for flipping did not make sense in that use-case.

But I guess when we’re exporting using the tile IDs adding flipping bits might be more useful than stripping them out, even if it results in big numbers that will confuse some people.

---

<div class="post-metadata">

### Author: ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)
#### Post date: [October 20, 2020, 9:39am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/18 "2020-10-20T09:39:13Z")

</div>

> <https://github.com/bjorn/tiled/commit/e0293ed6a40b2dc086cddf8cd5ad650869ed48f0>

So this will be included in Tiled 1.5, but using a custom script for this is probably a better idea.

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 20, 2020, 10:15am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/19 "2020-10-20T10:15:48Z")

</div>

In my case it was preferable to use the custom eishiya script because I needed to make the file extension txt (not CSV) for Godot.

---

<div class="post-metadata">

### Author: ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)
#### Post date: [October 20, 2020, 10:20am UTC](https://discourse.mapeditor.org/t/layer-data-base64/4772/20 "2020-10-20T10:20:37Z")

</div>

Hi, I think you forgot to include one of the transformations? specifically “RotatedHexagonal”.  
Although it is easy to include it manually I think.  
 ![Captura](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/5/58bba7ab1f19cb616697ad27aa695eaaf5dcac85.png)  
 ![Captura](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/a/a0be8c63a346e722334e1e13b65b1bc58e4b2b44.png)

[Next page](https://discourse.mapeditor.org/t/layer-data-base64/4772.md?page=2)
