Ejemplo n.º 1
0
  query: function(cartoQuery, cartoTemplate, layerNumber, layerIndex, cartocss, layerName, cartoLayers, resolve, reject) {
    // cartoQuery += ' WHERE cartodb_id < 10000';
    var _url = urls.cartoDataEndpoint(this.cartoUser, cartoQuery, this.cartoApiKey);
    // var esriJsonLayer = [];
    // request.id = 2;

    request(_url, {timeout: 30000}).then((data) => {
      const parsedData = JSON.parse(data);
      const geometryObjects = {...parsedData.objects};
      parsedData.objects = {
        geometries: Object.entries(geometryObjects).map(entry => entry[1]),
        type: 'GeometryCollection'
      };
      var geojson = topojson.feature(parsedData, parsedData.objects);
      // const meta = this.setMetadataFields(geojson.features[0].properties, layerNumber);
      const { esriObj, esriObjLineSymbol } = this.processCartoCSS(cartocss, geojson.features[0].geometry.type);
      this.setParameters(geojson.features[0].geometry.type, esriObj, esriObjLineSymbol);
      const esriJson = geojsonUtil.geojsonToArcGIS(geojson);

      esriJson.forEach(feature => {
        if (feature.geometry) {
          var graphic = new Graphic(feature);
          //project geometry to web mercator if needed

          if (graphic.geometry.spatialReference.wkid === 4326){
            graphic.setGeometry(
              webMercatorUtils.geographicToWebMercator(graphic.geometry)
            );
          }

          // Set the symbolDictionary depending on the geometry type
          if (!!this.symbolDictionary) {
            graphic.setSymbol(
              this.symbolDictionary[graphic.geometry.type]
            );
          }
          else {
            return this.emit('queryDrawError', {
              message: 'No symbolDictionary for feature'
            });
          }

          // esriJsonLayer.push(graphic);
        }
      });

      this.addLayer(esriJsonLayer, cartoTemplate, meta);
      const dataInfo = {symbol: this.symbolDictionary, cartoTemplate: cartoTemplate};
      resolve(dataInfo);

    }, () => {
      cartoLayers.forEach((layer, index) => {
        if(layer.id === layerName) {
          delete cartoLayers[index];
        }
      });
      const tempResources = resources;
      tempResources.layerPanel.GROUP_CARTO.layers = cartoLayers;
      this.cartoLayers = cartoLayers;
      this.loaded = true;
      this.emit('onCartoLayerAdd');
      resolve('err');
    });
  },
Ejemplo n.º 2
0
  getLayers: function() {

    // Getting the Carto template url
    const _url = urls.cartoTemplateEndpoint(this.cartoUser, this.cartoTemplateId, this.cartoApiKey);
    let json = {};

    // Making a call to get the Carto template
    request(_url).then((template) => {
      json = dojoJSON.parse(template);
      console.log(JSON.parse(template));
      const layers = json.template.layergroup.layers;
      const cartoMapID = json.template.layergroup.stat_tag;
      const cartoLayers = resources.layerPanel.GROUP_CARTO.layers;

      this.getLayerName(cartoLayers[0], cartoMapID).then(response => {
        const all = [];
        layers.forEach((layer, i) => {
          const promise = new Promise((resolve, reject) => {
            const cartoCSS = layer.options.cartocss;
            if(cartoCSS === undefined) {
              resolve('err');
              return;
            }
            const cartoTemplate = 'CARTO_TEMPLATE' + i;
            // Getting the query out of the original carto returned query
            const cartoQuery = layer.options.sql.match(/\((.*?)\)/)[1];
            // Querying carto to get the geojson layer
            cartoLayers.push({
              order: i + 1,
              id: cartoTemplate,
              type: 'carto',
              url: 'cartoLayer',
              // symbolDic: symbolDic,
              label: {
                en: response.layerNames[i - 1],
                fr: response.layerNames[i - 1],
                es: response.layerNames[i - 1],
                pt: response.layerNames[i - 1],
                id: response.layerNames[i - 1],
                zh: response.layerNames[i - 1]
              },
              sublabel: {
                en: '(carto_layer)',
                fr: '(carto_layer)',
                es: '(carto_layer)',
                pt: '(carto_layer)',
                id: '(carto_layer)',
                zh: '(carto_layer)'
              },
              popup: {
                title: {
                  en: response.layerNames[i - 1]
                },
                content: {
                  en: []
                }
              }
            });
            this.query(cartoQuery, cartoTemplate, i - 1, i, cartoCSS, cartoTemplate, cartoLayers, resolve, reject);
          });
          all.push(promise);
        });
        Promise.all(all).then((responses) => {
          responses.forEach((data) => {
            cartoLayers.forEach((layer) => {
              if(data.cartoTemplate === layer.id) {
                layer.symbol = data.symbol;
              }
            });
          });
          layerActions.updateCartoSymbol.defer(cartoLayers);
        });
        // Removing the first carto layer as it is the template
        cartoLayers.shift();
        const tempResources = resources;
        tempResources.layerPanel.GROUP_CARTO.layers = cartoLayers;
        this.cartoLayers = cartoLayers;
        this.loaded = true;
        this.emit('onCartoLayerAdd');
      });
    });
  },
Ejemplo n.º 3
0
 getItemInfo: appid => {
   return esriRequest(urls.itemInfo(appid), {
     responseType: 'json'
   });
 }