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_featurecollection(self):
p1 = geojson.Feature(geometry=geojson.Point((-115.11, 37.11)))
p2 = geojson.Feature(geometry=geojson.Point((-115.22, 37.22)))
itr = coords(geojson.FeatureCollection([p1, p2]))
pairs = list(itr)
self.assertEqual(pairs[0], (-115.11, 37.11))
self.assertEqual(pairs[1], (-115.22, 37.22))
def test_valid_jsonobject(self):
point = geojson.Point((-10.52, 2.33))
self.assertEqual(point.is_valid, True)
def test_invalid_geometrycollection(self):
point = geojson.Point((10, 20))
bad_poly = geojson.Polygon([[(2.38, 57.322), (23.194, -20.28),
(-120.43, 19.15), (25.44, -17.91)]])
geom_collection = geojson.GeometryCollection(
geometries=[point, bad_poly]
)
self.assertFalse(geom_collection.is_valid)
parameter_value = parameters_dict.get(parameter)
# print 'parameter_value = %s' % parameter_value
if (self._checkParameter(location, parameter_value)):
self.COOPS.variables = ['http://mmisw.org/ont/cf/parameter/%s' % parameter_value]
if parameter_value == 'water_surface_height_above_reference_datum':
self.COOPS.dataType = data_type
self.COOPS.datum = datum
response = self.COOPS.raw(responseFormat="text/csv", timeout=DEFAULT_TIMEOUT)
df = pd.read_csv(StringIO(response))
if df.empty:
print('No data found')
data_files[location][parameter] = None
continue
row = df.ix[0]
geometry = Point((float(row['longitude (degree)']),
float(row['latitude (degree)'])))
station_id = row['station_id']
location_id = station_id.split(':')[-1]
metadata = {'noaa_station_id': station_id,
'noaa_sensor_id': row['sensor_id']
}
df.index = pd.to_datetime(df['date_time'])
df.drop(df.columns[:5], axis=1, inplace=True)
filename = os.path.join(path, 'coops_stn_%s_%s.json' % (location, parameter))
data_files[location][parameter] = filename
io.write(filename, location_id=location_id, geometry=geometry, dataframe=df, metadata=metadata)
else:
data_files[location][parameter] = None
def find_nearest(self, point, layers):
"""
Find and return nearest object
:param point: GeoJSON Point or tuple of (x, y) or (x, y, srid)
:param layers: List of layer instances or layer names
"""
# Normalize point
if isinstance(point, tuple):
point = geojson.Point(coordinates=[point[0], point[1]])
if len(point) == 3:
point = self.transform(point, point[2], self.db_proj)
q = {"point__near": point}
if isinstance(layers, list):
q["layer__in"] = layers
else:
q["layer"] = layers
for o in Object.objects.filter(**q)[:1]:
return o
return None
def write_geojson(p, popup_content, filename):
with open(filename, 'w') as f:
properties = {'popupContent': popup_content}
features = [gj.Feature(geometry=gj.Point(p), properties=properties)]
f.write(gj.dumps(gj.FeatureCollection(features)))
f.close()
def stationToGeojson(station, period, headersOnly = False):
# negative Longitude in data is East, in MapBox it's West
point = geojson.Point((-1 * station['Long'],station['Lat']))
stationProperties = generateProperties(station, period, headersOnly)
feature = geojson.Feature(geometry = point, properties=stationProperties) if 'exclude' not in stationProperties else None
printv("FEATURE ENCODING COMPLETE, id: %s\t station: %20s\t" % (station['Number'], station['Name']))
return feature
tile_bounds = gmt.quadtree_to_lat_lon(geotile)
item_polygon = Polygon([[(tile_bounds[1], tile_bounds[0]),
(tile_bounds[1], tile_bounds[2]),
(tile_bounds[3], tile_bounds[2]),
(tile_bounds[3], tile_bounds[0]),
(tile_bounds[1], tile_bounds[0])
]])
item_f_poly = Feature(geometry=item_polygon)
item_f_poly.id = geo_node_derived
item_f_poly.geometry.id = geo_node_derived_geom
item_f_poly.properties.update(geo_props)
item_f_poly.properties['location-note'] = 'This region defines the '\
'approximate location for this item.'
item_f_poly.properties['id'] = geo_node_derived_props
features_dict[geo_node_derived] = item_f_poly
item_point = Point((float(geo.longitude), float(geo.latitude)))
item_f_point = Feature(geometry=item_point)
item_f_point.id = geo_node
item_f_point.geometry.id = geo_node_geom
item_f_point.properties.update(geo_props)
item_f_point.properties['location-note'] = 'This point defines the center of the '\
'region approximating the location for this item.'
item_f_point.properties['id'] = geo_node_props
features_dict[geo_node] = item_f_point
elif len(geo.coordinates) > 1:
# here we have geo_json expressed features and geometries to use
if geo.specificity < 0:
geo_props['location-precision-note'] = 'Location data approximated as a security precaution.'
elif geo.specificity > 0:
geo_props['location-precision-note'] = 'Location data has uncertainty.'
else:
geo_props['location-precision-note'] = 'Location data available with no '\
def row_to_geojson(row, lon, lat, precision, date_format='epoch'):
"""Convert a pandas dataframe row to a geojson format object. Converts all datetimes to epoch seconds.
"""
# Let pandas handle json serialization
row_json = json.loads(row.to_json(date_format=date_format, date_unit='s'))
return geojson.Feature(geometry=geojson.Point((round(row_json[lon], precision), round(row_json[lat], precision))),
properties={key: row_json[key] for key in row_json.keys() if key not in [lon, lat]})
def get_locations(self, bbox=None):
#get 100 random locations
if not bbox:
bbox = self.metadata['bbox']
x1, y1, x2, y2 = bbox
locations = [_random_point(bbox) for i in range(100)]
points = []
for location in locations:
properties = {'parameter': 'Temperature', 'units': 'degF', 'site_code': int(random()*1000000)}
points.append(Feature(geometry=Point(location), properties=properties, id=properties['site_code']))
return FeatureCollection(points)