# How CARTO (or other GIS-software) decides where labels are located

I´m calculating the distances between different places in my region/country.

I don´t know where I can find a database of all the towns/cities already geolocated, so I have a question for considering a "middle point" of each region of the shapefile:

How CARTO (or other GIS-software) decides where to put the labels (in bold in the image below) of each feature?

My target is getting the centroid and consider that that´s the center of each town/city.

• check geonames.org for a global place name gazetteer that you can download. You could use the coordinates for your distance calculations. – mixedbredie Aug 31 '17 at 14:26

That is a creative idea, but I don't think it will work. This is because CARTO (or any other software) will also use some conflict avoidance algorithm to make sure the names don't overlap and obscure each other.

@mixedbredie has a good solution, but if you didn't wanted to base everythign on the data you have then how about this:

N.B for this answer I'm assuming those names correspond to the polygons which are coloured according to the numbers under the names.

Assuming you have access to the layer in CARTO then, based on their docs, you would write something like:

SELECT town_id, town_name, ST_Centroid(the_geom) as the_geom,  ST_Transform(ST_Centroid(the_geom), 3857) as the_geom_webmercator FROM {table_name}


This would return point geometries at the center of each polygon. You could then use these to calculate the distances you want.

CARTO BUILDER places labels according to the CartoCSS properties which are applied. Some of the can be changed using the UI:

Labels are placed on the centroid of the geometry as explained by @RoperMaps, but moved 10px "upwards". You can check the code that is working behind those setting switching to the CartoCSS panel:

#layer {
marker-width: 7;
marker-fill: #FFB927;
marker-fill-opacity: 0.9;
marker-allow-overlap: true;
marker-line-width: 1;
marker-line-color: #FFF;
marker-line-opacity: 1;
}
/#layer::labels {
text-name: [city];
text-face-name: 'DejaVu Sans Book';
text-size: 10;
text-fill: #FFFFFF;
text-label-position-tolerance: 0;

You can fine tune the labels locations updating text-dy and text-dx CartoCSS properties.
Postdata: CARTO has some nice open datasets in the Data Library such as Populated Places for world cities and towns. Also, if you want to conver those polygons to points you can use Find centroid of geometries analysis.