1

I am trying to extract some cities' boundaries as .shp but only for one city (kars) it gives following error:

filesError: Shapefiles cannot contain multiple geometry types; found 'LineString', 'Polygon'. (Error code: 3)

Allcities borders are shown in the map as layer, and I can download them except kars, I cannot extract it to my drive. Do you have any suggestions to solve this problem? Any another way to download FAO GAUL first administrative level shapefiles is also okay.

here is my code:

var gaul = ee.FeatureCollection("FAO/GAUL/2015/level1");

var turkey = gaul.filter(ee.Filter.eq('ADM0_CODE', 249));

var cities = ['Ankara', 'Konya', 'Sanliurfa', 'Gaziantep', 'Diyarbakir', 'Kayseri', 
              'Van', 'Eskisehir', 'Mardin', 'Malatya', 'Erzurum', 'Afyon', 'Sivas', 
              'Adiyaman', 'Batman', 'Elazig', 'Corum', 'Agri', 'Aksaray', 'Yozgat', 
              'Nigde', 'Nevsehir', 'Kars', 'Kirikkale', 'Karaman', 
              'Kirsehir', 'Erzincan', 'Igdir', 'Cankiri', 'Gumushane', 'Kilis', 
              'Ardahan', 'Bayburt'];

var visParams = {
  color: 'blue',
  fillColor: '0000FFFF',
  width: 3
};

cities.forEach(function(city) {
  var cityFeature = turkey.filter(ee.Filter.eq('ADM1_NAME', city));
  Map.addLayer(cityFeature, visParams, city);
  
  var exportParams = {
    collection: cityFeature,
    description: city,
    fileNamePrefix: city, 
    fileFormat: 'SHP'
  };
  
  Export.table.toDrive(exportParams);
});
2

2 Answers 2

2

As stated in the error and the answer provided by @prusswan, the problem is that a feature contains multiple geometry types. Particularly in this case, the city of 'Kars' contains a Geometry Collection as geometry with two geometries: 'LineString' and 'Polygon'. Here is a solution to filter the geometries and stay only with the 'Polygon' one.

// All cities in the list of interest
var cityFeatures = turkey.filter(ee.Filter.inList('ADM1_NAME', cities));

// Get geometry types for each Feature in the collection
var geomColFunc = function(feat){
  var geomType = ee.Feature(feat).geometry().type();
  return ee.Feature(feat).set('geomType', ee.String(geomType));
};

// Map geom type
var cityFeatures = cityFeatures.map(geomColFunc);

// Filter collection to separate features with polygons as geometry and
// features with geometry collections.
var citiesPolys = cityFeatures.filter(ee.Filter.eq('geomType', 'Polygon'));
var citiesNoPolys = cityFeatures.filter(ee.Filter.neq('geomType', 'Polygon'));

// See which cities are in any pf the two collections
print('citiesPolys', citiesPolys);
print('citiesNoPolys', citiesNoPolys);

// For the features that have geometry collections as geometries, get the types of their
// geometries contained in the geometry collection and filter to remove any geometry
// that is not a polygon, e.g., LineString.
// Then create a feature with that polygon as geometry and copy properties
// Map this function over the features inside the citiesNoPolys collection
citiesNoPolys = citiesNoPolys.map(function(feat){
  // Get geometry and geometries from the Geometry Collection
  var geoms = feat.geometry()
    .geometries()
    // For each geometry get its type and create a feature with that geometry
    // and a property that indicates the type of geometry (i.e., Polygon or Line String)
    .map(function(geom){
      var geomType = ee.Geometry(geom).type();
      return ee.Feature(ee.Geometry(geom)).set('geomType', geomType);
    });
  // Cast list of features into a feature collection and filter to stay
  // with features that were created from a Polygon geometry
  var filtFeat = ee.FeatureCollection(geoms).filter(ee.Filter.eq('geomType', 'Polygon'));
  // Return first feature to convert from FeatureCollection to Feature and copy properties
  return ee.Feature(filtFeat.first()).copyProperties(feat);
});

print('citiesNoPolys', citiesNoPolys);

// Merge both collections
var finalCol = citiesPolys.merge(citiesNoPolys);
print('finalCol', finalCol);
0
1

As stated in the error message, "Shapefiles cannot contain multiple geometry types". So you need to choose a different export format (other than SHP) if the source data contain multiple geometry types. This constraint is also addressed in EE documentation: https://developers.google.com/earth-engine/guides/exporting#exporting_tables_and_vector_data

Another option is to filter the data by geometry type and export them into separate SHP files.

0

Your Answer

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