Looking for some guidance on how to convert a KML file to an image file showing simple polygons of the GPS data held in the file? I've been looking at ways to do this via python using mapnik and simplekml but I'm unsure if this is the correct usage of the tools.
Ideally, I just want a simple way to produce polygons from a KML file
Any advice very welcome
Manage to get a crud script working using geopandas
import geopandas as gpd
import matplotlib.pyplot as plt
gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'
# Filepath to KML file
fp = "history.kml"
polys = gpd.read_file(fp, driver='KML')
print(polys)
polys.plot()
plt.savefig('test.jpg')
Related
I need to create a Wind Rose KML file to be opened in google earth with as (similar to) the following pictures.
I can create wind roses using windrose python module, like this one:
And I know how to create KML points and lines in python using simplekml module like this one:
Does anyone know any package capable of doing that?
or any idea of how to do it?
If you use matplotlib when draw windrose, try to save the fig with following opthons.
plt.axis('off')
plt.savefig('windrose.png', bbox_inches='tight', pad_inches=0, transparent=True)
After you got the image file, generate ground overlay code in kml.
import simplekml
kml = simplekml.Kml(open=1)
doc = kml.newdocument(name='sample', open=1, visibility=0)
ground = doc.newgroundoverlay(name='windrose example')
ground.icon.href = 'windrose.png'
ground.altitudemode = simplekml.AltitudeMode.absolute
ground.altitude = 500.0
ground.latlonbox.north = 38.031368255615234
ground.latlonbox.south = 37.11344909667969
ground.latlonbox.east = 141.5791015625
ground.latlonbox.west = 140.4208984375
ground.visibility = 1
kml.save('sample.kml')
Each values are need to adjust.
Please refer following, if you'd like to know more about ground overlay.
https://developers.google.com/kml/documentation/altitudemode#absolute
Open Google Earth.
Draw a static picture\chart like wanted one over a map.
Save it into a KML file.
Open with any text editor.
It must show you an example which lead you to generate a valid KML with your data.
I have converted a NetCDF file to Geotiff. However when I display the Geotiff file it appears flipped or upside down. Here is my code:
import xarray as xr
import rioxarray as rio
import rasterio
nc_file = xr.open_dataset('Africa_salinity.nc')
nc_file
sss = nc_file['sss']
sss
sss = sss.rio.set_spatial_dims(x_dim='lon', y_dim='lat')
sss.rio.crs
sss.rio.write_crs("epsg:4326", inplace=True)
sss.rio.to_raster(r"Africa_19.tiff")
ax = show((sss))
How can I rotate the file and save it as tiff to use for further analysis?
You should be able to fix this using CDO on the command line. The following will likely work:
cdo invertlat infile outfile
This will invert the latitudes as the name suggests
I am using PyQGIS to import a csv file with a lat and long, when doing this I am using the appropriate crs of EPSG:4326.
I'm plotting this onto Google Maps.
I load my basemap, then import my CSV. The issue is that my basemap projection then changes to 4326 and I need it to remain on 3857.
I've tried importing the basemap after the CSV and moving it down in the layers, however this still changes the projections.
import requests
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from qgis.core import *
from qgis.utils import iface
from qgis import core
#Use Google Street Map as QGIS basemap.
service_url = "mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}"
service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
tms_layer = iface.addRasterLayer(service_uri, "GoogleSat", "wms")
#Import CSV and plot.
uri = 'file:///home/user/fred.csv?type=csv&xField=%s&yField=%s&crs=%s' % ("Site Longitude", "Site Latitude", "EPSG:4326")
layer_csv = QgsVectorLayer(uri, 'fred', 'delimitedtext')
layer_csv.isValid()
QgsProject.instance().addMapLayer(layer_csv)
I'll be the first to admit I'm a novice with QGIS!
It seems this has something to do with the application not refreshing properly as mentioned in this answer on gis stack. You may want to look into it for details.
To answer your question in brief, you can add QApplication.instance().processEvents() after QgsProject.instance().addMapLayer(layer_csv) and then use setCrs() to set your basemap CRS to whatever value you need. It will hold.
proj = QgsProject.instance()
proj.addMapLayer(layer_csv)
# This line makes the difference
QApplication.instance().processEvents()
# This sets the project CRS back to 3857
proj.setCrs(QgsCoordinateReferenceSystem(3857))
I was trying to read fits files from Kepler FITS files (Received from this URL https://archive.stsci.edu/pub/kepler/lightcurves/0007/000757076/) using astropy. Below are the set of commands I was trying to read the file:
from astropy.io import fits
fits_image_filename = fits.util.get_testdata_filepath(r'O:\MyWorks\keplar-test\kplr100000925-2009166043257_llc.fits')
But the above command produced this error:
I am not sure how to solve this error. My target is to read keplar data then plot this and/or convert this to CSV.
This: fits.util.get_testdata_filepath(r'O:\MyWorks\keplar-test\kplr100000925-2009166043257_llc.fits') is not the correct function for opening a file.
You should use fits.open('file.fits'), or if this is table data, as you imply, Table.read('file.fits')
See the note at the top of the FITS documentation
%matplotlib inline
from astropy.io import fits
import matplotlib
import matplotlib.pyplot as plt
#My required file has been downloaded in the following path of my HD,
"~/projects/eclipsing_binary/A/mastDownload/HLSP/hlsp_qlp_tess_ffi_s0018-0000000346784049_tess_v01_llc/hlsp_qlp_tess_ffi_s0018-000000346784049_tess_v01_llc.fits". Using linux command open and see the list
of the files in the directory.
%cd ~/projects/eclipsing_binary/A/mastDownload/HLSP/
hlsp_qlp_tess_ffi_s0018-0000000346784049_tess_v01_llc/
%ls
#Now plot the required file in simple way,
import lightkurve as lk
file_r = 'hlsp_qlp_tess_ffi_s0018-0000000346784049_tess_v01_llc.fits'
lr = lk.read(file_r)
lr.plot()
I have tried to export a visualisation of data with ipyleaflet as PNG or any other file format but i could not find a method that is working. For example in folium there is map.save(path). Is there a library or method in ipyleaflet that i have missed in my research which helps me to accomplish my goal?
here is some example code to generate a map
from ipyleaflet import *
center = [34.6252978589571, -77.34580993652344]
zoom = 10
m = Map(default_tiles=TileLayer(opacity=1.0), center=center, zoom=zoom)
m
I'd like to export this map as an image file without taking a screenshot manually.
I found two sources that allow to export javascript leaflet maps:
https://github.com/aratcliffe/Leaflet.print and https://github.com/mapbox/leaflet-image
Unfortunately i was not able to make use of them in python.
My colleague and I found a decent work around for ipyleaflet (python) image export. Here is how it works. The folium library is required for an export. The GeoJson data in this example is already prepared with style properties:
import folium
map = folium.Map([51., 12.], zoom_start=6,control_scale=True)
folium.GeoJson(data).add_to(map)
map.save('map.html')
This is how the result looks:
The html file can be further processed in python (windows) with subprocess calls to make a PDF or PNG out of it. I hope this helps as the ipyleaflet doc for python is almost non existant.
For generating html, you can use ipywidgets
from ipywidgets.embed import embed_minimal_html
embed_minimal_html('map.html', views=[m])
If you want to make a PNG, you can use ipywebrtc, more specifically:
https://ipywebrtc.readthedocs.io/en/latest/ImageRecorder.html
https://ipywebrtc.readthedocs.io/en/latest/WidgetStream.html
Or in code:
from ipywebrtc import WidgetStream, ImageRecorder
widget_stream = WidgetStream(widget=m, max_fps=1)
image_recorder = ImageRecorder(stream=widget_stream)
display(image_recorder)
Saving the PNG:
with open('map.png', 'wb') as f:
f.write(image_recorder.image.value)
Or converting to pillow image for preprocessing:
import PIL.Image
import io
im = PIL.Image.open(io.BytesIO(image_recorder.image.value))
ipyleaflet supports saving as html. Export of svg and png does not seem to be supported.
https://ipyleaflet.readthedocs.io/en/latest/map_and_basemaps/map.html#save-to-html
m.save('output.html')
I created an issue ticket for ipyleaflet:
https://github.com/jupyter-widgets/ipyleaflet/issues/1083