mapbox / shp-write

create and write to shapefiles in pure javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Polygons not supported

naveenjaiswal opened this issue · comments

For a Geojson file which contains 4 features , it converts eveything as a multipolygon instead of 4 feature each contains polygon. Do i miss any settings or config or its a bug.Please let me know how can i achieve this.

Same problem

Had the same problem. I changed the script to work for my specific requirement - separate shapeFiles for each polygon in a FeatureCollection (geoJson). The shapeFiles are to be named using properties.fileName of each feature in the geoJson.

Change made in code corresponding to zip.js

if(l.type === "POLYGON" && l.properties.length > 1 && l.properties.length == l.geometries[0].length)
          {
            for(var i=0; i<l.properties.length; i++){
              var myProps = [l.properties[i]];
              var myGeoms = [[l.geometries[0][i]]];
              write(
                // field definitions
                myProps,
                // geometry type
                l.type,
                // geometries
                myGeoms,
                function(err, files) {
                    var fileName = myProps[0].fileName;//custom filename in each feature's properties
                    //var fileName = options && options.types[l.type.toLowerCase()] ? options.types[l.type.toLowerCase()] : l.type;
                    layers.file(fileName + '.shp', files.shp.buffer, { binary: true });
                    layers.file(fileName + '.shx', files.shx.buffer, { binary: true });
                    layers.file(fileName + '.dbf', files.dbf.buffer, { binary: true });
                    layers.file(fileName + '.prj', prj);
              });
            }
          }

Another requirement is to get all polygons as individual records in a single shape file.
I made the following changes to get this:

  1. pass l.geometries[0] to the write function (instead of l.geometries).
  2. pass [[coordinates]] to parts function in poly.js( instead of [coordinates] - note two enveloping arrays)

Tested the generated shape files on mapshaper.org. They worked fine without errors.
I'm no expert at this, but if any one is interested, I'd be happy to discuss.

I created a loop to create the object list for features. It works for multiple records (in this case polygons) and it works fine for polygons with donuts

            var objectlist = []
                for (i = 0; i < myFeatureLayer.getSelectedFeatures().length; i++) {
                    var gra = new Graphic()
                    gra = myFeatureLayer.getSelectedFeatures()[i]
                    var geographicGeometry = webMercatorUtils.webMercatorToGeographic(gra.geometry);
                    properties = gra.attributes
                    var poly = new Polygon(geographicGeometry)
                    polyrings = poly.rings
                    var singleobject = {}
                    var singlegeometry = {}
                    singlegeometry['type'] = "Polygon"
                    singlegeometry['coordinates'] = [[[polyrings]]]
                    singleobject ['type'] = 'Feature';
                    singleobject['geometry'] = singlegeometry
                    singleobject['properties'] = properties
                    objectlist.push(singleobject)
                }

                shpwrite.download({
                    type: 'FeatureCollection',
                    features: objectlist
                }, options);

Is there any way to archeive creating a single shapefile containing multiple polygons instead of a single multipolygon? Any help will be appreciated.

I believe #65 will fix your problem. Hoping this will be accepted and released.