def row_to_geojson(row, lon, lat): """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='epoch', date_unit='s')) return geojson.Feature(geometry=geojson.Point( (row_json[lon], row_json[lat])), properties={ key: row_json[key] for key in row_json.keys() if key not in [lon, lat] })
def aggregate(featurecollection, resolution): """ Iterate through GeoJSON feature collection Returns GeoJSON feature collection of UTM boxes (polygons) with properties: utm UTM code with correct precision lat center coordinate lon center coordinate nresp number of responses contributing cdi aggregated intensity Arguments: pts GeoJSON feature collection resolution size of geocoding box in km (optional, default 1km) """ resolutionMeters = resolution * 1000 pts = featurecollection['features'] npts = len(pts) print('Got ' + str(npts) + ' entries, aggregating.') for pt in pts: loc = getAggregation(pt, resolutionMeters) if not loc: continue pt['properties']['loc'] = loc rawresults = {} earliesttimes = {} print('Aggregating points:') for pt in pts: if 'loc' not in pt['properties']: continue loc = pt['properties']['loc'] if loc in rawresults: rawresults[loc].append(pt) else: rawresults[loc] = [pt] results = [] for loc, pts in rawresults.items(): intensity = cdi.calculate(pts) coords = getCoords(loc, resolutionMeters) props = { 'cdi': intensity, 'nresp': len(pts), } pt = geojson.Feature(geometry=geojson.Point(coords), properties=props, id=loc) results.append(pt) print('Aggregated %i pts into %i pts' % (npts, len(results))) return results
def get_reps(self): self.reps = [] if not self.data: return for i, cluster in enumerate(self.clusters): logging.debug("Considering cluster %d = %s" % (i, cluster)) points = [[], [], [], []] # If this cluster has no points, we skip it if len(cluster) == 0: logging.info("Cluster %d = %s, has length %d, skipping" % (i, cluster, len(cluster))) continue for j, c in enumerate(cluster): logging.debug("Consider point %d = %s" % (j, c)) start_place = esda.get_entry(esda.CLEANED_PLACE_KEY, c.data.start_place) end_place = esda.get_entry(esda.CLEANED_PLACE_KEY, c.data.end_place) points[0].append( start_place.data.location["coordinates"][1]) # lat points[1].append( start_place.data.location["coordinates"][0]) # lng points[2].append( end_place.data.location["coordinates"][1]) # lat points[3].append( end_place.data.location["coordinates"][0]) # lng logging.debug("in representatives, endpoints have len = %s" % len(points)) centers = numpy.mean(points, axis=1) logging.debug("For cluster %d, centers are %s" % (i, centers)) t = ecwt.Trip({ "start_loc": gj.Point([centers[1], centers[0]]), "end_loc": gj.Point([centers[3], centers[2]]) }) a = ecwe.Entry.create_entry(c.user_id, "analysis/cleaned_trip", t) self.reps.append(a)
def build_a_feature(df: pd.DataFrame) -> Iterator[geojson.Feature]: """Yield a geojson feature for each row in the given df""" for x in df.itertuples(): geometry = geojson.Point((x.point.longitude, x.point.latitude)) yield geojson.Feature( geometry=geometry, properties={ 'doing_business_as': x.facility_name, 'inspection_date': x.activity_date, 'grade': x.resource_code if x.resource_code else "No Grade Recorded", 'address': x.address, 'violation_description': x.violation_description })
def get_visit_forecast_geojson(data, method="avg", progress=False): features = [] for i in range(len(data)): entry = data[i] hourly_forecast = get_hourly_forecast(entry, method) props = {"hourly_forecasts": hourly_forecast, "index": i} coords = (float(entry["mapping"]["longitude"]), float(entry["mapping"]["latitude"])) feat = gj.Feature(geometry=gj.Point(coords), properties=props) features.append(feat) if progress: print(i) feat_coll = gj.FeatureCollection(features) return feat_coll
def test_point(self): data = {"type": "Point", "coordinates": [D('100.0'), D('0.0')]} point = geojson.Point() point.coordinates = [D('100.0'), D('0.0')] self.assertTrue(point.is_valid()) for key, value in data.iteritems(): self.assertEquals(getattr(point, key), value) self.assertEquals(point.x, D('100.0')) self.assertEquals(point.y, D('0.0')) self.assertEquals(point.z, None) point.coordinates = [D('100.0'), D('0.0'), D('5200.0')] self.assertTrue(point.is_valid()) point.coordinates = [0, 1, 2, 3] self.assertTrue(point.is_valid())
def get_cur_loc(): data = {} try: df = pd.read_csv('https://s3.amazonaws.com/halfmarathon/current_loc', delimiter='|', names=['lat', 'lon', 'time']) lat = df.lat.tolist()[0] lon = df.lon.tolist()[0] point = geojson.Point((lon, lat)) data = json.loads(str(point)) data = {"geometry": data, "type": "Feature", "properties": {}} except Exception as e: pass return jsonify(data)
def scandir(dir): for itemindir in os.listdir(dir): # print("Map is: " + dir) # print("Bestand is: " + itemindir) totaalpad= dir + "\\" + itemindir # print(totaalpad) if itemindir != "Thumbs.db": plaatje = Image.open(totaalpad) exif_data = get_exif_data(plaatje) coords = get_lat_lon(exif_data) print(geojson.Point((coords[0],coords[1]))) # f = open("photos.json",'a') # f.write(str(geojson.Point((coords[0],coords[1])))) # f.close()
def geojson_points(self, as_features=False): """Returns list of geojson.Point or geojson.Feature(MultiPolygon).""" if not self.geojson_features: return None points = [] for area_feat in self.geojson_features: props = area_feat['properties'] p = props['point'] point = geojson.Point(p) if as_features: points.append(geojson.Feature(geometry=point,properties=props)) else: points.append(point) return points
def request(cls, req): import geojson n = 40 lat, lon = 37.775, -122.4183333 mod = lambda x: x + random.random() * 0.05 - 0.025 json = geojson.FeatureCollection(features=[ geojson.Feature( geometry=geojson.Point([mod(lon), mod(lat)]), properties={'ATTR': "%s, %s" % (mod(lon), mod(lat))}, ) for i in range(n) ]) return json
def nodesGenerator(graph, interleaved=True): nodes = graph.nodes(data=True) for i, (node, data) in enumerate(nodes): x = data['longitude'] y = data['latitude'] point = geojson.Point([x, y]) feature = geojson.Feature(geometry=point, id=node) if interleaved: yield (i, (x, y, x, y), geojson.dumps(feature, sort_keys=True)) else: yield (i, (x, x, y, y), geojson.dumps(feature, sort_keys=True))
def test_fromgeodataframefeatures(self): g_f = geojson.Feature() point = geojson.Point(coordinates=[12, 14]) g_f['properties'] = {"name": "name", "value": "value"} g_f['geometry'] = point g_fc = geojson.FeatureCollection([g_f]) gdf = geojson_2_geodataframe_features(g_fc) # type:geopandas.GeoDataFrame s_fs = from_geodataframe_features(gdf) result = list(s_fs) self.assertEqual(len(result), 1) self.assertEqual(result[0].geometry.points[0].x, 12) self.assertEqual(result[0].geometry.points[0].y, 14) self.assertEqual(result[0].fieldNames, ['name', 'value']) self.assertEqual(result[0].fieldValues, ['name', 'value'])
def get_weekly_visit_geojson(data): features = [] for n, entry in enumerate(data): props = { "week_visits": int(entry["record"]["raw_visit_counts"]), "index": n } coords = (float(entry["mapping"]["longitude"]), float(entry["mapping"]["latitude"])) feat = gj.Feature(geometry=gj.Point(coords), properties=props) features.append(feat) feat_coll = gj.FeatureCollection(features) return feat_coll
class Waters(): """ Quantifies waters """ def __init__(self): self.type = Flow(Flow.STANDING) # By default add floating waters self.type = Type(Type.SWEET) # Usually we want to track sweet waters coordinate = geojson.Point() # We can find all points of an area and quantify them as one water name = '' flow = Flow type = Type size = None # this is just a virtual property
def data2geojson(method, df, fileout): features = [] if method == 'velocity': insert_features = lambda X: features.append( geojson.Feature(geometry=geojson.Point((X["lon"], X["lat"])), properties=dict(timestamp=rfc3339.format(X["time"], utc=True), U=X["u"], V=X["v"]))) else: insert_features = lambda X: features.append( geojson.Feature(geometry=geojson.Point((X["lon"], X["lat"])), properties=dict(timestamp=rfc3339.format(X["time"], utc=True), E=X["e"]))) df.apply(insert_features, axis=1) with open(fileout, 'w', encoding='utf8') as fp: geojson.dump(geojson.FeatureCollection(features), fp, sort_keys=True, ensure_ascii=False)
def output_geojson(data): '''A GeoJSON output driver.''' fs = [] for point in data: pid, x, y, z, text = point x, y, z = [float(c) for c in x, y, z] p = geojson.Point([x, y, z]) prop = dict(text=text) f = geojson.Feature(id=pid, geometry=p, properties=prop) fs.append(f) fc = geojson.FeatureCollection(fs) return geojson.dumps(fc)
def mkGeoJSONPoint(obj, latkey, lonkey, attributes=False, transform=False, t_srs=config.TARGET_PROJECTION, s_srs=config.SOURCE_PROJECTION): ''' Return geojson feature collection ''' FC = [] for item in obj: x = item.pop(lonkey) y = item.pop(latkey) if transform: p1 = pyproj.Proj(s_srs) p2 = pyproj.Proj(t_srs) x, y = pyproj.transform(p1, p2, x, y) if attributes: FC.append( geojson.Feature(geometry=geojson.Point((x, y)), properties=item)) else: FC.append(geojson.Feature(geometry=geojson.Point((x, y)))) return geojson.dumps(geojson.FeatureCollection(FC), indent=2)
def output_geojson(self, INDENT=2, feature_type="line"): """Uses the geojson module to output a geojson file of the tropical cyclone. Default Arguments: INDENT = 2; used to provide indention to the output. Though it makes the output prettier and easier to read, it increases the file size. feature_type = "line"; determines the type of feature used in compiling the geoJSON file. "line" (default) indicates a geoJSON ``LineString`` while "point" indicates a geoJSON ``Point``. """ if feature_type.lower() not in ["point", "line"]: raise TypeError( "param feature_type must be either 'point' or 'line'.") ofn = "{}_{}_tracks_{}.geojson".format(self.atcfid, self.name, feature_type) # Ensure indention is an int INDENT = int(INDENT) feats = [] for trk in range(len(self.entry)): # Point feature if feature_type.lower() == "point": ls = geojson.Point((self.entry[trk].lon, self.entry[trk].lat)) # Polyline Feature elif feature_type.lower() == "line": ls = geojson.LineString([ (self.entry[trk].lon, self.entry[trk].lat), (self.entry[trk + 1].lon, self.entry[trk + 1].lat), ]) if trk != len(self.entry) - 1 else geojson.LineString([]) prp = { "ENTRY_ID": trk, "ATCFID": self.atcfid, "NAME": self.name, "ENTRY_TIME": self.entry[trk].entrytime.isoformat(), "LAT": self.entry[trk].lat, "LON": self.entry[trk].lon, "STATUS": self.entry[trk].status, "PEAK_WIND": self.entry[trk].wind if self.entry[trk].wind > 0 else None, "MSLP": self.entry[trk].mslp } feats.append(geojson.Feature(geometry=ls, properties=prp)) gjs = geojson.FeatureCollection(feats) with open(ofn, "w") as w: w.write(geojson.dumps(gjs, indent=INDENT))
def latest(request): points = LatestPoint.objects.filter( updated_at__gt=timezone.now() - timedelta(days=14), ).order_by('-updated_at')[:100] collection = geojson.FeatureCollection([ geojson.Feature(geometry=geojson.Point(i.point.coords), properties={ 'user': i.user.get_full_name(), 'user_url': api.user.get_resource_uri(i.user), 'updated_at': i.updated_at.isoformat(), }) for i in points ]) return HttpResponse(geojson.dumps(collection), content_type='application/json')
def create_map(): cap = capital.Capital_Service() query = cap.ds.query(kind=cap.kind) query.order = ['id'] city = [] for ent in list(query.fetch()): city.append(dict(ent)) features = [] for c in city: point = geojson.Point((float(c['longitude']), float(c['latitude']))) feature = geojson.Feature(geometry=point) features.append(feature) collection = geojson.FeatureCollection(features) return render_template('map_template.html', geocode=collection)
def df_to_geojson(df, out_file): features = [] insert_features = lambda X: features.append( geojson.Feature(geometry=geojson.Point((X["lon"], X["lat"])), properties=dict(date=X["date"], time=X["time"], big_boat=X["big_boat"], medium_boat=X["medium_boat"]))) df.apply(insert_features, axis=1) with open(out_file, 'w', encoding='utf8') as fp: geojson.dump(geojson.FeatureCollection(features), fp, sort_keys=True, ensure_ascii=False)
def _createTripEntry(self, start_ts, end_ts, start_loc, end_loc): t = ecwct.Cleanedtrip() sp = ecwcp.Cleanedplace() ep = ecwcp.Cleanedplace() t.start_ts = start_ts t.end_ts = end_ts if start_loc is not None: t.start_loc = gj.Point(start_loc) sp.location = t.start_loc if end_loc is not None: t.end_loc = gj.Point(end_loc) ep.location = t.end_loc sp.exit_ts = start_ts ep.enter_ts = end_ts spe = ecwe.Entry.create_entry(self.testUUID, "analysis/cleaned_place", sp, create_id=True) epe = ecwe.Entry.create_entry(self.testUUID, "analysis/cleaned_place", ep, create_id=True) t.start_place = spe.get_id() t.end_place = epe.get_id() te = ecwe.Entry.create_entry(self.testUUID, "analysis/cleaned_trip", t, create_id=True) self.ts.insert(spe) self.ts.insert(epe) self.ts.insert(te) return te
def ImgToPoint(path): f = open(path, 'rb') fileName = os.path.basename(path) #create dictionary of GPS Exif tags exif = {} tags = exifread.process_file(f, details=False) for i in tags: if i[0:3] == 'GPS': #print(i, ':', tags[i]) exif[i] = str(tags[i]) #Get Latitude latString = exif['GPS GPSLatitude'] latString = latString.strip('[ ]') latList = latString.split(',') separate = latList[2].split('/') d = int(latList[0]) m = int(latList[1])/60 s = int(separate[0])/int(separate[1]) s = s/3600 Latitude = d + m + s if exif['GPS GPSLatitudeRef'] == 'S': Latitude = Latitude*-1 #Get Longitude longString = exif['GPS GPSLongitude'].strip('[ ]') longList = longString.split(',') separate = longList[2].split('/') d = int(longList[0]) m = int(longList[1]) s = int(separate[0])/int(separate[1]) Longitude = d + m/60 + s/3600 if exif['GPS GPSLongitudeRef'] == 'W': Longitude = Longitude*-1 #Get Altitude altString = str(exif['GPS GPSAltitude']) altList = altString.split('/') altNum = int(altList[0])/int(altList[1]) #print(fileName) #print(Latitude, ', ', Longitude, sep='') point = geojson.Point((Longitude, Latitude)) feature = geojson.Feature(id=len(featureList)+1,geometry=point, properties={"Name":fileName, "Path":path}) return feature
def shapefile_to_geojson(shp_file, p=None): #input: shapefile name, index name, document type, OPTIONAL pyproj projection #output: geojson feature collection ###ONLY WORKS FOR SINGLE TYPE SHAPEFILE#### sf = shapefile.Reader(shp_file) features = [] recs = sf.iterRecords() for shape in sf.iterShapes(): rec = recs.next() props = {k: v for k, v in zip([f[0] for f in sf.fields[1:]], rec)} zipcode = props['ZCTA5CE10'] if p: coords = [p(x, y, inverse=True) for x, y in shape.points] else: coords = [(lng, lat) for lng, lat in shape.points] if shape.shapeType == 1: features.append( geojson.Feature(geometry=geojson.Point(coords[0]), properties={'zipcode': zipcode})) if shape.shapeType == 3: features.append( geojson.Feature(geometry=geojson.LineString(coords), properties={'zipcode': zipcode})) if shape.shapeType == 5: if len(shape.parts) == 1: geom = geojson.Polygon([[(pt1, pt2) for pt1, pt2 in coords]]) features.append( geojson.Feature(geometry=geom, properties={'zipcode': zipcode})) else: for i in range(len(shape.parts)): if i < len(shape.parts) - 1: part = coords[shape.parts[i]:shape.parts[i + 1]] else: part = coords[shape.parts[-1]:] geom = geojson.Polygon([[(pt1, pt2) for pt1, pt2 in part]]) feat = geojson.Feature( geometry=geom, properties={'zipcode': zipcode + '-' + str(i + 1)}) features.append(feat) return geojson.FeatureCollection(features)
def extract_station_records(outFilePath, station_file=None): #input: Location of zipped NOAA QCLCD data file #output: geojson feature collection of weather station locations, updates existing station geojson file if passed if os.path.isfile(outFilePath) and zipfile.is_zipfile(outFilePath): #if the url passed to the def exists and is a valid zip file #added for Linux (was creating an empty file for non-existent url downloads) z = zipfile.ZipFile(outFilePath) for f in z.namelist(): ##UPDATE THE WEATHER STATION INFORMATION if f.find('station.txt') > -1: csv.register_dialect('WBAN_dialect', delimiter='|') #WBAN file is PSV #update station info with contextlib.closing(z.open(f, 'r')) as stationFile: csv_dict = csv.DictReader( stationFile, dialect='WBAN_dialect' ) #read the PSV as list of row dicts curr_stations = {} #dict of current stations for row in csv_dict: decode_row = {} for k in row: decode_row[k] = row[k].decode( 'utf-8', 'ignore' ) #decode text, I was getting utf-8 errors without this #create geojson FeatureCollection of stations curr_stations[row['WBAN']] = geojson.Feature( geometry=geojson.Point((float(row['Longitude']), float(row['Latitude']))), properties=row) if os.path.isfile(station_file): with open(station_file, 'r') as geo: station_geojson = geojson.load(geo) for station in station_geojson['features']: _id = station['properties']['WBAN'] if _id in curr_stations: #update the station geojson feature information with stations listed in the current file station['geometry'] = curr_stations[_id][ 'geometry'] station['properties'] = curr_stations[_id][ 'properties'] else: #If the stations geojson feature collection doesn't already exist, create it from the current stations dict station_geojson = geojson.FeatureCollection( [curr_stations[wban] for wban in curr_stations]) z.close() return station_geojson #return the weather observation dataframe
def create_geojson(fruitname, infile, outfile): csv = read_csv(infile) data = [] collection = {'features': {}} for line in csv: if line != None: coord = [0, 0] try: # Definiere Grid für Koordinatenumwandlung utm = pyproj.Proj(proj="utm",zone=line[1]) dec = pyproj.Proj(init = "epsg:4326") # Wandle UTM Koordinaten um zu Lat/Long coord[1], coord[0] = pyproj.transform(utm, dec, line[2], line[3] ) except: print(fruitname + ": Fehler beim Umwandeln der Koordinaten von UTM zu Lat/Long: ", line[2], line[3] + "\n") # Kreiere GeoJson properties = { "UTM Region":line[1], "X_Coord" :line[2], "Y_Coord" :line[3], #"Strschl":line[12], #"Straße":line[13], #"Hausnummer":line[14], #"Kennung":line[18], "Pflanzjahr":line[4], "Höhe":int(line[12]) if line[12] != "0" else "keine Angabe", "Baumumfang":int(line[30]) if line[30] != "0" else "keine Angabe", "Alter":int(datetime.datetime.now().year) - int(line[4]), "Name":fruitname, "Deutsch":line[10], "Gattung":line[7], "Art":line[8], "Sorte":line[9], #"Kürzel":line[6], "Reifezeit":line[11], #"Artenschutz":line[44], } print(str(properties) + "\n") point = geojson.Point((coord[1], coord[0])) feature = geojson.Feature(geometry=point, id=line[0], properties=properties) data.append(feature) collection = geojson.FeatureCollection(data) # speichere in Datei dump_file(outfile, str(collection)) return collection
def data2geojson_epicenter(df): features = [] insert_features = lambda X: features.append( geojson.Feature(geometry=geojson.Point((X["lon"], X["lat"])), properties=dict( Rad=X["radius"], Time=X['time'], ))) df.apply(insert_features, axis=1) with open('media/epicenter_' + date + '.geojson', 'w', encoding='utf8') as fp: geojson.dump(geojson.FeatureCollection(features), fp, sort_keys=True, ensure_ascii=False)
def data2geojson(df): try: features = [] df.apply(lambda data: features.append( geojson.Feature(geometry=geojson.Point( (data["lat"], data["long"], data["elev"])), properties= {"country_area": data["country_area"]})), axis=1) with open('map.geojson', 'w') as fp: geojson.dump(geojson.FeatureCollection(features), fp, sort_keys=True) except ValueError: print('Out of range float values are not JSON compliant: nan')
def to_feature(self): if not self.coordinates: if self.refs: self.coordinates = self.find_bbox_center(self.refs) elif self.members: self.coordinates = self.find_bounding_box(self.members) if self.coordinates: point = geojson.Point(self.coordinates) return geojson.Feature(geometry=point, id=self.id, properties={ "tags": self.tags, "name": self.name }) return None
def geojsonConvert_Predictions(queryResult): collection = [] print(queryResult.size) for data in queryResult.itertuples(index=True, name='Pandas'): point = geojson.Point((data[1], data[2])) date = str(data.datetime.month) + "-" + str(data.datetime.year) feature = geojson.Feature(geometry=point, properties={ "certainty": data.certainty, "date": date }) collection.append(feature) dump = geojson.dumps(geojson.FeatureCollection(collection)) # print(dump) return dump