for (polyIdx=0;i<polys.length;polyIdx++){ var paths = polys[polyIdx].getPaths().getArray(); var arMultiPolygons=[]; for (j=0;j<paths.length;j++){ var arPolygons=[]; for (i=0;i<paths[j].length;i++) { arPolygons.push([paths[j].getArray()[i].lng(),paths[j].getArray()[i].lat()]); } arMultiPolygons.push(p) } }
polys
- all polygons are stored. Then it is easy to get the coordinates of all the vertices of the polygons, but you need not forget that all the same it is MultiPolygons . Next you need to put these polygons in the database. Solution: selenium , which allows js code to be executed. Base - PostgreSQL + Postgis. from selenium import webdriver from shapely.geometry import polygon,multipolygon from shapely.wkt import loads import psycopg2 con = psycopg2.connect(database="WB",user='postgres',password='pass', host='127.0.0.1', port='5432') cur = con.cursor() # ur.execute("create table googlePolygons ( idx integer, googlegeom geometry );") browser = webdriver.Firefox() browser.get("http://gmaps-samples.googlecode.com/svn/trunk/poly/puzzledrag.html") polysCount = browser.execute_script('return polys.length;') for iPoly in xrange(polysCount): element = browser.execute_script('var paths = polys[%s].getPaths().getArray();var arMultiPolygons=[];' 'for (j=0;j<paths.length;j++){ var arPolygons=[];' 'for (i=0;i<paths[j].length;i++) ' '{arPolygons.push([paths[j].getArray()[i].lng(),paths[j].getArray()[i].lat()]);} arMultiPolygons.push(p)}' 'return arMultiPolygons;'% str(iPoly)) polygons = [] # for i in element: polygons.append(polygon.Polygon(i)) # cur.execute("insert into googlePolygons (googlegeom,idx) values (geomfromewkt('"+multipolygon.MultiPolygon(polygons).wkt+"')," + str(iPoly)+");") con.commit()
cur.execute("select idx,st_area(geography(st_setsrid(googlegeom,4326)))/1000000, " "st_astext(st_centroid(st_setsrid(googlegeom,4326))) " "from googlePolygons order by idx;") unRealC = cur.fetchall() cur.execute("select name, st_area(geography(st_setsrid(geom,4326)))/1000000, " "st_astext(st_centroid(st_setsrid(geom,4326))) from tm_world_borders;") realC = cur.fetchall() for urc in unRealC: for rc in realC: if float(abs(rc[1]-urc[1])) /rc[1]<0.02: browser.execute_script('pl = new google.maps.Polyline(' '{path: [new google.maps.LatLng('+str(loads(rc[2]).y)+','+ str(loads(rc[2]).x)+'),' ' new google.maps.LatLng('+str(loads(urc[2]).y)+','+ str(loads(urc[2]).x)+')],' 'map: map});')
float(abs(rc[1]-urc[1])) /rc[1]<0.02
- 2% error. Added a line that connected the center of the original polygon with the center of its likely actual location. loads()
- converting WKT to an object.Source: https://habr.com/ru/post/169493/
All Articles