def image_poly(imgar): """ :param imgar: :return: """ polys = [] over_poly = [] bar = Bar('Plotting Image Bounds', max=len(imgar)) # print("BAR", bar) for cent in iter(imgar): lat = float(cent['coords'][1]) lng = float(cent['coords'][0]) print("**Drones Lng, Lats**", lng, lat) prps = cent['props'] fimr = float(prps['FlightRollDegree']) fimp = float(prps['FlightPitchDegree']) fimy = float(prps['FlightYawDegree']) gimr = float(prps['GimbalRollDegree']) gimp = float(prps['GimbalPitchDegree']) gimy = float(prps['GimbalYawDegree']) wid = prps['Image_Width'] hite = prps['Image_Height'] print("**Gimbal Pitch**", gimp, "\n**Gimbal Roll**", gimr, "\n**Gimbal Yaw**", gimy) # print("**ACFT Pitch**", fimp, "\n**ACFT Roll**", fimr, "\n**ACFT Yaw**", fimy) img_n = prps['File_Name'] print("**file name**", img_n) focal_lgth = prps['Focal_Length'] alt = float(prps["Relative_Altitude"]) cds1 = utm.from_latlon(lat, lng) # poly = new_gross(wid, hite, cds1, alt, focal_lgth, 90 + gimp, gimr, gimy, fimr, fimp, fimy) poly = new_gross(wid, hite, cds1, alt, focal_lgth, gimy, gimr, 90 + gimp, fimr, fimp, fimy) sensor = dict(name="name", focal=focal_lgth, sensor_x=13.2, sensor_y=8.8, pixel_x=wid, pixel_y=hite, alt=alt, gimy=gimy) ifov = footprint(sensor) print("IFOV MF", ifov) res = resolution(sensor, ifov) print("RES MF", res) p2 = convert_wgs_to_utm(lng, lat) print("P2", p2) project = partial( pyproj.transform, pyproj.Proj(init='epsg:4326'), # source coordinate system pyproj.Proj(init='epsg:%s' % p2)) # destination coordinate system g2 = transform(project, poly) over_poly.append(g2) # Create GeoJSON wow3 = geojson.dumps(poly) print("WOWOW3", wow3) wow4 = json.loads(wow3) wow4 = rewind(wow4) gd_feat = dict(type="Feature", geometry=wow4, properties=prps) # gs1 = json.dumps(gd_feat) # print("gs1", gs1) polys.append(gd_feat) bar.next() union_buffered_poly = cascaded_union([l.buffer(.001) for l in over_poly]) # print("UNION", union_buffered_poly) polyz = union_buffered_poly.simplify(0.005, preserve_topology=False) # print("polyz", polyz) projected = partial( pyproj.transform, pyproj.Proj(init='epsg:%s' % p2), # source coordinate system pyproj.Proj(init='epsg:4326')) # destination coordinate system g3 = transform(projected, polyz) # print("G3", g3) pop3 = geojson.dumps(g3) # print("POP3", pop3) pop4 = json.loads(pop3) pop4 = rewind(pop4) # print("pops4", pop4, "\npolys", polys) ssx = json.dumps(polys) # print("SSX", ssx) bar.finish() return polys, pop4
with urlopen( 'https://opendata.arcgis.com/datasets/48b6b85bb7ea43699ee85f4ecd12fd36_4.geojson' ) as response: counties = json.load(response) #Load data to be charted dummy_data = pd.read_csv( 'https://opendata.arcgis.com/datasets/48b6b85bb7ea43699ee85f4ecd12fd36_0.csv?outSR=%7B%22latestWkid%22%3A27700%2C%22wkid%22%3A27700%7D' ) #add dummy data dummy_data['value'] = np.random.randint(10, 100, size=len(dummy_data)) import plotly.express as px from geojson_rewind import rewind #With Plotly #Make the rings clockwwise (to make it compatible with plotly) counties_corrected = rewind(counties, rfc7946=False) fig = px.choropleth(dummy_data, geojson=counties_corrected, locations='nuts218cd', featureidkey="properties.nuts218cd", color='value', color_continuous_scale="PurPor", labels={'label name': 'label name'}, title='MAP TITLE', scope="europe") fig.update_geos(fitbounds="locations", visible=False)
def asdf(name=None): res = {} with db.get_cursor() as cursor: res["cartodata"] = str(rewind(cursor.all(SQL)[0])).replace("'", '"') return render_template('app.html', res=res)
def image_poly(imgar): """ :param imgar: :return: """ polys = [] over_poly = [] bar = Bar('Plotting Image Bounds', max=len(imgar)) # print("BAR", bar) for cent in iter(imgar): lat = float(cent['coords'][1]) lng = float(cent['coords'][0]) print("**Drones Lng, Lats**", lng, lat) prps = cent['props'] fimy = float(prps['FlightRollDegree']) fimx = float(prps['FlightPitchDegree']) fimz = float(prps['FlightYawDegree']) gimr = float(prps['GimbalRollDegree']) gimp = float(prps['GimbalPitchDegree']) gimy = float(prps['GimbalYawDegree']) print("**Gimbal Pitch**", gimp, "\n **Gimbal Roll**", gimr, "\n **Gimbal Yaw**", gimy) img_n = prps['File_Name'] # print("file name", img_n) focal_lgth = prps['Focal_Length'] alt = float(prps["Relative_Altitude"]) cds1 = utm.from_latlon(lat, lng) poly = new_gross(cds1, alt, focal_lgth, gimp, gimr, gimy, fimx, fimy, fimz) project = partial( pyproj.transform, # pyproj.Proj(init=darepo), pyproj.Proj(init='epsg:4326'), # source coordinate system pyproj.Proj(init='epsg:3857')) # destination coordinate system g2 = transform(project, poly) over_poly.append(g2) # Create GeoJSON wow3 = geojson.dumps(poly) wow4 = json.loads(wow3) wow4 = rewind(wow4) gd_feat = dict(type="Feature", geometry=wow4, properties=prps) gs1 = json.dumps(gd_feat) print("gs1", gs1) polys.append(gd_feat) bar.next() union_buffered_poly = cascaded_union([l.buffer(.001) for l in over_poly]) print("UNION", union_buffered_poly) polyz = union_buffered_poly.simplify(0.005, preserve_topology=False) print("polyz", polyz) projected = partial( pyproj.transform, pyproj.Proj(init='epsg:3857'), # source coordinate system pyproj.Proj(init='epsg:4326')) # destination coordinate system g3 = transform(projected, polyz) print("G3", g3) pop3 = geojson.dumps(g3) # print("POP3", pop3) pop4 = json.loads(pop3) pop4 = rewind(pop4) # print("pops4", pop4, "\npolys", polys) ssx = json.dumps(polys) # print("SSX", ssx) bar.finish() return polys, pop4
def test_no_mutate(self): # should not transform in-place infile = self.get_fixture_path('inner_reversed.input.geojson') _input = json.load(open(infile)) output = rewind(_input) self.assertNotEqual(_input, output)
def test_no_crs_no_warning(self): infile = self.get_fixture_path('point-no-crs.input.geojson') _input = json.load(open(infile)) with io.StringIO() as buf, redirect_stderr(buf): rewind(_input) self.assertEqual('', buf.getvalue())
def test_geometry_collection(self): infile = self.get_fixture_path('geomcollection.input.geojson') outfile = self.get_fixture_path('geomcollection.output.geojson') self.assertDictEqual(rewind(json.load(open(infile))), json.load(open(outfile)))
def test_str(self): # should convert str -> str infile = self.get_fixture_path('passthrough.input.geojson') output = rewind(open(infile).read()) self.assertIsInstance(output, str) self.assertEqual(output, json.dumps(json.load(open(infile))))
def plot_map(cum_plot_df, country='US', like_prediction='Per Tweet'): cum_plot_df['year'] = pd.DatetimeIndex(cum_plot_df['date']).year cum_plot_df['month'] = pd.DatetimeIndex(cum_plot_df['date']).month if like_prediction == 'Per Tweet': zmin = -1 zmax = 1 colorbar = {'title': 'Sentiment Polarity Rating'} elif like_prediction == 'Likes Per Tweet': zmin = -1 zmax = 1 colorbar = {'title': 'Sentiment Popularity Polarity Rating'} if country == "UK": with open('green_mood_tracker/raw_data/uk_regions.geojson') as f: data = json.load(f) data_wind = rewind(data, rfc7946=False) cum_plot_df['state_code'] = cum_plot_df['state_code'].replace({ "East of England": "East", "Yorkshire": "Yorkshire and the Humber", }) # your color-scale scl = [[0.0, "#800000"], [0.25, '#ff0000'], [0.5, '#ffa500'], [0.75, '#00ff00'], [1.0, '#008000']] # purples data_slider = [] altair_sent_by_year = [] altair_like_by_year = [] for year in cum_plot_df['year'].unique(): df_segmented_year = cum_plot_df[(cum_plot_df['year'] == year)] df_segmented = polarity_calc(df_segmented_year, like_prediction=like_prediction) altrair_sent_final = pd.DataFrame( columns=['date', 'Percentage of Sentiment', 'sentiment', 'month']) altrair_like_final = pd.DataFrame(columns=[ 'date', 'Percentage of Likes Per Sentiment', 'sentiment', 'month' ]) for month in df_segmented_year['month'].unique(): df_segmented_month = df_segmented_year[( cum_plot_df['month'] == month)] df_segmented_month_cumulative = cumulative_features( df_segmented_month) altrair_like_sum, altrair_sent_sum = altair_data( df_segmented_month_cumulative) altrair_sent_final = pd.concat( [altrair_sent_final, altrair_sent_sum], axis=0) altrair_like_final = pd.concat( [altrair_like_final, altrair_like_sum], axis=0) altrair_sent_final = altrair_sent_final.sort_values(by='month') altrair_like_final = altrair_like_final.sort_values(by='month') altair_sent_by_year.append(altrair_sent_final) altair_like_by_year.append(altrair_like_final) # df_segmented = df_segmented_year_cumulative.groupby('state_code').last()[['year','pos-per']].reset_index() if country == 'US': for col in df_segmented.columns: df_segmented[col] = df_segmented[col].astype(str) data_each_yr = dict(type='choropleth', locations=df_segmented['state_code'], z=df_segmented['polarity_av'].astype(float), locationmode='USA-states', colorscale=scl, zmin=zmin, zmax=zmax, colorbar=colorbar) data_slider.append(data_each_yr) elif country == 'UK': data_each_yr = dict(type='choropleth', locations=df_segmented['state_code'], z=df_segmented['polarity_av'].astype(float), geojson=data_wind, featureidkey="properties.rgn19nm", colorscale=scl, zmin=zmin, zmax=zmax, colorbar=colorbar) data_slider.append(data_each_yr) #steps = [] # for i in range(len(data_slider)): # step = dict(method='restyle', #args=['visible', [False] * len(data_slider)], # label='Year {}'.format(i + 2010)) #step['args'][1][i] = True # steps.append(step) #sliders = [dict(active=0, pad={"t": 1}, steps=steps)] if country == 'US': layout = dict(geo=dict(scope='usa', projection={'type': 'albers usa'}), ) elif country == 'UK': layout = dict(geo=dict(scope='europe', projection={'type': 'mercator'}), ) return altair_sent_by_year, altair_like_by_year, layout, data_slider
def test_multipolygon(self): infile = self.get_fixture_path('multipolygon.input.geojson') outfile = self.get_fixture_path('multipolygon.output.geojson') self.assertDictEqual(rewind(json.load(open(infile))), json.load(open(outfile)))
import dash import dash_html_components as html import json import pandas as pd import plotly.express as px import dash_core_components as dcc from dash.dependencies import Input, Output import plotly.graph_objs as go import geopandas as gpd from geojson_rewind import rewind f = gpd.read_file('maps.json', ) geo_ur = json.loads(f.to_json()) geo = rewind(geo_ur, rfc7946=False) df = pd.read_csv('maps.csv', sep=';') df['CC_1'] = df['CC_1'].astype(int) df['VARNAME_1'] = df['VARNAME_1'].astype(int) fig = px.choropleth(df, geojson=geo, locations="GID_1", color="VARNAME_1", hover_name="NAME_1", featureidkey="properties.GID_1", color_discrete_sequence=None, color_discrete_map={}, range_color=[1, 100], color_continuous_scale='Rainbow') fig.update_geos(fitbounds="locations", visible=False)
for obj in countries.find(): mongo_id = obj["_id"] del (obj["_id"]) name = obj["properties"]["ADMIN"] obj["name"] = name path = "/Users/griffin/1-Courses/5303-Adv-Database/Resources/01_Random_Data/countries/" + name + ".geojson" if not os.path.exists(path): f = open(path, "w") f.write(json.dumps(obj)) f.close() else: f = open(path, "r") obj = f.read() obj = json.loads(obj) name = obj["name"] del (obj["name"]) geojson = {} geojson["location"] = {} rgeometry = rewind(obj["geometry"]) geojson["location"]["type"] = "Feature" geojson["properties"] = {"name": name, "size": 0} geojson["location"]["geometry"] = rgeometry geojson["name"] = name countries_geo.insert_one(geojson)
def field_and_response_to_geometry(field, response): """ Return a GeoJSON `geometry` object given a field object (`FormField`) and an unformatted response (str). Maps XForm data types to GeoJSON geomtry types as follows: * `geopoint` to `Point`; * `geotrace` to `LineString`; * `geoshape` to `Polygon`. Gotcha: XForm lists the latitude first, but GeoJSON places the longitude in the first position! Both agree that the third position, if included, specifies the altitude in meters. The XForm types are described by https://opendatakit.github.io/xforms-spec/: `geopoint` | Space-separated list of valid latitude (decimal degrees), longitude (decimal degrees), altitude (decimal meters) and accuracy (decimal meters) `geotrace` | Semi-colon-separated list of at least 2 geopoints, where the last geopoint's latitude and longitude is not equal to the first `geoshape` | Semi-colon-separated list of at least 3 geopoints, where the last geopoint's latitude and longitude is equal to the first """ def split_geopoint_str(geopoint_str): """ From https://tools.ietf.org/html/rfc7946#section-4: "An OPTIONAL third-position element SHALL be the height in meters above or below the WGS 84 reference ellipsoid." From https://tools.ietf.org/html/rfc7946#section-9: "GeoJSON has no concept of uncertainty; imprecise or uncertain 'geo' URIs thus cannot be mapped to GeoJSON geometries." """ point_components = geopoint_str.split(' ') if not 2 <= len(point_components) <= 4: raise FormPackGeoJsonError('Cannot parse coordinates') try: coordinates = map(float, point_components[:3]) except ValueError: raise FormPackGeoJsonError('Non-numeric data for a coordinate') # Swap the coordinates because that's what GeoJSON wants 🙄 latitude, longitude = coordinates[:2] coordinates[0] = longitude coordinates[1] = latitude return coordinates geometry = {} if field.data_type == 'geopoint': geometry['type'] = 'Point' geometry['coordinates'] = split_geopoint_str(response) elif field.data_type == 'geotrace': geometry['type'] = 'LineString' geometry['coordinates'] = [ split_geopoint_str(point) for point in response.split(';') ] if len(geometry['coordinates']) < 2: raise FormPackGeoJsonError('Too few points for a line') elif field.data_type == 'geoshape': geometry['type'] = 'Polygon' geometry['coordinates'] = [ [split_geopoint_str(point) for point in response.split(';')], # We don't specify any holes in the `Polygon`, but if we did, # they'd go in another list here ] if len(geometry['coordinates'][0]) < 4: raise FormPackGeoJsonError('Too few points for a shape') # The first point must be equal to the last if geometry['coordinates'][0][0] != geometry['coordinates'][0][-1]: raise FormPackGeoJsonError('Shape is not closed') # GeoJSON requires the points to follow the right-hand rule; XForm does # not geometry = rewind(geometry) else: raise RuntimeError( '{field_name} is a {data_type}, which is not geographic'.format( field_name=field.name, data_type=field.data_type)) return geometry
def get_data(self): # pragma: no cover data_str = get_data_from_url(self.url) data_str = rewind(data_str.decode(self.encoding)) data = json.loads(data_str) return (data_str.encode(self.encoding), data)
#!/usr/bin/env python3 import geojson import json import sys from geojson_rewind import rewind with open(sys.argv[1]) as json_file: data = json.load(json_file) if 'crs' in data: del data['crs'] data = rewind(data) widget = geojson.GeoJSON(data) widget.is_valid widget.errors() print(widget)