""" Proyecto Ana Cruz Martín """ from gvsig import * from geom import * from commonsdialog import * from math import * def unifyGeometryType(geometryType): if geometryType == "Point2D" or geometryType == "Point2DZ": unifiedGeometryType = "U_POINT" elif geometryType == "Curve2D" or geometryType == "Curve2DZ": unifiedGeometryType = "U_LINE" elif geometryType == "Surface2D" or geometryType == "Surface2DZ": unifiedGeometryType = "U_POLYGON" else: unifiedGeometryType = [] return unifiedGeometryType def main(*args): """This script "moves" a set of selected geometries of the active layer (or all the geometries in the layer, if there are not selected geometries). The movement is a linear uniformly acceleration motion, that depends on the speed, acceleration, direction and amount of time given by the user. Those parameters are represented in the following units and assuming projection EPSG:32630: - Speed: meters/second (only float positive values) - Acceleration: meters/second² (only float positive values) - Direction: sexagesimal degrees (float positive/negative values) - Time: seconds (only float positive values) The script builds a new layer which contains the new positions of the geometries according to movement defined by the parameters. The new layer will be stored according to the user's selection. It will follow a basic schema: ID, GEOMETRY""" #Get the parameters for the movement. Error control could be extended. speed = inputbox("Please, enter the speed value (float positive meters/second)","Speed",3) speed = float(speed) if speed < 0: msgbox(u"Error: speed should be positive","Speed error",2) return acceleration = inputbox("Please, enter the acceleration value (float positive meters/seconds^2)","Acceleration",3) acceleration = float(acceleration) if acceleration < 0: msgbox(u"Error: acceleration should be positive","Acceleration error",2) return direction = inputbox("Please, enter the direction value (float positive or negative sexagesimal degrees)","Direction",3) direction = float(direction) endTime = inputbox("Please, enter the final time value (float positive seconds)","Time",3) endTime = float(endTime) if endTime < 0: msgbox(u"Error: time should be positive","Time error",2) return newLayerName = inputbox("Please, enter the path and name of the new layer","Layer Name",3) #Create the new layer projection = currentView().getProjection() newLayerSchema = createSchema() newLayerSchema.append("ID","INTEGER") newLayerSchema.append("GEOMETRY","GEOMETRY") newLayer = createShape(newLayerSchema,newLayerName,CRS=projection,geometryType=POINT) # Get the selected entities of the active layer; if there are no selected entities, all the entities of the layer are moved if currentLayer().getSelection().getCount() == 0: entities = currentLayer().features() else: entities = currentLayer().getSelection() # Get a unified geometry type for the new layer, according to the geometry type of the active layer geometryType = currentLayer().getTypeVectorLayer().name unifiedGeometryType = unifyGeometryType(geometryType) #Compute the "movement" of the geometries index = 1 if unifiedGeometryType == "U_POINT": for time in range(1,endTime): for entity in entities: #New coordinates oldX = entity.geometry().getX() oldY = entity.geometry().getY() space = speed*time+(0.5*acceleration*pow(time,2)) newX = oldX+space*cos(radians(direction)) newY = oldY+space*sin(radians(direction)) #Create a geometry with the new values and add them to the new layer newGeometry = createPoint(newX,newY) newValues = {"ID":index,"GEOMETRY":newGeometry} newLayer.append(newValues) index+=1 elif unifiedGeometryType == "U_LINE": print "Linea" elif unifiedGeometryType == "U_POLYGON": print "Poligono" else: msgbox("Error: the layer geometry type is wrong","Geometry type error",2) return # Add the new layer to the current view and commit currentView().addLayer(newLayer) nuevaCapa.commit()