def getDict(city, shapefile): cityDict = {city: {}} rec = [] shp = [] points = [] # Select only the records representing the # "city" and discard all other for i in shapefile.shapeRecords(): if i.record[4].upper() == city: rec.append(i.record) shp.append(i.shape) # total_area = sum( [float(i[0]) for i in rec] ) / (1000*1000) for j in shp: for i in getParts(j): points.append(i) # # # Prepare the dictionary # # Seperate the points into two separate lists of lists (easier for bokeh to consume) # # - one representing latitudes # # - second representing longitudes # lat = [] lng = [] for i in points: lat.append([j[0] for j in i]) lng.append([j[1] for j in i]) # # cityDict[city]['lat_list'] = lat cityDict[city]['lng_list'] = lng # stateDict[state_name]['total_area'] = total_area return cityDict
def shape_to_csv(self, shapefile): csv_records = [] field_names = [field[0] for field in shapefile.fields[1:]] for feature in shapefile.shapeRecords(): csv_record = (dict(zip(field_names, feature.record))) csv_record['geom'] = shape(feature.shape.__geo_interface__).wkt csv_records.append(csv_record) return csv_records
def _write_shapefile_to_geojson(shapefile, geojson_f): # From http://geospatialpython.com/2013/07/shapefile-to-geojson.html fields = shapefile.fields[1:] field_names = [field[0] for field in fields] buffer = [] for sr in shapefile.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 geojson_f.write( json.dumps({"type": "FeatureCollection", "features": buffer}, indent=2) + "\n" )
def getDict ( state_name, shapefile ): stateDict = {state_name: {} } rec = [] shp = [] points = [] # Select only the records representing the # "state_name" and discard all other for i in shapefile.shapeRecords( ): if i.record[2] == state_name: rec.append(i.record) shp.append(i.shape) # In a multi record state for calculating total area # sum up the area of all the individual records # - first record element represents area in cms^2 total_area = sum( [float(i[0]) for i in rec] ) / (1000*1000) # For each selected shape object get # list of points while considering the cases where there may be # multiple parts in a single record for j in shp: for i in getParts(j): points.append(i) # Prepare the dictionary # Seperate the points into two separate lists of lists (easier for bokeh to consume) # - one representing latitudes # - second representing longitudes lat = [] lng = [] for i in points: lat.append( [j[0] for j in i] ) lng.append( [j[1] for j in i] ) stateDict[state_name]['lat_list'] = lat stateDict[state_name]['lng_list'] = lng stateDict[state_name]['total_area'] = total_area return stateDict
def getDict ( county, shapefile ): countyDict = {(county[0], county[1]): {} } rec = [] shp = [] points = [] # Select only the records representing the # "state_name" and discard all other for i in shapefile.shapeRecords( ): if i.record[0] == county[0] and i.record[1] == county[1]: rec.append(i.record) shp.append(i.shape) # For each selected shape object get # list of points while considering the cases where there may be # multiple parts in a single record for j in shp: for i in getParts(j): points.append(i) # Prepare the dictionary # Seperate the points into two separate lists of lists (easier for bokeh to consume) # - one representing latitudes # - second representing longitudes lat = [] lng = [] for i in points: lat.append( [j[0] for j in i] ) lng.append( [j[1] for j in i] ) countyDict[(county[0], county[1])]['lat_list'] = lat countyDict[(county[0], county[1])]['lng_list'] = lng return countyDict
def find_point(shapefile, df): ''' :param shapefile: shapefile文件 :param df: 经纬度点 :return: 属于shapefile所在区域的位置 ''' in_shape_points = [] latitudes = [] longitudes = [] indexs = [] table_data = [] rds = shapefile.shapeRecords() print(rds[0]) for index, row in df.iterrows(): lat = row['latitude'] lon = row['longitude'] if lon < lon1 or lon > lon2 or lat < lat1 or lat > lat2: pass else: print(lon, lat) latitudes.append(lat) longitudes.append(lon) indexs.append(index) table_data.append(row.values) pt = (lon, lat) for rd in rds: if geometry.Point(pt).within(geometry.shape(rd.shape)): in_shape_points.append(pt) print(pt) break else: # not_in_shape_points.append(pt) pass selected_lon = [elem[0] for elem in in_shape_points] selected_lat = [elem[1] for elem in in_shape_points] return (selected_lon, selected_lat), table_data
#!/usr/bin/env python3 import shapefile import shapely_geojson from shapely.geometry import shape shapefile = shapefile.Reader('tiling_grid/shape/sentinel2_tiles_world.shp') records = shapefile.shapeRecords() for feature in records: #print(feature.shape.__geo_interface__) print(feature.record[0]) geom = shape(feature.shape).buffer(-0.3) geom2 = shapely_geojson.Feature(geom, properties={'name': feature.record[0]}) #print(shapely_geojson.dumps(geom2)) with open('tiling_grid/geojson/{0}.geojson'.format(feature.record[0]), 'w') as outfile: outfile.write(shapely_geojson.dumps(shapely_geojson.FeatureCollection([geom2])))