def generatePolygon(self): # Iniciando la escritura del nuevo shapefile shp = shapefile.Writer(shapefile.POLYGON) # Agregando coordenadas shp.poly(parts=self.coord) # Creando campos para asignar el codigo ingresado shp.field('OBJECTID', 'N', decimal=0) shp.field('CODIGOU', 'C', '40') shp.field('CONCESION', 'C', '50') shp.field('TIPO_RENUN', 'C', '40') shp.field('HAS', 'N', decimal=4) # Registrando el codigo dentro del campo shp.record(1, code, concession, resignation, None) # Almacenando shapefile segun parametro 'output' shp.save(output) shp = shapefile.Reader(output) temp = shp.shape(0) geojson = temp.__geo_interface__ from shapely.geometry import shape area = shape(geojson).area / 10000 e = shapefile.Editor(output) self.modify('HAS', area, e) e.save(output) # Eliminando la variable shp del shp # Adignando Sistema de referencia self.getSpatialReference()
def job(): print "in job" running_time = str(get_date()) PATH = DATAPATH + "/" + running_time if not os.path.exists(PATH): w = shapefile.Writer(shapefile.POINT) w.field('ID', 'C', '30') #Create fields w.field('created_time', 'C', '100') w.field('Chinese', 'C', '200') w.field('City', 'C', '40') w.field('Distance_mile', 'C', '140') w.field('Distance_meter', 'C', '140') w.field('Date_Hour', 'C', '140') w.field('Hour', 'C', '140') run_crawler(running_time, w) w.save(PATH + '/' + running_time) else: e = shapefile.Editor(shapefile=PATH + '/' + running_time + ".shp") run_crawler_edit(running_time, e) e.save(PATH + '/' + running_time) return
def main(input, output, threshold): input_shape = shapefile.Editor(input) length = len(input_shape.shapes()) # print(len(input_shape._shapes[0].points)) flag = 0 # for i in range(length-1): # if len(input_shape._shapes[flag].points)>500: # flag=flag+1 # else: # input_shape.delete(flag) for i in range(length - 1): if len(input_shape._shapes[flag].points) > len( input_shape._shapes[i + 1].points): flag = flag else: flag = i + 1 shapely = input_shape.shapes()[flag] print(len(shapely.points)) output_shapely = shape_from_pyshp_to_shapely(shapely, threshold) output_shape = shapefile.Writer() output_shape.shapeType = input_shape.shapeType output_shape.field('DN', fieldType="N", size="9") output_shape._shapes.append(output_shapely) rec = [1] output_shape.records.append(rec) input_prj = os.path.splitext(input)[0] + ".prj" output_prj = os.path.splitext(output)[0] + ".prj" os.system('cp ' + input_prj + ' ' + output_prj) output_shape.save(output)
def parse(lng, lat, fid, src, tit, desc, month, hour, tags): print(lng, lat, fid, src, tit, desc, month, hour, tags) e = shapefile.Editor(shapefile=flickr2shapeFile) e.point(lng, lat, 0, 0) e.record(fid, src, tit, desc, month, hour, tags) e.save(flickr2shapeFile) return
def parse(lng, lat, fid, source, title, time): global nFotos nFotos+=1 e= shapefile.Editor(shapefile=files) e.point(lng,lat,0,0) e.record(fid,title,time,lat,lng,source) e.save(files) #print (fid,title,time,lat,lng,source) return
def main(input,label,date): input_shape = shapefile.Editor(input) shape_data = input_shape.shapes() error_area=area(shape_data,date) dis=cal_length(label) result=error_area/dis print("%s %f %f %f " % (date,error_area,dis,result))
def append_shapefile(self): w = shapefile.Editor(shape_file) w.poly(parts=self.parts) w.record(self.Mission1,self.ModeBeam1,self.ProductTyp1,self.Date1,self.Pass1,self.Polar1,self.AbOrbit1,self.RelOrbit1,self.Frame1,self.UniqProdID1,self.DatatakeID1,self.ResClass1,self.ProcLevel1,self.ProdClass1,self.StartTime1,self.StopTime1,self.CenLon1,self.CenLat1,self.UL_Lon1,self.UL_Lat1,self.UR_Lon1,self.UR_Lat1,self.LR_Lon1,self.LR_Lat1,self.LL_Lon1,self.LL_Lat1,self.GridDir1,self.ZipFile1) w.save(shape_file) w = None os.chdir(temp_dir) shutil.rmtree(self.zip_dir) os.chdir(proj_dir)
def findBoundingBox(self, currSurfCoordinates, sID): """ Using Library from: https://pypi.python.org/pypi/pyshp From: https://pypi.python.org/pypi/pyshp """ #gbxml standard 3D space: #y is north #z is vertical to the sky #x is east # Single Room Coordiantes for Surface 1: # [(-23.28571, 10.36111, 0.0), (-23.28571, 10.36111, 11.0625), (-5.965204, 20.36111, 11.0625), (-5.965204, 20.36111, 0.0)] w = shapefile.Writer(shapeType=shapefile.POLYGONZ) PT = [] for i in currSurfCoordinates: for j in i: x = float(j[0]) y = float(j[1]) z = float(j[2]) p = [x, y, z] PT.append(p) w.poly([PT], shapeType=15) #w.poly([[[x, y, z], [x, y, z], [x, y, z]]], shapeType=15) #w.autoBalance = 1 # Feature to make sure when you add either a shape or a record the two sides of the equation line up #print "MFPT: ", len(currSurfCoordinates), PT, type(PT).__name__ #, type(PT[1]).__name__, type(PT[1][0]).__name__ w.field("currentSurfaceShapefile") w.record("currentSurfaceShapefile") w.save("shapefiles/currentSurfaceShapefile") #Example: w.poly([[[-89.0, 33, 12], [-90, 31, 11], [-91, 30, 12]]], shapeType=15) # xyz points are stored as xy and then z is seperate #r = shapefile.Reader("shapefiles/test/MyPolyZ") #s = r.shape(0) #s.points outputs >>>[[-89.0, 33.0], [-90.0, 31.0], [-91.0, 30.0], [-89.0, 33.0]] #s.z outputs >>>[12.0, 11.0, 12.0, 12.0] sf = shapefile.Reader("shapefiles/currentSurfaceShapefile.shp") shapes = sf.shapes() bbox = shapes[0].bbox # Retrieves the bounding box of the first shape #print "bbox: ,", bbox # Will print the bounding box coordinates # Remove the last shape in the polygon shapefile. e = shapefile.Editor( shapefile="shapefiles/currentSurfaceShapefile.shp") e.delete(-1) #e.save('shapefiles/currentSurfaceShapefile') e = None return bbox, PT
def cut_files(sourcedir, filenames, destdir): for filename in filenames: input_shapefile_path = os.path.join(os.path.abspath(sourcedir), filename) reader = shapefile.Reader(input_shapefile_path) num_features = len(reader.shapeRecords()) editor = shapefile.Editor(input_shapefile_path) for i in range(0, (num_features / 3) * 2): editor.delete(-1) output_shapefile_path = os.path.join(os.path.abspath(destdir), filename) editor.save(output_shapefile_path)
def populateFieldShapeFile(communeID,targetShapeFile,targetFieldIndex,value): #This function modifies the spatial .shp file. #Adds JSON response info to attributes. #Matches with commune ID at index 6 #INPUTS: Commune ID, Value to Update with. try: logger.info("[Populating shape file with Commune Statistics.]") e = shapefile.Editor(targetShapeFile) for i in range(len(e.records)): if(str(e.records[i][6])==str(communeID)): e.records[i][targetFieldIndex]=value logger.info("[Edited Commune [" + str(communeID) + "] with value [" + str(value) + "]]") e.save(targetShapeFile) except: logger.critical("[Error while populating data to shapefile.] " + sys.exc_info()[0])
def edit_poly(): # checks for duplicates for names in seen: if names == name: return seen.append(name) # adds more shapes to current shapefile e = shapefile.Editor(shapefile="shapefiles/polygon.shp") upleft, upright, downright, downleft = get_quadrat() e.poly(parts=[[[upright[0], upright[1]], [downright[0], downright[1]], [downleft[0], downleft[1]], [upleft[0], upleft[1]]]]) e.record(Quadrat=name, Date=date, Latitude=lat, Longitude=lon, Max_Height=height) # e.record(Latitude=lat, Longitude=lon) e.save('shapefiles/polygon') e = None
def filtershp(shpname,filterlink): sf=shapefile.Reader(shpname) records=sf.records() length=len(records) print "\nThe length of the shp is: "+str(length) fullindex=range(length) for j in range(length-1,-1,-1): for i in filterlink: if records[j][0]==i: fullindex.pop(j) fullindex.sort(reverse=True) print '\nFiltershp finished. Waiting for editing...' w=shapefile.Editor(shpname) for i in fullindex: w.delete(i) w.save('Roadx.shp') print '\nCompleted.'
def parse(tid, lat, lng, time, hour): now = datetime.datetime.now() nowf = now.strftime("%Y%m%d") try: e = shapefile.Editor(shapefile=nowf) e.point(lng, lat, 0, 0) e.record(tid, time, hour) e.save(nowf) except: w = shapefile.Writer(shapeType=1) w.autobalance = 1 w.point(lng, lat, 0, 0) w.field('tid', 'C', '40', 0) w.field('tTime', 'C', '20', 0) w.field('hour', 'C', '10', 0) w.record(tid, time, hour) w.save(nowf) return
def parse(data, types): places=data['results'] global placesParsed for place in places: lat=place['geometry']['location']['lat'] lng=place['geometry']['location']['lng'] name=place['name'] name=name.encode('ascii', 'xmlcharrefreplace') idp=place.get('id') rating=place.get('rating') ref=place.get('reference') e= shapefile.Editor(shapefile=types) e.point(lng,lat,0,0) e.record(idp, name, rating, types, lat, lng, ref) e.save(types) placesParsed+=1 print placesParsed, return
def cleanup_hotspots(city_id): print 'Cleaning up hotspots for', city_id reader = shapefile.Reader(os.path.join(HOTSPOTS_DIR, str(city_id) + '_hotspots.shp')) hotspots = shapefile.Editor(shapefile=os.path.join(HOTSPOTS_DIR, str(city_id) + '_hotspots.shp')) print reader.fields num_deleted = 0 for index, sr in enumerate(reader.shapeRecords()): if float(sr.record[1]) <= 0: print 'Deleting %d' % index try: hotspots.delete(index - num_deleted) except IndexError: print 'Cannot delete %d due to IndexError' % index else: num_deleted += 1 else: print float(sr.record[2]) #hotspots.save(os.path.join(HOTSPOTS_DIR, str(city_id) + '_cleaned.shp')) print 'FINISHED %s!' % city_id
def loadShapefile(self, filepath): """ Attempts to load a shapefile from the filepath -Note: the shapefile lib is extension agnostic, so filepath extension is irrelevant """ # init reader from filepath, init writer self.Reader = shapefile.Reader(filepath) self.Editor = shapefile.Editor(filepath) # get parent directory self.parentDir = os.path.basename(filepath) # Determine best attribute if not self.determineBestAttribute(): raise RuntimeError("determine best attribute failed") print "best attr = ", self.bestAttr # Add unique IDs to shapes if not self.addUniqueShapeIDs(filepath): raise RuntimeError("Unable to add unique shapeIDs")
def main(input, output): input_shape = shapefile.Editor(input) length = len(input_shape.shapes()) for i in range(length - 1): if len(input_shape._shapes[0].points) > 2000: input_shape.delete(-1) else: input_shape.delete(0) shapely = input_shape.shapes()[0] output_shapely = shape_from_pyshp_to_shapely(shapely) output_shape = shapefile.Writer() output_shape.shapeType = input_shape.shapeType output_shape.field('DN', fieldType="N", size="9") output_shape._shapes.append(output_shapely) rec = [1] output_shape.records.append(rec) input_prj = os.path.splitext(input)[0] + ".prj" output_prj = os.path.splitext(output)[0] + ".prj" os.system('cp ' + input_prj + ' ' + output_prj) output_shape.save(output)
w.records.append(recnew) index += 1 exportshp = exportFolder + "Diversity_NYC_" + dataYear w.save(exportshp) prj = open(exportshp + ".prj", "w") epsg = getWKT_PRJ("2263") prj.write(epsg) prj.close() # no parks rm_parks_index = df.loc[((df['PUMA'] == 'park') & (df['Total' + dataYear[-2:]] < 100)) | (df['PUMA'] == 'Airport')].index.values.tolist() df_noparks = df.drop(df.index[rm_parks_index]) e = shapefile.Editor(exportFolder + "tracts") # has only shapes and fields, no records n = 0 for i in rm_parks_index: e.delete(i - n) # for each iteration -1 since not dropping indexes, but nth row n += 1 shape_noparks = shapefile.Reader(e.save()) e.save(exportFolder + 'tracts_noparks') # slight difference in clusters to GeoDa -- centroids are different in GeoDa https://github.com/GeoDaCenter/geoda/issues/1533 W = pysal.queen_from_shapefile(exportFolder + "tracts_noparks" + ".shp") string = ', '.join(str(e) for e in list(df_noparks.columns)) ent_cols = re.findall(r"RE\d\d|CE\d\d|IE\d\d", string) for col in ent_cols: y = np.array(df_noparks[col], dtype=float)
def parse(lng, lat, fid, src, tit): e= shapefile.Editor(shapefile=pano2shapeFile) e.point(lng,lat,0,0) e.record(fid, src, tit, 0, 0, 0) e.save(pano2shapeFile) return
# -*- coding:utf-8 -*- import shapefile # 例1,增加一个点文件 e = shapefile.Editor(shapefile="shp/test.shp") e.point(0, 0, 10, 2) #注意,这里如果没有z值(10,即高度值)和M值(2,即测量值),编辑时可能会出现问题 e.record("Appended", "Point") e.save('shp/test.shp') # 例2,增加一条线 # e = shapefile.Editor(shapefile="shapefiles/test/line.shp") # e.line(parts=[[[10,5],[15,5],[15,1],[13,3],[11,1]]]) # e.record('Appended','Line') # e.save('shapefiles/test/line') # # # 例3,增加一个多边形 # e = shapefile.Editor(shapefile="shapefiles/test/polygon.shp") # e.poly(parts=[[[5.1,5],[9.9,5],[9.9,1],[7.5,3],[5.1,1]]]) # e.record("Appended","Polygon") # e.save('shapefiles/test/polygon') # 例4,删除第一个点 e = shapefile.Editor(shapefile="shp/test.shp") e.delete(2) e.save('shp/test.shp') # 例5,删除最后一个多边形 # e = shapefile.Editor(shapefile="shapefiles/test/polygon.shp") # e.delete(-1) # e.save('shapefiles/test/polygon')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- ############################################################################### import shapefile sf = shapefile.Editor('/gdata/shape_towns.shp') ############################################################################### e = shapefile.Editor(shapefile="xx_sf_point.shp") e.point(0, 0, 10, 2) e.record("Appended", "Point") e.save('xx_sf_point') ############################################################################### e = shapefile.Editor(shapefile="xx_sf_line.shp") e.line(parts=[[[10, 5], [15, 5], [15, 1], [13, 3], [11, 1]]]) e.record('Appended', 'Line') e.save('xx_sf_line') ############################################################################### e = shapefile.Editor(shapefile="xx_sf_polygon.shp") e.poly(parts=[[[5.1, 5], [9.9, 5], [9.9, 1], [7.5, 3], [5.1, 1]]]) e.record("Appended", "Polygon") e.save('xx_sf_polygon') ############################################################################### e = shapefile.Editor(shapefile="xx_sf_point.shp") e.delete(0) e.save('xx_sf_point') ############################################################################### e = shapefile.Editor(shapefile="xx_sf_polygon.shp") e.delete(-1) e.save('xx_sf_polygon')
# -*- coding: utf-8 -*- print('=' * 40) print(__file__) from helper.textool import get_tmp_file ################################################################################ import shapefile e = shapefile.Editor(shapefile="xx_sf_point.shp") ################################################################################ e.point(0,0,10,2) e.record("Appended","Point") e.save('xx_sf_point') ################################################################################ e = shapefile.Editor(shapefile="xx_sf_line.shp") e.line(parts=[[[10,5],[15,5],[15,1],[13,3],[11,1]]]) e.record('Appended','Line') e.save('xx_sf_line') ################################################################################ e = shapefile.Editor(shapefile="xx_sf_polygon.shp") e.poly(parts=[[[5.1,5],[9.9,5],[9.9,1],[7.5,3],[5.1,1]]]) e.record("Appended","Polygon") e.save('xx_sf_polygon') ################################################################################ e = shapefile.Editor(shapefile="xx_sf_point.shp") e.delete(0) e.save('xx_sf_point')
def writePointToFile(points,fileStream): f = shapefile.Editor(shapefile=fp) fileStream.point(float(points[0]),float(points[1]),float(points[2]),float(points[3])) fileStream.record("Appended","Point") fileStream.save(fp) return
#!/usr/bin/env python import shapefile import sys import os e=shapefile.Editor('app_usa_states/us/us_states.shp') e.buildQuadTree('app_usa_states/quadTree.txt') for line in sys.stdin: tmp = line.strip().split(',') ind=e.index_of_first_feature_contains_point(float(tmp[2]),float(tmp[1])) if ind != -1: print line.strip()+','+str(ind)
counties = ['081', '047', '061', '005', '085'] newYorkFIPS = '36' records = sf.shapeRecords() for i in range(len(records)): if (records[i].record[1] in counties and records[i].record[0] == newYorkFIPS): indicies.append(i) print(indicies) #open the shapefile for reading. #Remove all records/shapes execpt for the five that were found above. e = shapefile.Editor(pathToFile) count = 0 for i in range(len(records) - 1, -1, -1): if (i not in indicies): e.delete(i) del e.records[i] count = count + 1 else: print(records[i].record) print(str(count) + " culled.") e.save(pathToFile + "Culled")
#!/usr/bin/env python import shapefile import sys e = shapefile.Editor(sys.argv[1]) e.buildQuadTree() output = open(sys.argv[2], 'w') e.printNode(e.root, output) output.close()