I use the pyshp library to retrieve the coordinates of a shape.
sf = shapefile.Reader(r"{}".format(boundary_file))
shapes = sf.shapes()
fields = sf.fields
records = sf.records()
for record in records:
if record['NAME'] in cities_list:
city = record['NAME']
s = sf.shape(record_id)
geom = s.__geo_interface__
geom_list = []
for dict in geom:
if dict == "coordinates":
coord_dict = geom[dict]
coords = coord_dict[0]
for pairs in coords:
geom_list.append(pairs)
Then I need to create a string based on the coordinates from the 'geom' dict. I use the function:
def listToString(s):
# initialize an empty string
str1 = ""
# traverse in the string
for ele in s:
str1 += "'{}',".format(ele)
# return string
return str1
I further process the string to remove any other character except for coordinates.
My problem is there is a newline present that I cannot remove.
When printing print(listToString(geom_list ))
I notice that there is a newline after a particular number of characters
This is how it looks in notepad++.
I would like to remove the newline and print the list of coordinates in one string.
Instead of using the geo_interface, try accessing the geometry directly.
So instead of:
Try:
That will give you a list of python tuples which should be easier to manipulate. For example:
Then extend your geometry list: