Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
attribute - the attribute to be mapped
The rest of the arguments are keyword:
classification - type of classification scheme to be used
classes - number of classes used
bins - breakpoints, if manual classes are desired
'''
#Polymorphism by hand...
if isinstance(jsonpath, str):
if os.path.isfile(jsonpath):
sjson = gj.load(open(jsonpath))
else:
raise IOError('File not found')
elif isinstance(jsonpath, tuple):
if 'ShpWrapper' in str(type(jsonpath[0])) and 'DBF' in str(type(jsonpath[1])):
flip('tmp.json', jsonpath[0], jsonpath[1])
sjson = gj.load(open('tmp.json'))
jsonpath = 'tmp.json'
elif 'ShpWrapper' in str(type(jsonpath[1])) and 'DBF' in str(type(jsonpath[0])):
flip('tmp.json', jsonpath[1], jsonpath[0])
sjson = gj.load(open('tmp.json'))
jsonpath = 'tmp.json'
else:
raise IOError('Inputs must be GeoJSON filepath, GeoJSON dictionary in memory, or shp-dbf tuple')
else:
return_history (bool): Return a list containing metrics from past epochs.
Defaults to False.
save_all_weights (bool): Save model weights after each epoch. A directory
called models will be created in the working directory. Defaults to
True.
retrain (bool): Freeze all layers except final softmax to retrain only
the final weights of the model. Defaults to False
learning_rate_2 (float): Learning rate for the second round of training.
Only relevant if retrain is True. Defaults to 0.01.
OUTPUT trained model, history
'''
resize_dim, validation_data, full_hist = None, None, []
# load geojson training polygons
with open(train_geojson) as f:
polygons = geojson.load(f)['features'][:train_size]
if len(polygons) < train_size:
raise Exception('Not enough polygons to train on. Please add more training ' \
'data or decrease value of batches_per_epoch.')
# Determine size of chips to extract and resize dimension
if not max_side_dim:
max_side_dim = self.input_shape[-1]
elif max_side_dim != self.input_shape[-1]:
resize_dim = self.input_shape # resize chips to match input shape
# Recompile model with retrain params
if retrain:
for i in xrange(len(self.model.layers[:-1])):
self.model.layers[i].trainable = False
def geojsonToShapes():
datadir = os.path.join(os.getcwd(), 'data')
gtfsdir = os.path.join(datadir, 'gtfs')
geojsondir = os.path.join(datadir, 'geojson')
with open(gtfsdir + "/shapes_new.txt", 'wb') as shapesfile:
shapeswriter = csv.writer(shapesfile)
shapeswriter.writerow(["shape_id","shape_pt_sequence","shape_dist_traveled","shape_pt_lon","shape_pt_lat"])
geojsonfile = os.path.join(geojsondir, 'shapes.geojson')
with open(geojsonfile, 'rb') as fc:
geo_fc = geojson.load(fc)
# print geo_fc
for feature in geo_fc['features']:
for i, coord in enumerate(feature['geometry']['coordinates']):
shapeswriter.writerow([feature['properties']['shape_id'],i,'',coord[0],coord[1]])
def get_locations(self, locations=None, bounding_box=None, **kwargs):
if locations is not None:
return self.get_feature_locations(locations, **kwargs)
src_path = kwargs.get('src_path')
if src_path is None:
raise IOError('src path not defined')
path = os.path.join(src_path, kwargs['location_file'])
fmt = kwargs['location_fmt']
if fmt.lower()=='geojson':
return load(open(path))
if fmt.lower()=='shapefile':
features = []
with fiona.drivers():
with fiona.open(path, 'r') as source:
for feature in source:
features.append(feature)
return FeatureCollection(features)
if fmt.lower()=='txt-bbox':
polys = []
with open(path) as f:
#skip first line which is a bunding polygon
f.readline()
for line in f:
(not including file extension)
(3) bool 'balanced': put equal amounts of each class in the output shapefile.
Otherwise simply outputs shuffled version of original dataself.
(4) list[string] 'class_names': name of classes of interest as listed in
properties['class_name']. defaults to pool classes.
(5) int or None 'samples_per_class': number of samples to select per class.
if None, uses length of smallest class. Defaults to None
(6) float or None 'train_test': proportion of polygons to save in test file.
if None, only saves one file (balanced data). otherwise saves a train and
test file. Defaults to None.
OUTPUT (1) geojson file with balanced classes in current directory
'''
with open(shapefile) as f:
data = geojson.load(f)
if balanced_classes:
# sort classes into separate lists
sorted_classes = []
for i in class_names:
this_data = []
for feat in data['features']:
if feat['properties']['class_name'] == i:
this_data.append(feat)
sorted_classes.append(this_data)
# randomly select given number of samples per class
if samples_per_class:
def load_geojson_data():
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "chinageojson.json"), 'r',
encoding='utf-8') as geoJsonFile:
return geojson.load(geoJsonFile)
resize_dim, yprob, ytrue = None, [], []
# Determine size of chips to extract and resize dimension
if not max_side_dim:
max_side_dim = self.input_shape[-1]
elif max_side_dim != self.input_shape[-1]:
resize_dim = self.input_shape # resize chips to match input shape
# Format output filename
if not output_name.endswith('.geojson'):
output_name = '{}.geojson'.format(output_name)
# Get polygon list from geojson
with open(target_geojson) as f:
features = geojson.load(f)['features']
# Classify in batches of 1000
for ix in xrange(0, len(features), chips_in_mem):
this_batch = features[ix: (ix + chips_in_mem)]
try:
X = get_chips(this_batch, min_side_dim=min_side_dim,
max_side_dim=max_side_dim, classes=self.classes,
normalize=True, return_labels=False,
bit_depth=bit_depth, mask=True, show_percentage=False,
assert_all_valid=True, resize_dim=resize_dim)
except (AssertionError):
raise ValueError('Please filter the input geojson file using ' \
'geojoson_tools.filter_geojson() and ensure all ' \
'polygons are valid before using this method.')
INPUT input_file (str): File name
classes (list[str]): Classes in input_file to include in the balanced
output file. Must exactly match the 'class_name' property in the features
of input_file.
output_file (str): Name under which to save the balanced output file.
Defualts to balanced.geojson.
samples_per_class (int or None): Number of features to select per class in
input_file. If None will use the smallest class size. Defaults to None.
'''
if not output_file.endswith('.geojson'):
output_file += '.geojson'
with open(input_file) as f:
data = geojson.load(f)
# Sort classes in separate lists
sorted_classes = {clss : [] for clss in classes}
for feat in data['features']:
try:
sorted_classes[feat['properties']['class_name']].append(feat)
except (KeyError):
continue
# Determine sample size per class
if not samples_per_class:
smallest_class = min(sorted_classes, key=lambda clss: len(sorted_classes[clss]))
samples_per_class = len(sorted_classes[smallest_class])
# Randomly select features from each class
def join(input_files, output_file):
'''
Join geojsons into one. The spatial reference system of the
output file is the same as the one of the last file in the list.
Args:
input_files (list): List of file name strings.
output_file (str): Output file name.
'''
# get feature collections
final_features = []
for file in input_files:
with open(file) as f:
feat_collection = geojson.load(f)
final_features += feat_collection['features']
feat_collection['features'] = final_features
# write to output file
with open(output_file, 'w') as f:
geojson.dump(feat_collection, f)