1

I have problems with a get call for populate an empty geoJson. My purpose is the following:

I want to create a geometry. (Working)

Write into carto database. (Working)

Get the recent geometry and put it in a empty geojson.

Populate another geojson with the data recently got it.

What it happens is not all the reponses of the get call are data, some are empty. Then it cannot be written in the geojson. Others the get call comes with data but is not recognized o something similar.

At the first call the reply can come empty, until third or fourth attempt, when the call comes as expected, with data, but still the geometry is not written in the geojson. Is in the following atempt of geometry record when it starts to create the previous one. And then, sometimes the response can come empty.

//Function to get data to Carto
function cogeDatos(usuarioCarto, sql, geojsonLayer){
    $.getJSON("https://"+usuarioCarto+".carto.com/api/v2/sql?format=GEOJSON&q="+sql+"", function(data) {geojsonLayer.addData(data)});
}

escribeCarto(sqlN); //Function that writes the record in the carto Data Base is working nicely, so no need to show.
var geojsonTemp = geoJsonTemplate; //Temporal GeoJson based on an geojson object                                                               
var sqlTemp = "SELECT cartodb_id,the_geom, name from suc_zgz where name='"+nomCab+"';";//SQL query to get again the recently record
cogeDatos(userCarto, sqlTemp, geojsonTemp);//getting the new row
var nFeat = geojsonTemp.getLayers().length;//Finding the amount of new geometries
console.log('Numero elementos a introducir '+nFeat);
geojsonTemp.eachLayer(function (layer) {                                                                     
                                        console.log(layer.feature.properties.name);//Trying to find the value of the new Data added to the temp geoJson
                                       });
geojsonSuc.addData(geojsonTemp.toGeoJSON());//adding the data to a previous geoJson layer.

The problem really comes with the get function cogeDatos, I don't know if the problem comes because of me, it's a kind of bug of Carto, jQuery.

EDIT: You can see the whole code working here. Now, some responses are randomly empty. So I can not add any geometry to the geojson when this happens. I have updated the fiddle, now less code it's on it. I hope all the problems are repaired.

1

The easiest way to get data and populate a JSON with CARTO is using cartodb.SQL method. As explained in the documentation, it is a really powerful technique for returning things like items closest to a point, items ordered by date, or GeoJSON vector geometries:

var sql = new cartodb.SQL({ user: 'cartodb_user' });
sql.execute("SELECT * FROM table_name WHERE id > {{id}}", { id: 3 })
  .done(function(data) {
    console.log(data.rows);
  })
  .error(function(errors) {
    // errors contains a list of errors
    console.log("errors:" + errors);
  })

Here you have a working example explaining how to populate both a GeoJSON (and then paint it with Leaflet) and a JSON to store some value fields.

  • Hi!! Thanks for your reply! The code works fine, just sometimes the reply is empty. The response is this: {"type": "FeatureCollection", "features": [ ]}. So when it comes empty, none geometry is added to the geojson. What can I do? – Luismi Jan 3 '17 at 12:04
  • Please update your question with a fiddle with your whole code. In doing so, we can detect what is causing the problem you have pointed out in the last comment. – ramiroaznar Jan 3 '17 at 16:05
  • Done!! Question edited and fiddle reproducing the error. – Luismi Jan 4 '17 at 18:58
  • The error says L.Control.Draw is not a constructor, so it is related to Leaflet.draw method. You have a lot of code to try to debug easily, so I recommend to start from the ground up and check when it starts failing. Or maybe people who master Leaflet can help you out. – ramiroaznar Jan 5 '17 at 10:44
  • Maybe some errors happened with the fiddle, errors that aren't related with my issue. I have updated the fiddle with new code. Please, check it. – Luismi Jan 5 '17 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.