-1

I am trying to set up a csv file with two columns for lat and lng for arcgis. Then for lat, the sheet to generate number between 42,84 and for lng between -142,-52. Basically creating a grid that goes across Canada with the ability to change intervals to change size of the grid squares.

The code I wrote below I think is on the right track, it has a csv file being created, numpy being used for interval and rows being created.

However, I have tried the code and gotten a range of errors in my attempts, from 'too many value to unpack' to just syntax error.

I have the lat and long ranges working, just need to move data into csv using pandas or csv.

import csv
import numpy as np

lat_lng = [(lat,long) for lat,long in zip(np.arange(42,84,0.01),np.arange(-142,-52,0.01))] 

for latitude,longitude in lat_lng:
    print (latitude,longitude)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
John_Muir
  • 119
  • 7
  • It seems you are missing some part of your code? Otherwise try printing `lat` and `long` in your for loop to understand what `range` does – braulio Jul 21 '21 at 19:08
  • `for lat,long in zip(np.arange(42,84,0.01),np.arange(-142,-52,0.01)):` `print(lat, long)` – braulio Jul 21 '21 at 19:09
  • I edited the code just to print but then I get "ValueError: too many values to unpack (expected 2)". Would I have to solve this before I even try to put it into a csv? – John_Muir Jul 21 '21 at 19:12
  • Once you have an array or a list with the lat, long of each point, you could use pandas to wrire a dataframe to a csv file – braulio Jul 21 '21 at 19:13
  • Use `zip()` to loop through the two iterables from `range()` – braulio Jul 21 '21 at 19:15
  • @braulio would the issue with valueError effect how pandas would see it? – John_Muir Jul 21 '21 at 19:15
  • You cannot create a pandas dataframe to write out the csv until you have the data you want to write out – braulio Jul 21 '21 at 19:18
  • So I got a list coming down in two columns, could I now just use pandas? – John_Muir Jul 21 '21 at 19:21
  • Sure, just do something like `pd.DataFrame().to_csv()`, the function `DataFrame()` creates a dataframe (use the help to understand what inputs it takes, clearly you need to pass it the list with values, and a list with column names) then write it out to csv with `to_csv` – braulio Jul 21 '21 at 19:25
  • Let's step back for a moment. You have 4200 elements in your first range and 9000 elements in your second range. `zip` is going to give you (0,0), (1,1), (2,2), (3,3), and so on, until the first list ends. That is, 4,200 elements. Your description makes it sounds like you want a grid of 37,800,000 elements. Numpy can do that, but not the way you're doing it. – Tim Roberts Jul 21 '21 at 19:26
  • Does this answer your question? [Writing a Python list of lists to a csv file](https://stackoverflow.com/questions/14037540/writing-a-python-list-of-lists-to-a-csv-file) – mkrieger1 Jul 21 '21 at 19:26
  • @TimRoberts Yea the goal is have grid that does have a lot of point depending on the interval I am trying to use. What method do you suggest I use instead to great the grid? From what I can tell would array type of numpy be better? – John_Muir Jul 21 '21 at 19:29

1 Answers1

0

This does what you ask. It might be better for you to just use nested "for" loops. You do understand your CSV file will have 37 million records? Canada is a big frickin' country, especially if you're dividing it into 1km grids, as you are doing here.

lat = np.arange(42,84,0.01)
lon = np.arange(-142,-52,0.01)
a,b = np.meshgrid(lat,lon)
coords = np.stack((a.ravel(),b.ravel()),axis=1)

## Write to a CSV file.

mycsv = open('mycsv.csv','w')
for c in coords:
    print( f"{c[0]},{c[1]}", file=mycsv )
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Yea I know it sounds crazy. This is just a test to see how exact I can get with a grid system that I will be transferring into arcgis. For a whole country like Canada its bad if I try to get exact but for places like toronto it almost a need due to how dense the population can get. Also, after looking at it I would probably bring it up to 0.1 or .5 just because this is a test that I somehow got stuck with as a geographer. What method do you suggest for moving it into a excel or csv file? – John_Muir Jul 21 '21 at 19:51
  • Don't make it more complicated than it needs to be. You certainly could use the "csv" module if you want, but I'm not sure it gains you very much. CSV files are easy. – Tim Roberts Jul 21 '21 at 19:59