Geocode CSV in JavaScript

5805
12
Jump to solution
09-05-2014 07:43 AM
HeathAnderson
Occasional Contributor II

I am trying to geocode a list of addresses from a CSV.  I know that there is a drag and drop option that works well if you have lat long, but I only have addresses.  I want my coworkers to be able to save an xls as a csv then in the JS app browse to the CSV and run the gp model or drag and drop the csv into the map and kickoff the gp tool.  The service is upload enabled, but I am struggling getting the correct code down.  Has anyone done this before where they can point me in the correct direction.

Cheers,

Heath

0 Kudos
12 Replies
LangdonSanders2
Occasional Contributor

Thank you Paul! This is incredibly helpful.  I appreciate you posting your code and lightweight use of Leaflet.

0 Kudos
ChrisSmith7
Frequent Contributor

One thing though - since we're talking about batch processing - this method looks like it will parse the CSV and make a geocoding request on each address... not batch per se. There is a way to make a batch request - we use our own locator and generated an ArcGIS geocoding service to geocode in batch, e.g.:

{"records":[{"attributes":{"UID": 33549,"STREET": "123 Main Street","CITY": "Beverly Hills","STATE":"CA","ZIP":"90210",}},
{"attributes":{"UID": 30520,"STREET": "321 Main Street","CITY": "Beverly Hills","STATE":"CA","ZIP":"90210",}},
{"attributes":{"UID": 8391,"STREET": "987 2nd Street","CITY": "Beverly Hills","STATE":"CA","ZIP":"90210",}},
{"attributes":{"UID": 33512,"STREET": "1001 1st Street","CITY": "Beverly Hills","STATE":"CA","ZIP":"90210",}}]}

Which generate a response like this (note - using a fake data and fake response):

{
"spatialReference": {
  "wkid": 4326,
  "latestWkid": 4326
},
"locations": [
  {
  "address": "",
  "location": {
  "x": "NaN",
  "y": "NaN"
  },
  "score": 0,
  "attributes": {
  "ResultID": -1,
  "Status": "U",
  "Score": 0,
  "Match_addr": "",
  "Side": "",
  "AddNum": "",
  "AddNumFrom": "",
  "AddNumTo": "",
  "StPreDir": "",
  "StPreType": "",
  "StName": "",
  "StType": "",
  "StDir": "",
  "Nbrhd": "",
  "City": "",
  "Subregion": "",
  "Region": "",
  "Postal": "",
  "PostalExt": "",
  "Country": "",
  "LangCode": "",
  "Addr_type": "",
  "StreetID": "",
  "Distance": 0,
  "X": 0,
  "Y": 0,
  "DisplayX": 0,
  "DisplayY": 0,
  "Xmin": 0,
  "Ymin": 0,
  "Xmax": 0,
  "Ymax": 0
  }
  },
  {
  "address": "",
  "location": {
  "x": "NaN",
  "y": "NaN"
  },
  "score": 0,
  "attributes": {
  "ResultID": -1,
  "Status": "U",
  "Score": 0,
  "Match_addr": "",
  "Side": "",
  "AddNum": "",
  "AddNumFrom": "",
  "AddNumTo": "",
  "StPreDir": "",
  "StPreType": "",
  "StName": "",
  "StType": "",
  "StDir": "",
  "Nbrhd": "",
  "City": "",
  "Subregion": "",
  "Region": "",
  "Postal": "",
  "PostalExt": "",
  "Country": "",
  "LangCode": "",
  "Addr_type": "",
  "StreetID": "",
  "Distance": 0,
  "X": 0,
  "Y": 0,
  "DisplayX": 0,
  "DisplayY": 0,
  "Xmin": 0,
  "Ymin": 0,
  "Xmax": 0,
  "Ymax": 0
  }
  },
  {
  "address": "",
  "location": {
  "x": "NaN",
  "y": "NaN"
  },
  "score": 0,
  "attributes": {
  "ResultID": -1,
  "Status": "U",
  "Score": 0,
  "Match_addr": "",
  "Side": "",
  "AddNum": "",
  "AddNumFrom": "",
  "AddNumTo": "",
  "StPreDir": "",
  "StPreType": "",
  "StName": "",
  "StType": "",
  "StDir": "",
  "Nbrhd": "",
  "City": "",
  "Subregion": "",
  "Region": "",
  "Postal": "",
  "PostalExt": "",
  "Country": "",
  "LangCode": "",
  "Addr_type": "",
  "StreetID": "",
  "Distance": 0,
  "X": 0,
  "Y": 0,
  "DisplayX": 0,
  "DisplayY": 0,
  "Xmin": 0,
  "Ymin": 0,
  "Xmax": 0,
  "Ymax": 0
  }
  },
  {
  "address": "",
  "location": {
  "x": "NaN",
  "y": "NaN"
  },
  "score": 0,
  "attributes": {
  "ResultID": -1,
  "Status": "U",
  "Score": 0,
  "Match_addr": "",
  "Side": "",
  "AddNum": "",
  "AddNumFrom": "",
  "AddNumTo": "",
  "StPreDir": "",
  "StPreType": "",
  "StName": "",
  "StType": "",
  "StDir": "",
  "Nbrhd": "",
  "City": "",
  "Subregion": "",
  "Region": "",
  "Postal": "",
  "PostalExt": "",
  "Country": "",
  "LangCode": "",
  "Addr_type": "",
  "StreetID": "",
  "Distance": 0,
  "X": 0,
  "Y": 0,
  "DisplayX": 0,
  "DisplayY": 0,
  "Xmin": 0,
  "Ymin": 0,
  "Xmax": 0,
  "Ymax": 0
  }
  }
]
}

You could then parse the JSON and grab the x/y coords and plot. I don't have any code samples readily available, though! Just wanted to give some other ideas... To make the request, you would need to create a JSON object from the CSV and post to the server. The advantage to this would be that you're making one request for the entire file instead of one request per address.

LangdonSanders2
Occasional Contributor

Right, I saw that it uses the findAddressCandidates method while looping over each candidate.  I wonder why it Mr. Crickard chose this instead of the method you described. The response seems fairly snappy for the small sample set I have.

I intended on adding a results passed counter and other plugins like heatmap display.

How do you handle the file upload process to get your initial candidate list? (This is before it is converted to JSON or sent via the url request as shown above).

0 Kudos