Undefined SpatialReference

693
7
Jump to solution
12-07-2023 12:35 PM
Charan
by
New Contributor III

So I want to create a buffer around addresses (received via user upload) and extract aggregated data for them but I'm getting issues around the geometry() in BufferGeom(). Could someone please help me understand and resolve this issue?

 

 

            const address_geo = []

            document.getElementById("download-button").onclick = () => {
              var fileInput = document.getElementById('fileInput');
              var file = fileInput.files[0];
              if (file) {
                var reader = new FileReader();

                reader.onload = function (e) {
                  var data = e.target.result;
                  var workbook = XLSX.read(data, { type: 'binary' });
                  var sheetName = workbook.SheetNames[0];
                  var sheet = workbook.Sheets[sheetName];

                  var dataArray = XLSX.utils.sheet_to_json(sheet, { header: 1 });
                  list_address = dataArray.flat()
                  const geocode = "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
                  for (var i = 0; i < list_address.length; i++) {
                    const addressinput = list_address[i]
                    const params = {
                      address: {
                        address: addressinput,
                      },
                      countryCode: "CAN",
                    };
                    locator.addressToLocations(geocode, params).then((results) => {
                      showResult(results);
                    });
                    function showResult(results) {
                      if (results.length) {
                        const result = results[0];
                        uniqueAddressID2.push(result.address);

                        const point = new Graphic({
                        symbol: {
                          type: "simple-marker",
                          style: "circle",
                          size: "16px",
                          color: [70, 90, 141, 0.4],
                        },
                        geometry: result.location,
                        attributes: {
                          title: "Address",
                          address: result.address,
                          score: result.score,
                        },
                      });
                      address_geo.push(point)

                      }
                    };
                  }
                  BufferGeom(address_geo)
                };
                reader.readAsBinaryString(file);
              } else {
                alert('Please select a file.');
              }
            }
            function BufferGeom(geo_points) {
              var d = document.getElementById("distance");
              const buffGeoms = geometryEngine.geodesicBuffer(
                geo_points,
                d.value,
                "kilometers"
              );
              buffGeoms.forEach((buffGeom) => {
                const buffgra = new Graphic({
                  geometry: buffGeom,
                  symbol: {
                    type: "simple-fill",
                    color: [112, 128, 144, 0],
                  },
                });
              });

 

Charan_0-1701980882235.png

 

 

0 Kudos
1 Solution

Accepted Solutions
JeffreyThompson2
MVP Regular Contributor

I think I have found the problem. Your points are an array and the distance is a single value. That is not allowed.

The buffer input geometry. The geometry and distance parameters must be specified as either both arrays or both non-arrays. Never specify one as an array and the other a non-array.

distance Number|Number[]

The specified distance(s) for buffering. The geometry and distance parameters must be specified as either both arrays or both non-arrays. Never specify one as an array and the other a non-array. When using an array of geometries as input, the length of the geometry array does not have to equal the length of the distance array. For example, if you pass an array of four geometries: [g1, g2, g3, g4] and an array with one distance: [d1], all four geometries will be buffered by the single distance value. If instead you use an array of three distances: [d1, d2, d3], g1 will be buffered by d1, g2 by d2, and g3 and g4 will both be buffered by d3. The value of the geometry array will be matched one to one with those in the distance array until the final value of the distance array is reached, in which case that value will be applied to the remaining geometries.

GIS Developer
City of Arlington, Texas

View solution in original post

0 Kudos
7 Replies
JeffreyThompson2
MVP Regular Contributor

I think you need to explicitly load the projection. Try wrapping projection.load() around your call to the BufferGeom function. Something like this:

projection.load().then(() => {
 BufferGeom(address_geo)
})

You will also need to load projection into your imports if you haven't already.

https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-projection.html

GIS Developer
City of Arlington, Texas
0 Kudos
Charan
by
New Contributor III

Hi @JeffreyThompson2 I added the above code and it is still giving me the same error

 

0 Kudos
JeffreyThompson2
MVP Regular Contributor

What projection are you working in? geodesicBuffer only works in WGS84 or Web Mercator.

This method only works with WGS84 (wkid: 4326) and Web Mercator spatial references. In general, if your input geometries are assigned one of those two spatial references, you should always use geodesicBuffer() to obtain the most accurate results for those geometries. If needing to buffer points assigned a projected coordinate system other than Web Mercator, use buffer() instead. If the input geometries have a geographic coordinate system other than WGS84 (wkid: 4326), use geometryService.buffer().

https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-geometryEngine.html#geod...

GIS Developer
City of Arlington, Texas
0 Kudos
Charan
by
New Contributor III

I used WKID 4326 as the spatial reference for the point as well as during projection.load()

0 Kudos
JeffreyThompson2
MVP Regular Contributor

Calling projection.project() that way will re-project the points in address_geo, not the basemap.

Have you console logged geo_points and d.value? Are they properly defined?

GIS Developer
City of Arlington, Texas
0 Kudos
Charan
by
New Contributor III

When i do console.log(d.value) I get a number and console.log(geo_points) i get:

Charan_0-1701985812361.png

 

 

0 Kudos
JeffreyThompson2
MVP Regular Contributor

I think I have found the problem. Your points are an array and the distance is a single value. That is not allowed.

The buffer input geometry. The geometry and distance parameters must be specified as either both arrays or both non-arrays. Never specify one as an array and the other a non-array.

distance Number|Number[]

The specified distance(s) for buffering. The geometry and distance parameters must be specified as either both arrays or both non-arrays. Never specify one as an array and the other a non-array. When using an array of geometries as input, the length of the geometry array does not have to equal the length of the distance array. For example, if you pass an array of four geometries: [g1, g2, g3, g4] and an array with one distance: [d1], all four geometries will be buffered by the single distance value. If instead you use an array of three distances: [d1, d2, d3], g1 will be buffered by d1, g2 by d2, and g3 and g4 will both be buffered by d3. The value of the geometry array will be matched one to one with those in the distance array until the final value of the distance array is reached, in which case that value will be applied to the remaining geometries.

GIS Developer
City of Arlington, Texas
0 Kudos