Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use Post to download zip shapefile itself? #41

Closed
zhengniu opened this issue Jun 22, 2015 · 5 comments
Closed

how to use Post to download zip shapefile itself? #41

zhengniu opened this issue Jun 22, 2015 · 5 comments

Comments

@zhengniu
Copy link

Thanks for sharing this great tool

I have tried to use Post request in my code to get zipped file download through web client, but no clue how to do it in callback function to fullfil the same ability as the convert button "Convert to Shapefile" does.

Could you please provide newbies some help? Many Thanks

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
var objectcol={"displayFieldName":"","geometryType":"esriGeometryPolygon","spatialReference":{"wkid":102100,"latestWkid":3857},"fields":[{"name":"OID","type":"esriFieldTypeOID","alias":"OID"},{"name":"Name","type":"esriFieldTypeString","alias":"Name","length":60},{"name":"BUFF_DIST","type":"esriFieldTypeDouble","alias":"BUFF_DIST"},{"name":"SUM_CLIPPOP","type":"esriFieldTypeInteger","alias":"SUM_CLIPPOP"},{"name":"SUM_CLIPEMP","type":"esriFieldTypeInteger","alias":"SUM_CLIPEMP"},{"name":"Shape_Length","type":"esriFieldTypeDouble","alias":"Shape_Length"},{"name":"Shape_Area","type":"esriFieldTypeDouble","alias":"Shape_Area"}],"features":[{"geometry":{"type":"polygon","rings":[[[-8841308.9388,5412113.0163],[-8841274.2092,5412109.977899998],[-8841240.5348,5412100.954800002],[-8841208.9388,5412086.2214],[-8841180.3813,5412066.225199997],[-8841155.7299,5412041.573799998],[-8841135.7337,5412013.0163],[-8841121.0003,5411981.420299999],[-8841111.9772,5411947.745899998],[-8841108.9388,5411913.0163],[-8841111.9772,5411878.286700003],[-8841121.0003,5411844.612300001],[-8841135.7337,5411813.0163],[-8841155.7299,5411784.458800003],[-8841180.3813,5411759.807400003],[-8841208.9388,5411739.8112],[-8841240.5348,5411725.077799998],[-8841274.2092,5411716.054700002],[-8841308.9388,5411713.0163],[-8841343.6684,5411716.054700002],[-8841377.3428,5411725.077799998],[-8841408.9388,5411739.8112],[-8841437.4963,5411759.807400003],[-8841462.1477,5411784.458800003],[-8841482.1439,5411813.0163],[-8841496.8773,5411844.612300001],[-8841505.9004,5411878.286700003],[-8841508.9388,5411913.0163],[-8841505.9004,5411947.745899998],[-8841496.8773,5411981.420299999],[-8841482.1439,5412013.0163],[-8841462.1477,5412041.573799998],[-8841437.4963,5412066.225199997],[-8841408.9388,5412086.2214],[-8841377.3428,5412100.954800002],[-8841343.6684,5412109.977899998],[-8841308.9388,5412113.0163]]],"_ring":0,"spatialReference":{"wkid":102100,"latestWkid":3857}},"attributes":{"OID":1,"Name":null,"BUFF_DIST":200,"SUM_CLIPPOP":2,"SUM_CLIPEMP":null,"Shape_Length":1255.0427251988326,"Shape_Area":125026.69382391222}},{"geometry":{"type":"polygon","rings":[[[-8840769.1023,5412624.188900001],[-8840734.3727,5412621.1504999995],[-8840700.6983,5412612.127400003],[-8840669.1023,5412597.394000001],[-8840640.5448,5412577.3977999985],[-8840615.8934,5412552.746399999],[-8840595.8972,5412524.188900001],[-8840581.1638,5412492.5929000005],[-8840572.1407,5412458.918499999],[-8840569.1023,5412424.188900001],[-8840572.1407,5412389.4592999965],[-8840581.1638,5412355.784900002],[-8840595.8972,5412324.188900001],[-8840615.8934,5412295.631399997],[-8840640.5448,5412270.979999997],[-8840669.1023,5412250.983800001],[-8840700.6983,5412236.250399999],[-8840734.3727,5412227.227300003],[-8840769.1023,5412224.188900001],[-8840803.8319,5412227.227300003],[-8840837.5063,5412236.250399999],[-8840869.1023,5412250.983800001],[-8840897.6598,5412270.979999997],[-8840922.3112,5412295.631399997],[-8840942.3074,5412324.188900001],[-8840957.0408,5412355.784900002],[-8840966.0639,5412389.4592999965],[-8840969.1023,5412424.188900001],[-8840966.0639,5412458.918499999],[-8840957.0408,5412492.5929000005],[-8840942.3074,5412524.188900001],[-8840922.3112,5412552.746399999],[-8840897.6598,5412577.3977999985],[-8840869.1023,5412597.394000001],[-8840837.5063,5412612.127400003],[-8840803.8319,5412621.1504999995],[-8840769.1023,5412624.188900001]]],"_ring":0,"spatialReference":{"wkid":102100,"latestWkid":3857}},"attributes":{"OID":2,"Name":null,"BUFF_DIST":200,"SUM_CLIPPOP":401,"SUM_CLIPEMP":103,"Shape_Length":1255.0427252029426,"Shape_Area":125026.69382473118}},{"geometry":{"type":"polygon","rings":[[[-8840644.8921,5411926.700999998],[-8840610.1625,5411923.662600003],[-8840576.4881,5411914.6395],[-8840544.8921,5411899.906099997],[-8840516.3346,5411879.909900002],[-8840491.6832,5411855.258500002],[-8840471.687,5411826.700999998],[-8840456.9536,5411795.104999997],[-8840447.9305,5411761.430600002],[-8840444.8921,5411726.700999998],[-8840447.9305,5411691.9714],[-8840456.9536,5411658.296999998],[-8840471.687,5411626.700999998],[-8840491.6832,5411598.1435],[-8840516.3346,5411573.4921],[-8840544.8921,5411553.495899998],[-8840576.4881,5411538.762500003],[-8840610.1625,5411529.739399999],[-8840644.8921,5411526.700999998],[-8840679.6217,5411529.739399999],[-8840713.2961,5411538.762500003],[-8840744.8921,5411553.495899998],[-8840773.4496,5411573.4921],[-8840798.101,5411598.1435],[-8840818.0972,5411626.700999998],[-8840832.8306,5411658.296999998],[-8840841.8537,5411691.9714],[-8840844.8921,5411726.700999998],[-8840841.8537,5411761.430600002],[-8840832.8306,5411795.104999997],[-8840818.0972,5411826.700999998],[-8840798.101,5411855.258500002],[-8840773.4496,5411879.909900002],[-8840744.8921,5411899.906099997],[-8840713.2961,5411914.6395],[-8840679.6217,5411923.662600003],[-8840644.8921,5411926.700999998]]],"_ring":0,"spatialReference":{"wkid":102100,"latestWkid":3857}},"attributes":{"OID":3,"Name":null,"BUFF_DIST":200,"SUM_CLIPPOP":200,"SUM_CLIPEMP":65,"Shape_Length":1255.04272520306,"Shape_Area":125026.69382475453}},{"geometry":{"type":"polygon","rings":[[[-8838528.5419,5411850.263999999],[-8838493.8123,5411847.225599997],[-8838460.1379,5411838.202500001],[-8838428.5419,5411823.4690999985],[-8838399.9844,5411803.472900003],[-8838375.333,5411778.821500003],[-8838355.3368,5411750.263999999],[-8838340.6034,5411718.667999998],[-8838331.5803,5411684.993600003],[-8838328.5419,5411650.263999999],[-8838331.5803,5411615.534400001],[-8838340.6034,5411581.859999999],[-8838355.3368,5411550.263999999],[-8838375.333,5411521.706500001],[-8838399.9844,5411497.055100001],[-8838428.5419,5411477.058899999],[-8838460.1379,5411462.325499997],[-8838493.8123,5411453.3024],[-8838528.5419,5411450.263999999],[-8838563.2715,5411453.3024],[-8838596.9459,5411462.325499997],[-8838628.5419,5411477.058899999],[-8838657.0994,5411497.055100001],[-8838681.7508,5411521.706500001],[-8838701.747,5411550.263999999],[-8838716.4804,5411581.859999999],[-8838725.5035,5411615.534400001],[-8838728.5419,5411650.263999999],[-8838725.5035,5411684.993600003],[-8838716.4804,5411718.667999998],[-8838701.747,5411750.263999999],[-8838681.7508,5411778.821500003],[-8838657.0994,5411803.472900003],[-8838628.5419,5411823.4690999985],[-8838596.9459,5411838.202500001],[-8838563.2715,5411847.225599997],[-8838528.5419,5411850.263999999]]],"_ring":0,"spatialReference":{"wkid":102100,"latestWkid":3857}},"attributes":{"OID":4,"Name":null,"BUFF_DIST":200,"SUM_CLIPPOP":22,"SUM_CLIPEMP":37,"Shape_Length":1255.0427252029426,"Shape_Area":125026.69382473118}},{"geometry":{"type":"polygon","rings":[[[-8840687.888,5411573.179799996],[-8840653.1584,5411570.141400002],[-8840619.484,5411561.118299998],[-8840587.888,5411546.384900004],[-8840559.3305,5411526.388700001],[-8840534.6791,5411501.737300001],[-8840514.6829,5411473.179799996],[-8840499.9495,5411441.583800003],[-8840490.9264,5411407.909400001],[-8840487.888,5411373.179799996],[-8840490.9264,5411338.450199999],[-8840499.9495,5411304.775799997],[-8840514.6829,5411273.179799996],[-8840534.6791,5411244.622299999],[-8840559.3305,5411219.970899999],[-8840587.888,5411199.974699996],[-8840619.484,5411185.241300002],[-8840653.1584,5411176.218199998],[-8840687.888,5411173.179799996],[-8840722.6176,5411176.218199998],[-8840756.292,5411185.241300002],[-8840787.888,5411199.974699996],[-8840816.4455,5411219.970899999],[-8840841.0969,5411244.622299999],[-8840861.0931,5411273.179799996],[-8840875.8265,5411304.775799997],[-8840884.8496,5411338.450199999],[-8840887.888,5411373.179799996],[-8840884.8496,5411407.909400001],[-8840875.8265,5411441.583800003],[-8840861.0931,5411473.179799996],[-8840841.0969,5411501.737300001],[-8840816.4455,5411526.388700001],[-8840787.888,5411546.384900004],[-8840756.292,5411561.118299998],[-8840722.6176,5411570.141400002],[-8840687.888,5411573.179799996]]],"_ring":0,"spatialReference":{"wkid":102100,"latestWkid":3857}},"attributes":{"OID":5,"Name":null,"BUFF_DIST":200,"SUM_CLIPPOP":183,"SUM_CLIPEMP":59,"Shape_Length":1255.0427252061977,"Shape_Area":125026.69382537971}}],"exceededTransferLimit":false} ;

function Esri2geo(data){
    console.log("json converted to object");
    console.log("begin parsing json");
    var geomType,
    originCRS={
        "type": "EPSG",
        "properties": {
            "code": 3857
        }
    };
    function parseGeometryType(type){
        if (type === "esriGeometryPoint"){
            return "Point";
        }
        else if (type === "esriGeometryMultipoint") {
            return "MultiPoint";
        }
        else if (type === "esriGeometryPolyline") {
            return "LineString";
        } 
        else if (type === "esriGeometryPolygon") {
            return "Polygon";
        }
        else{
            return "Empty";
        }

    }
    function featureToGeo (feature_in,geomType){
        var geometry = {};
        geometry.type = geomType;
        // grab the rings to coordinates
        var geom = feature_in.geometry;
        var coordinates;
        if (geomType === "Polygon") {
            coordinates = geom.rings;
        } else if (geomType === "LineString") {
            coordinates = geom.paths;
        } else if (geomType === "Point") {
            coordinates = [geom.x, geom.y];
        }

        geometry.coordinates = coordinates;     
        // convert attributes to properties
        var properties = {};
        var attr = feature_in.attributes;
        for (var field in attr) {
            properties[field] = attr[field];
        }       
        var feature_out = {};
        feature_out.type = "Feature";
        feature_out.geometry = geometry;
        feature_out.properties = properties;
        return feature_out;
    }
    var result;

    geomType=parseGeometryType(data.geometryType);
    console.log(geomType);
    console.log(data.features);
    var features = [];
    for (var i = 0, feature = {}; feature = data.features[i]; i++) {
        // prepare the main parts of the GeoJSON
        var feat = featureToGeo(feature, geomType);
        features.push(feat);
    }
    var featColl = {};
    featColl.type = "FeatureCollection";
    featColl.crs= originCRS;
    featColl.features = features;
    result = JSON.stringify(featColl, function(key, value) {
        if (typeof value === 'number' && !isFinite(value)){
            return String(value);
        }
        return value;
    });
    console.log("json parsed, return it");
    //console.log(result);
    return result;
}
//var data= objectcol;
var GeoJson=Esri2geo(objectcol);
//console.log(typeof(GeoJson));
console.log(GeoJson);
$.post("http://ogre.adc4gis.com/convertJson",
    {json:GeoJson, 
    outputName:"zippedshp"
    },
    function(result){
        console.info(result);

    });
@wavded wavded self-assigned this Jun 22, 2015
@zhengniu
Copy link
Author

Hi Wavded,

I try another way to create Post request like below, but it can not find the path of zip file,

var parameters ={
json:GeoJson,
outputName:"zippedshp.zip"
}
var options ={
url:"http://ogre.adc4gis.com/convertJson",
type: "POST",
data: parameters,
contentype:"application/zip",
success:function(data){
if(data){
console.log(data.path);
if(data.path) {
//Create an hidden iframe, with the 'src' attribute set to the created ZIP file.
var dlif = $('<iframe/>',{'src':data.path}).hide();
//Append the iFrame to the context
this.append(dlif);
}
else if (data.error){
alert (data.error);
}
else{
alert ("Somthing wrong");
}
}
}

}
$.ajax(options);

@zhengniu
Copy link
Author

hi Waveded,

I find one solution can download the zipped shapefile through Post request , as shown below. however, the download zip file can not be used due to : Windows cannot open the folder. The compressed (zipped) Folder 'C:\Users\niuzh\Downloads\zippedshp(6).zip' is invalid.

Thanks for your comments.

var params ={
    json:GeoJson, 
    outputName:"zippedshp.zip"
}

$.ajax({
    url:'http://ogre.adc4gis.com/convertJson',
    type: "POST",
    data: params,
    success: function(response, status, xhr) {
        var filename = "zippedshp";
        var type = xhr.getResponseHeader('Content-Type');
        var blob = new Blob([response], { type: type });

        if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(blob, filename);
        }
        else {
            var URL = window.URL || window.webkitURL;
            var downloadUrl = URL.createObjectURL(blob);
            //console.log(downloadUrl);
            if (filename){
                var a = document.createElement("a");
                if (typeof a.download === 'undefined') {
                    window.location = downloadUrl;
                }
                else {
                    a.href = downloadUrl;
                    a.download = filename;
                    document.body.appendChild(a);
                    a.click();
                }
            }
            else {
                window.location = downloadUrl;
            }
            setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100);
        }
    }
});

@wavded
Copy link
Owner

wavded commented Jun 23, 2015

I am not up to speed on latest browser file handling but my current understanding is that you cannot trigger a download from an AJAX request. I believe you want to create a hidden form and submit that to get the download behavior.

@wavded wavded removed their assignment Jun 23, 2015
@zhengniu
Copy link
Author

Thank you so much. it is working well.

is it possible to host the similar service locally?

@wavded
Copy link
Owner

wavded commented Jun 24, 2015

yes, the readme has instructions, there also is a Dockerfile included in the project if you want to build it that way.

@wavded wavded closed this as completed Jun 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants