How do I only select .shp's to read and then convert to .geojson. The homefolder has 3 different shapefiles and this code currently converts every single file in the folder to a .json. Just trying to speed up the running process. I have some other issues too with naming conventions that I will deal with next. Among other things, this is a work in progress.
homefolder = "D:/UW/Winter_2016/501/pythonProject/publicLandsData/newShapefiles/"
# read the shapefiles
filenames = os.listdir(homefolder)
for filename in filenames:
reader = shapefile.Reader(homefolder + os.path.splitext(os.path.basename(filename))[0])
print filename
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
print filename + "buffer dictionary done"
for sr in reader.shapeRecords():
atr = dict(zip(field_names, sr.record))
geom = sr.shape.__geo_interface__
buffer.append(dict(type="Feature", \
geometry=geom, properties=atr))
# write the GeoJSON file
#currently it saves into the wrong directory..and all one shapefile, might be nice to be in multiple geojson's
#list directory "homefolder" in variable filenames
filenames = os.listdir(homefolder)
for filename in filenames:
#for loop to rename local government to loca
geojson = open(((os.path.splitext(os.path.basename(filename))[0]) + ".json"), "w")
#os.rename(os.path.join(homefolder, filename), os.path.join(homefolder, (filename.replace('.shp', 'shp'))))
geojson.write(dumps({"type": "FeatureCollection",\
" features": buffer}, indent=2) + "\n")
geojson.close()
I have tried using basename, selecting only ".shp" files with .endwith() etc to no avail. Suggestions?
I found this shp to geojson conversion code at the link below: https://gist.github.com/frankrowe/6071443