Calculate road distance

Distance from primary, secondary and tertiary roads

Step 1: import the osm data and select the primary, secondary, tertiary and other roads from the feature collection

// import country boundaries
var countries = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017");
var cambodia = countries.filter(ee.Filter.eq("country_na","Cambodia"));
 
//import road feature
var roads = ee.FeatureCollection('projects/servir-mekong/osm/cambodia/gis_osm_roads');
// import osm health center data
var osmhealthCenter = ee.FeatureCollection("projects/servir-mekong/osm/cambodia/hotosm_khm_health_facilities").filterBounds(cambodia);
 
// print the road categories
print("road categories:", roads.aggregate_histogram("fclass"));
 
// create lists to categorize the road networks
var primary = ["primary","primary_link"];
var secondary = ["secondary","secondary_link"];
var tertiary = ["tertiary","tertiary_link"];
var other = ee.List(roads.aggregate_histogram("fclass").keys()).removeAll(primary).removeAll(secondary).removeAll(tertiary);
 
// filter for primary, secondary and tertiary roads
var primaryRoads = roads.filter(ee.Filter.inList("fclass",primary));
var secondaryRoads = roads.filter(ee.Filter.inList("fclass",secondary));
var tertiaryRoads = roads.filter(ee.Filter.inList("fclass",tertiary));
var otherRoads = roads.filter(ee.Filter.inList("fclass",other));

Map.addLayer(primaryRoads,{},"Primary roads");
Map.addLayer(secondaryRoads,{},"Secondary roads");
Map.addLayer(tertiaryRoads,{},"Tertiaryroads");
Map.addLayer(otherRoads,{},"otherroads");

Step 2: calculate the distance to road and scale the values

// calculate the road distance
var costPrimary = primaryRoads.distance(10*10000).clip(cambodia);
var costSecondary = secondaryRoads.distance(10*10000).clip(cambodia);
var costTertiary = tertiaryRoads.distance(10*10000).clip(cambodia);
var costOther = otherRoads.distance(10*10000).clip(cambodia);

// scale the values
costPrimary = costPrimary.divide(10*10000).unmask(1).multiply(100);
costSecondary  = costSecondary .divide(10*10000).unmask(1).multiply(100);
costTertiary = costTertiary.divide(10*10000).unmask(1).multiply(100);
costOther = costOther.divide(10*10000).unmask(1).multiply(100);

// add layer to vizualize
Map.addLayer(costPrimary.clip(cambodia),{min:0,max:100},"cost primary road");

Step 3: export the layers to an image collection. Note to change the export path.

// export the imagery
Export.image.toAsset({image:costPrimary.toInt(), description:"cost", assetId:"yourasset/distanceLayers/primaryRoads", region:cambodia, scale:100, crs:"EPSG:32648", maxPixels:1e13});
Export.image.toAsset({image:costSecondary.toInt(), description:"cost", assetId:"yourasset/distanceLayers/secondaryRoads", region:cambodia, scale:100, crs:"EPSG:32648", maxPixels:1e13});
Export.image.toAsset({image:costTertiary.toInt(), description:"cost", assetId:"yourasset/distanceLayers/tertiaryRoads", region:cambodia, scale:100, crs:"EPSG:32648", maxPixels:1e13});
Export.image.toAsset({image:costOther.toInt(), description:"cost", assetId:"yourasset/distanceLayers/otherRoads", region:cambodia, scale:100, crs:"EPSG:32648", maxPixels:1e13});

Find the full code here

Leave a Reply