tomchadwin / web2qgis

QGIS webmap scraper
GNU General Public License v2.0
15 stars 2 forks source link

Import Leaflet style #9

Open tomchadwin opened 6 years ago

tomchadwin commented 6 years ago

Not easy - perhaps start with simple styles only (ie when the style is not a function)?

tomchadwin commented 6 years ago

Implemented for object and function styles with simple logic.

tomchadwin commented 6 years ago

Currently doesn't work for point layers, as need to parse their pointToLayer property.

tomchadwin commented 6 years ago

I believe this currently only works with style functions if they are anonymous functions directly set as the value of the style property. In order to handle functions declared independently, will have to retrieve the relevant function. No idea how to do that yet.

tomchadwin commented 6 years ago

OK, so my current starting point is to get loop through all elements in window and return only those which are an instanceof L.Map. I then parse that with Esprima to get everything I need (layers, etc). This works when properties' values are simple values, objects, or anonymous functions, because in all those cases all the relevant code is within the `L.Map'.

However, this fails when something (in this case a style property) has a value which is a function call. The corresponding function definition is obviously not part of the L.Map, so I haven't parsed it.

Do I have to lop through window beforehand, and build up an array of every object instanceof Function? My main L.Map parse with Esprima gives me the function name which is called, so I guess this could work.

But it seems like a lot of work...

tomchadwin commented 6 years ago

No, I think I've got that fundamentally wrong...

My existing loop matches the DOM element which is instanceof L.Map. Function definitions are not part of a DOM element, I guess, so this approach won't work.

So, obviously without resorting to the completely impossible task of retrieving relevant script via regex, how can I retrieve the source of a function's definition when all I have is a call to it? I have parsed the call, so I know the function name.

tomchadwin commented 6 years ago

No, I wasn't wrong. Apparently function definitions within a <script> tag are part of the DOM, so I can get them. Now, how to get a named function definition without looping through all of them...

tomchadwin commented 6 years ago

Good grief. I've gone all the way round this in my head, just to arrive at: window.funcName. That means I can do:

esprima.parse(window.funcName.toString());