I'm trying to translate coordinates to center them over Brussels. Due to the specific structure of my variable I don't know how to iterate over them. Please can someone explain me how I could proceed while keeping the structure of the coordinates because I need them to be in such structure to plot them.
# Load JSON file
with open("districts.json", "r") as f:
data = json.load(f)
# Create a transformation object
in_proj = pyproj.Proj(init='EPSG:3857')
out_proj = pyproj.Proj(proj='longlat', datum='WGS84')
# Transform the coordinates
features = data["features"]
for feature in features:
coords = feature["geometry"]["coordinates"][0]
coords = [pyproj.transform(in_proj, out_proj, coord[0], coord[1]) for coord in coords]
# Adjust the coordinates by adding a translation
brussels_center = [50.85045, 4.34878]
coords = [[(x[0]+brussels_center[0], x[1]+brussels_center[1]) for x in coord] for coord in coords]
feature["geometry"]["coordinates"] = [coords]
# Plot the polyggon on a map
m = folium.Map()
folium.GeoJson(data).add_to(m)
m
This is how the coordinates look like:
[(1.326960232616557, 1.5057587136636252),
(1.3270959770391675, 1.5058519176095608),
...,
(1.3264792386807474, 1.5054145891073423),
(1.326960232616557, 1.5057587136636252)]
I'm trying to translate the coordinates in such way that the structure does not change but to center them around Brussels. I don't understand why my iteration is wrong.
I am looking for an image transformation which can convert a line into an arc. My ultimate goal is to generate curved text of "Devanagari script".
Please be kind, I have looked and search on google and not able to find anything.
def transform_desire(image,curveIntensity):
'''
will convert image to arc form.
im1 : image.
curveIntensity : How much curved text you desired.
'''
im1 = image
ratio = 0.0001*curveIntensity
## calculate the desired width of the image.
height,width,channel = im1.shape
x = np.linspace(0,width,width).astype(int)
y = (ratio * ((x-(width/2))**2)).astype(int)
## corrosponding to an x every point will get shifted by y amount.
## time to shift.
## create canvas for new image.
adder = 0
if ratio >= 0:
adder = max(y)
else:
adder = (-1)*min(y)
retImage = (np.ones((height+adder,width, channel))*0).astype(np.uint8)
if ratio >= 0:
adder = 0
for xs in range(width):
ys = y[xs]
for t in range(height):
retImage[t+ys+adder,xs,:] = im1[t,xs,:]
return retImage
This worked for me. Please help yourself out, if you need this.
I'm using VTK 8.0.1 and python 3.5 and am brand new to VTK. I am trying to export a surface plot using vtkPlotSurface.
By referencing TestSurfacePlot.cxx I have successfully created a surface plot and have been able to render it in python (even though it doesn't really look like a surface plot).
import vtk
import math as m
import numpy as np
## Set things up
chart = vtk.vtkChartXYZ()
view = vtk.vtkContextView()
view.GetRenderWindow().SetSize(800,800)
view.GetScene().AddItem(chart)
## Create a surface
table = vtk.vtkTable()
numPoints = 70;
inc = 9.424778 / (numPoints - 1);
for i in range(0,numPoints):
arr = vtk.vtkFloatArray()
table.AddColumn(arr)
table.SetNumberOfRows(numPoints)
for i in range(0,numPoints):
x = i * inc;
for j in range(0,numPoints):
y = j * inc;
table.SetValue(i, j, m.sin(m.sqrt(x*x + y*y)))
# Using table, create a surface plot
test = vtk.vtkPlotSurface()
test.SetXRange(0,9.424778)
test.SetYRange(0,9.424778)
test.SetInputData(table)
# Start visualizing the surface plot
chart.AddPlot(test)
view.GetRenderWindow().SetMultiSamples(0)
view.GetInteractor().Initialize()
view.GetRenderWindow().Render()
out = vtk.vtkOBJExporter()
out.SetFilePrefix("test")
out.SetInput(chart)
out.Write()
view.GetInteractor().Start()
In order to better visualize what I've made, I wanted to try and export it and then visualize using Paraview/Visit. However, I'm struggling to find any concrete examples where this type of vtk object is exported...
I have tried adding the following:
out = vtk.vtkOBJExporter()
out.SetFilePrefix("test")
out.SetInput(chart)
out.Write()
But end up with the following type error:
TypeError: SetInput argument 1: method requires a vtkRenderWindow, a vtkContextView was provided.
Can anyone provide assistance? Thanks in advance.
You might benefit from using PyVista as it makes creating these types of spatially reference datasets and rendering much more user-friendly. I would avoid using a vtkTable like you have above and move towards VTK data objects that actually represent meshes/surfaces.
import pyvista as pv
import numpy as np
# Create a spatial reference
numPoints = 70
inc = 9.424778 / (numPoints - 1)
x = np.arange(0, numPoints) * inc
y = np.arange(0, numPoints) * inc
xx, yy, _ = np.meshgrid(x, y, [0])
zz = np.sin(np.sqrt(xx*xx + yy*yy))
# Make a PyVista/VTK mesh
surface = pv.StructuredGrid(xx, yy, zz)
# Plot it!
surface.plot(show_edges=True, show_grid=True, notebook=False)
# or save it out for opening in ParaView
surface.save("my_surface.vtk")
i want to get a python code to implement Fayad and Irani's Entropy based discretization.
please help me.
Three pythonic ways in which continuous variables/features can be discretized using a supervised method - MDLP by Fayyad, U.; Irani, K.
Assumptions
Input Data : X
target : y
categorical_features: Categorical columns that need not be discretized
Orange
import Orange
continuous_features = [*set(X.columns)-set(categorical_features)]
X[categorical_features] = X[categorical_features].astype('category')
data_df = pd.concat([X[continuous_features ], y.astype('category')], axis=1)
orange_table = table_from_frame(data_df, class_name=data_df.colum[-1])
disc = Orange.preprocess.Discretize()
disc.method = Orange.preprocess.discretize.EntropyMDL()
orange_table_discrete = disc(orange_table)
X[continuous_features ] = orange_table_discrete.X
Pypi package
from mdlp.discretization import MDLP
transformer = MDLP(continuous_features=[True if x not in categorical_features else False for x in X.columns])
X = pd.DataFrame(transformer.fit_transform(X, y), columns=X.columns)
Github repo
from entropymdlp import MDLP
mdlp = MDLP()
for col in X[continous_fetures].columns:
bins = mdlp.cut_points(X[col].values, y.values)
X[col] = mdlp.discretize_feature(X[col], bins)
X[col] = np.where(X[col].isnull(), np.nan, X[col])
To check discretization count of each column/feature
print(X.T.apply(lambda x: x.nunique(), axis=1))
Using this method in orange ML platform.
It seems to be based on Fayyad and Irani.
I think there is one here: https://github.com/navicto/Discretization-MDLPC
I haven't check it in detail, though.
I'm trying to update data in a mayavi 3D plot. Some of the changes to the data don't affect the data shape so the mlab_source.set() method can be used (which updates underlying data and refreshes the display without resetting the camera, regenerating the VTK pipeline, regenerating the underlying data structure, etc. This is the best possible case for animation or quick plot updates.
If the underlying data changes shape, the documentation recommends using the mlab_source.reset() method, which while not recreating the entire pipeline or messing up the current scene's camera, does cause the data structure to be rebuilt, costing some performance overhead. This is causing crashes for me.
The worst way to go is completely deleting the plot source and generating a new one with a new call to mlab.mesh() or whatever function was used to plot the data. This recreates a new VTK pipeline, new data structure, and resets the scene's view (loses current zoom and camera settings, which can make smooth interactivity impossible depending on the application).
I've illustrated a simple example from my application in which a Sphere class can have it's properties manipulated (position, size, and resolution). While changing position and size cause the coordinates to refresh, the data remains the same size. However changing the resolution affects the number of latitude and longitude subdivisions used to represent the sphere, which changes the number of coordinates. When attempting to use the "reset" function, the interpreter crashes completely. I'm pretty sure this is a C level segfault in the VTK code based on similar errors around the web. This thread seems to indicate the core developers dealing with the problem almost 5 years ago, but I can't tell if it was truly solved.
I am on Mayavi 4.3.1 which I got along with the Enthought Tool Suite with Python(x, y). I'm on Windows 7 64-bit with Python 2.7.5. I'm using PySide, but I removed those calls and let mlab work by itself for this example.
Here's my example that shows mlab_source.set() working but crashes on mlab_source.reset(). Any thoughts on why it's crashing? Can others duplicate it? I'm pretty sure there are other ways to update the data through the source (TVTK?) object, but I can't find it in the docs and the dozens of traits related attributes are very difficult to wade through.
Any help is appreciated!
#Numpy Imports
from time import sleep
import numpy as np
from numpy import sin, cos, pi
class Sphere(object):
"""
Class for a sphere
"""
def __init__(self, c=None, r=None, n=None):
#Initial defaults
self._coordinates = None
self._c = np.array([0.0, 0.0, 0.0])
self._r = 1.0
self._n = 20
self._hash = []
self._required_inputs = [('c', list),
('r', float)]
#Assign Inputs
if c is not None:
self.c = c
else:
self.c = self._c
if r is not None:
self.r = r
else:
self.r = self._r
if n is not None:
self.n = n
else:
self.n = self._n
#property
def c(self):
"""
Center point of sphere
- Point is specified as a cartesian coordinate triplet, [x, y, z]
- Coordinates are stored as a numpy array
- Coordinates input as a list will be coerced to a numpy array
"""
return self._c
#c.setter
def c(self, val):
if isinstance(val, list):
val = np.array(val)
self._c = val
#property
def r(self):
"""
Radius of sphere
"""
return self._r
#r.setter
def r(self, val):
if val < 0:
raise ValueError("Sphere radius input must be positive")
self._r = val
#property
def n(self):
"""
Resolution of curvature
- Number of points used to represent circles and arcs
- For a sphere, n is the number of subdivisions per hemisphere (in both latitude and longitude)
"""
return self._n
#n.setter
def n(self, val):
if val < 0:
raise ValueError("Sphere n-value for specifying arc/circle resolution must be positive")
self._n = val
#property
def coordinates(self):
"""
Returns x, y, z coordinate arrays to visualize the shape in 3D
"""
self._lazy_update()
return self._coordinates
def _lazy_update(self):
"""
Only update the coordinates data if necessary
"""
#Get a newly calculated hash based on the sphere's inputs
new_hash = self._get_hash()
#Get the old hash
old_hash = self._hash
#Check if the sphere's state has changed
if new_hash != old_hash:
#Something changed - update the coordinates
self._update_coordinates()
def _get_hash(self):
"""
Get the sphere's inputs as an immutable data structure
"""
return tuple(map(tuple, [self._c, [self._r, self._n]]))
def _update_coordinates(self):
"""
Calculate 3D coordinates to represent the sphere
"""
c, r, n = self._c, self._r, self._n
#Get the angular distance between latitude and longitude lines
dphi, dtheta = pi / n, pi / n
#Generate a latitude and longitude grid
[phi, theta] = np.mgrid[0:pi + dphi*1.0:dphi,
0:2 * pi + dtheta*1.0:dtheta]
#Map the latitude longitude grid into cartesian x, y, z coordinates
x = c[0] + r * cos(phi) * sin(theta)
y = c[1] + r * sin(phi) * sin(theta)
z = c[2] + r * cos(theta)
#Store the coordinates
self._coordinates = x, y, z
#Update the hash to coordinates to these coordinates
self._hash = self._get_hash()
if __name__ == '__main__':
from mayavi import mlab
#Make a sphere
sphere = Sphere()
#Plot the sphere
source = mlab.mesh(*sphere.coordinates, representation='wireframe')
#Get the mlab_source
ms = source.mlab_source
#Increase the sphere's radius by 2
sphere.r *= 2
#New coordinates (with larger radius)
x, y, z = sphere.coordinates
#Currently plotted coordinates
x_old, y_old, z_old = ms.x, ms.y, ms.z
#Verify that new x, y, z are all same shape as old x, y, z
data_is_same_shape = all([i.shape == j.shape for i, j in zip([x_old, y_old, z_old], [x, y, z])])
#Pause to see the old sphere
sleep(2)
#Check if data has changed shape... (shouldn't have)
if data_is_same_shape:
print "Updating same-shaped data"
ms.set(x=x, y=y, z=z)
else:
print "Updating with different shaped data"
ms.reset(x=x, y=y, z=z)
#Increase the arc resolution
sphere.n = 50
#New coordinates (with more points)
x, y, z = sphere.coordinates
#Currently plotted coordinates
x_old, y_old, z_old = ms.x, ms.y, ms.z
#Verify that new x, y, z are all same shape as old x, y, z
data_is_same_shape = all([i.shape == j.shape for i, j in zip([x_old, y_old, z_old], [x, y, z])])
#Pause to see the bigger sphere
sleep(2)
#Check if data has changed shape... (should have this time...)
if data_is_same_shape:
print "Updating same-shaped data"
ms.set(x=x, y=y, z=z)
else:
#This is where the segfault / crash occurs
print "Updating with different shaped data"
ms.reset(x=x, y=y, z=z)
mlab.show()
EDIT:
I just verified that all of these mlab_source tests pass for me which includes testing reset on an MGridSource. This does show some possible workarounds like accessing source.mlab_source.dataset.points ... maybe there's a way to update the data manually?
EDIT 2:
I tried this:
p = np.array([x.flatten(), y.flatten(), z.flatten()]).T
ms.dataset.points = p
ms.dataset.point_data.scalars = np.zeros(x.shape)
ms.dataset.points.modified()
#Regenerate the data structure
ms.reset(x=x, y=y, z=z)
It appears that modifying the TVTK Polydata object directly partly works. It appears that it's updating the points without also auto-fixing the connectivity, which is why I have to also run the mlab_source.reset(). I assume the reset() can work now because the data coming in has the same number of points and the mlab_source handles auto-generating the connectivity data. It still crashes when reducing the number of points, maybe because connectivity data exists for points that don't exist? I'm still very frustrated with this.
EDIT 3:
I've implemented the brute force method of just generating a new surface from mlab.mesh(). To prevent resetting the view I disable rendering and store the camera settings, then restore the camera settings after mlab.mesh() and then re-enable rendering. Seems to work quick enough - still wish underlying data could be updated with reset()
Here's the entire class I use to manage plotting objects (responds to GUI signals after an edit has been made).
class PlottablePrimitive(QtCore.QObject):
def __init__(self, parent=None, shape=None, scene=None, mlab=None):
super(PlottablePrimitive, self).__init__(parent=parent)
self._shape = None
self._scene = None
self._mlab = None
self._source = None
self._color = [0.706, 0.510, 0.196]
self._visible = True
self._opacity = 1.0
self._camera = {'position': None,
'focal_point': None,
'view_angle': None,
'view_up': None,
'clipping_range': None}
if shape is not None:
self._shape = shape
if scene is not None:
self._scene = scene
if mlab is not None:
self._mlab = mlab
#property
def shape(self):
return self._shape
#shape.setter
def shape(self, val):
self._shape = val
#property
def color(self):
return self._color
#color.setter
def color(self, color):
self._color = color
if self._source is not None:
surface = self._source.children[0].children[0].children[0]
surface.actor.mapper.scalar_visibility = False
surface.actor.property.color = tuple(color)
def plot(self):
x, y, z = self._shape.coordinates
self._source = self._mlab.mesh(x, y, z)
def update_plot(self):
ms = self._source.mlab_source
x, y, z = self._shape.coordinates
a, b, c = ms.x, ms.y, ms.z
data_is_same_shape = all([i.shape == j.shape for i, j in zip([a, b, c], [x, y, z])])
if data_is_same_shape:
print "Same Data Shape... updating"
#Update the data in-place
ms.set(x=x, y=y, z=z)
else:
print "New Data Shape... resetting data"
method = 'new_source'
if method == 'tvtk':
#Modify TVTK directly
p = np.array([x.flatten(), y.flatten(), z.flatten()]).T
ms.dataset.points = p
ms.dataset.point_data.scalars = np.zeros(x.shape)
ms.dataset.points.modified()
#Regenerate the data structure
ms.reset(x=x, y=y, z=z)
elif method == 'reset':
#Regenerate the data structure
ms.reset(x=x, y=y, z=z)
elif method == 'new_source':
scene = self._scene
#Save camera settings
self._save_camera()
#Disable rendering
self._scene.disable_render = True
#Delete old plot
self.delete_plot()
#Generate new mesh
self._source = self._mlab.mesh(x, y, z)
#Reset camera
self._restore_camera()
self._scene.disable_render = False
def _save_camera(self):
scene = self._scene
#Save camera settings
for setting in self._camera.keys():
self._camera[setting] = getattr(scene.camera, setting)
def _restore_camera(self):
scene = self._scene
#Save camera settings
for setting in self._camera.keys():
if self._camera[setting] is not None:
setattr(scene.camera, setting, self._camera[setting])
def delete_plot(self):
#Remove
if self._source is not None:
self._source.remove()
self._source = None