TransWikia.com

Filter through geometries of a feature collection in Google Earth Engine to export as a shapefile

Geographic Information Systems Asked by Gabriela A on July 12, 2021

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!

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP