Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_invalid(self):
with self.assertRaises(ValueError) as cm:
geojson.loads('{"type":"Point", "coordinates":[[-Infinity, 4.0]]}')
self.assertIn('is not JSON compliant', str(cm.exception))
' "properties":'
' {"link": "http://example.org/features/1",'
' "summary": "The first feature",'
' "title": "Feature 1"},'
' "type": "Feature"}')
self.assertEqual(geojson.dumps(feature, sort_keys=True), json)
# Decoding
factory = geojson.examples.create_simple_web_feature
json = ('{"geometry": {"type": "Point",'
' "coordinates": [53.0, -4.0]},'
' "id": "1",'
' "properties": {"summary": "The first feature",'
' "link": "http://example.org/features/1",'
' "title": "Feature 1"}}')
feature = geojson.loads(json, object_hook=factory, encoding="utf-8")
self.assertEqual(repr(type(feature)),
"")
self.assertEqual(feature.id, '1')
self.assertEqual(feature.properties['title'], 'Feature 1')
self.assertEqual(feature.properties['summary'], 'The first feature')
self.assertEqual(feature.properties['link'],
'http://example.org/features/1')
self.assertEqual(geojson.dumps(feature.geometry, sort_keys=True),
'{"coordinates": [53.0, -4.0], "type": "Point"}')
def test_feature_collection_to_multi_polygon_nodissolve(self):
# arrange
grid_json = get_canned_json("test_grid.json")
grid_dto = GridDTO(grid_json)
expected = geojson.loads(json.dumps(get_canned_json("multi_polygon.json")))
aoi_geojson = geojson.loads(json.dumps(grid_dto.area_of_interest))
# act
result = GridService.merge_to_multi_polygon(aoi_geojson, False)
# assert
self.assertEqual(str(expected), str(result))
def upload_geojson(self, version, query):
json_res = self.db.query(query.format("GeoJSON"))
json = geojson.dumps(geojson.FeatureCollection([
geojson.Feature(
id=x[0],
geometry=geojson.loads(x[3]),
properties={"id": x[0], "name": x[1], "name_disp": x[2]}
) for x in json_res
]))
strio = StringIO.StringIO()
json_file = gzip.GzipFile(fileobj=strio, mode='w')
json_file.write(json)
json_file.close()
strio.seek(0)
key1 = self.bucket.new_key('{}.geojson.gz'.format(version))
key1.set_contents_from_file(strio, {"x-amz-acl": "public-read",
"Content-Type": "application/gzip"})
strio.seek(0)
key2 = self.bucket.new_key('{}.geojson'.format(version))
key2.set_contents_from_file(strio, {"x-amz-acl": "public-read",
"Content-Encoding": "gzip",
' found within 200m')
# get entire query results to work with
route_segments = cur.fetchall()
# empty list to hold each segment for our GeoJSON output
route_result = []
# loop over each segment in the result route segments
# create the list of our new GeoJSON
for segment in route_segments:
seg_cost = segment[3] # cost value
layer_level = segment[4] # floor number
seg_type = segment[5]
geojs = segment[6] # geojson coordinates
geojs_geom = loads(geojs) # load string to geom
geojs_feat = Feature(geometry=geojs_geom,
properties={'floor': layer_level,
'length': seg_cost,
'type_id': seg_type})
route_result.append(geojs_feat)
# using the geojson module to create our GeoJSON Feature Collection
geojs_fc = FeatureCollection(route_result)
return geojs_fc
@app.route('/encode', methods=['POST'])
def geojson_encode():
try:
g = request.form['geojson']
f = geojson.loads(g)
except Exception, e:
error = "failed to load geojson, because %s" % e
logging.error(error)
return jsonify(ok=0, error=error)
if (f.is_valid == False):
error = "GeoJSON doesn't validate: %s" % f.errors()
logging.error(error)
return jsonify(ok=0, error=error)
e = flask.g.geojson_encoder
try:
fh = StringIO.StringIO()
e.encode_feature(f, fh)
except Exception, e:
# SQLAlchemy's Reflecting Tables mechanism uses decimal.Decimal
# for numeric columns and datetime.date for dates. Python json
# doesn't deal with these types. This class provides a simple
# encoder to deal with objects of these types.
def default(self, obj):
if isinstance(obj, (datetime.date, datetime.datetime, datetime.time)):
return obj.isoformat()
if isinstance(obj, decimal.Decimal):
# The decimal is converted to a lossy float
return float(obj)
return GeoJSONEncoder.default(self, obj)
dumps = functools.partial(_dumps, cls=Encoder)
loads = functools.partial(_loads, cls=Encoder)
route_info = calc_distance_walktime(route_segments)
# empty list to hold each segment for our GeoJSON output
route_result = []
# loop over each segment in the result route segments
# create the list of our new GeoJSON
for segment in route_segments:
seg_length = segment[4] # length of segment
layer_level = segment[6] # floor number
seg_type = segment[7]
seg_node_id = segment[2]
seq_sequence = segment[0]
geojs = segment[8] # geojson coordinates
geojs_geom = loads(geojs) # load string to geom
geojs_feat = Feature(geometry=geojs_geom,
properties={'floor': layer_level,
'segment_length': seg_length,
'network_type': seg_type,
'seg_node_id': seg_node_id,
'sequence': seq_sequence}
)
route_result.append(geojs_feat)
# using the geojson module to create our GeoJSON Feature Collection
geojs_fc = FeatureCollection(route_result)
geojs_fc.update(route_info)
return geojs_fc