2

I'm trying to export a shapefile of administrative units out of GEE, but I get the error Shapefiles cannot contain multiple geometry types; found 'LineString', 'Polygon'

Here is my code and the feature collection I'm trying to export:

// Create variable with FAO Admin 2 boundaries, adm2
var adm2 = ee.FeatureCollection('FAO/GAUL/2015/level2');

// filter adm2 to only keep the municipality of interest, Ceiba
var ceiba = adm2.filter(ee.Filter.eq('ADM2_NAME', 'Ceiba Municipio'));

// add this layer to the map and center there
// Map.addLayer(ceiba, {}, 'Ceiba');

// // Export the ceiba shapefile to Drive
Export.table.toDrive({
collection: ceiba,
description:'faoadmin2_ceiba_only',
fileFormat: 'SHP'
 });

// "error: Shapefiles cannot contain multiple geometry types; found 'LineString', 'Polygon'."

The problem is that the Geometry of the feature is a GeometryCollection. Within that geometry collection there are 118 geometries, and all but one are Polygons. Geometry 0 is a LineString. In fact, it looks like a mistake to me (see visualization below), a random line off of one polygon, so I would like to delete the LineString geometry type or to filter it out.

// explore Ceiba in the console
print(ceiba);

// There are 118 geometries, all but one are polygons. Geometry 0, is a LineString 
// Visualize this line in the map
var line_in_ceiba = ee.Geometry.LineString([[-65.5991688684573,18.21315366178916], [-65.5991688684573,18.2131492026881]]);
var point_in_ceiba = ee.Geometry.Point([-65.5991688684573,18.21315366178916]); // just for visibility
Map.addLayer(line_in_ceiba , {color: 'FF0000'}, 'line in Ceiba');
Map.addLayer(point_in_ceiba, {color: 'FF0000'}, 'start point in Ceiba');
Map.centerObject(line_in_ceiba, 20);

So far, I've tried setting the geometries as a property based on this answer, adding it as a new column using map, and trying to filter by that property

ceiba = ceiba.map(function(f) {
  return f.set('geo_type', f.geometry().geometries())
});

print(ceiba);
print(ceiba.filter(ee.Filter.eq('geo_type', 'Polygon')));

However, this doesn't quite work because the new column "geotype" now has a list of geometries, not individual elements.

How may I delete or filter out the LineString geometry type? My main issue is that I need some of the FAO GAUL 2015 data as a shapefile, If there's an alternative way to export (or even to access it outside of GEE), I'm open to that too!

1 Answer 1

1

If you just want to keep the polygons, you can do the followings.

var ceiba_polygon = ceiba.map(function (f) {
    var geometries = f.geometry().geometries()                                      // all geometries are mixed
    var geometries_polygon = geometries.filter(ee.Filter.hasType('item','Polygon')) // only keep the polygons
    var geometry = ee.Geometry.MultiPolygon(geometries_polygon)                     // turn into a single geometry, namely the MultiPolygon
    return ee.Feature(geometry).copyProperties(f)                                   // make sure you don't lose the properties
});
Export.table.toDrive({
    collection: ceiba_polygon,
    description:'ceiba_polygon',
    fileFormat: 'SHP'
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.