<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>Create a FeatureLayer from a shapefile | Sample | ArcGIS Maps SDK for JavaScript</title>
width: min(320px, calc(100vw - 32px));
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
<script type="module" src="https://js.arcgis.com/5.2/"></script>
<arcgis-map basemap="dark-gray-vector" center="-41.647, 36.41" zoom="2">
<arcgis-zoom slot="top-left"></arcgis-zoom>
<arcgis-popup slot="popup" default-popup-template-enabled></arcgis-popup>
<arcgis-expand slot="top-right" expand-icon="upload" expand-tooltip="Add a shapefile" expanded>
<calcite-panel heading="Add a shapefile" description="Create a FeatureLayer from a zipped shapefile.">
<div class="upload-content">
href="%prefix%/assets/sample-code/layers-featurelayer-shapefile/drp_county_boundary.zip"
Download sample shapefile
<calcite-button id="choose-file" icon-start="upload" width="full">Choose a ZIP file</calcite-button>
<input id="file-input" type="file" accept=".zip,application/zip" hidden />
<calcite-notice id="upload-status" open icon="information" kind="info" aria-live="polite">
<div slot="title" id="status-title">Ready to upload</div>
<div slot="message" id="status-message">
Download the sample or choose your own .zip file. Files are sent to ArcGIS Online to generate features.
href="https://doc.esri.com/en/arcgis-online/latest/reference/shapefiles.html"
Shapefile requirements and limitations
const [request, FeatureLayer, Field, Graphic] = await $arcgis.import([
"@arcgis/core/request.js",
"@arcgis/core/layers/FeatureLayer.js",
"@arcgis/core/layers/support/Field.js",
"@arcgis/core/Graphic.js",
const portalUrl = "https://www.arcgis.com";
const viewElement = document.querySelector("arcgis-map");
const chooseButton = document.getElementById("choose-file");
const fileInput = document.getElementById("file-input");
const status = document.getElementById("upload-status");
const statusTitle = document.getElementById("status-title");
const statusMessage = document.getElementById("status-message");
await viewElement.viewOnReady();
chooseButton.addEventListener("click", () => fileInput.click());
fileInput.addEventListener("change", async () => {
const file = fileInput.files[0];
if (!/\.zip$/i.test(file.name)) {
showStatus("Choose a ZIP file", "The shapefile must be packaged in a .zip archive.", "danger");
chooseButton.loading = true;
chooseButton.disabled = true;
fileInput.disabled = true;
showStatus("Loading shapefile", file.name);
await generateFeatureCollection(file);
showStatus("Shapefile added", `${file.name} is ready to explore on the map.`, "success");
showStatus("Unable to add shapefile", error.message, "danger");
chooseButton.loading = false;
chooseButton.disabled = false;
fileInput.disabled = false;
function showStatus(title, message, kind = "info") {
kind === "danger" ? "exclamation-mark-triangle" : kind === "success" ? "check-circle" : "information";
statusTitle.textContent = title;
statusMessage.textContent = message;
async function generateFeatureCollection(file) {
// define the input params for generate see the rest doc for details
// https://developers.arcgis.com/rest/users-groups-and-items/generate.htm
name: file.name.replace(/\.zip$/i, ""),
targetSR: viewElement.spatialReference,
enforceInputFileSizeLimit: true,
enforceOutputJsonSizeLimit: true,
// generalize features to 10 meters for better performance
params.generalize = true;
params.maxAllowableOffset = 10;
params.reducePrecision = true;
params.numberOfDigitsAfterDecimal = 0;
publishParameters: JSON.stringify(params),
// use the REST generate operation to generate a feature collection from the zipped shapefile
const body = new FormData();
body.append("file", file);
const response = await request(portalUrl + "/sharing/rest/content/features/generate", {
addShapefileToMap(response.data.featureCollection);
function addShapefileToMap(featureCollection) {
// add the shapefile to the map and zoom to the feature collection extent
// if you want to persist the feature collection when you reload browser, you could store the
// collection in local storage by serializing the layer using featureLayer.toJson()
// see the 'Feature Collection in Local Storage' sample for an example of how to work with local storage
const layers = featureCollection.layers.map((layer) => {
const graphics = layer.featureSet.features.map((feature) => {
return Graphic.fromJSON(feature);
sourceGraphics = sourceGraphics.concat(graphics);
const featureLayer = new FeatureLayer({
fields: layer.layerDefinition.fields.map((field) => {
return Field.fromJSON(field);
// associate the feature with the popup on click to enable highlight and zoom to
viewElement.map.addMany(layers);
viewElement.goTo(sourceGraphics).catch((error) => {
if (error.name != "AbortError") {